public partial class ReadID : Form
{
public ReadID()
{
InitializeComponent();
}
/// <summary>
/// 身份证阅读类
/// </summary>
class CVRSDK
{
[DllImport("termb.dll", EntryPoint = "CVR_InitComm", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int CVR_InitComm(int Port);//声明外部的标准动态库, 跟Win32API是一样的
[DllImport("termb.dll", EntryPoint = "CVR_Authenticate", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int CVR_Authenticate();
[DllImport("termb.dll", EntryPoint = "CVR_Read_Content", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int CVR_Read_Content(int Active);
[DllImport("termb.dll", EntryPoint = "CVR_CloseComm", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int CVR_CloseComm();
[DllImport("termb.dll", EntryPoint = "GetPeopleName", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern unsafe int GetPeopleName(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetPeopleNation", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int GetPeopleNation(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetPeopleBirthday", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleBirthday(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetPeopleAddress", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleAddress(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetPeopleIDCode", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleIDCode(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetDepartment", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetDepartment(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetStartDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetStartDate(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetEndDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetEndDate(ref byte strTmp, ref int strLen);
[DllImport("termb.dll", EntryPoint = "GetPeopleSex", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleSex(ref byte strTmp, ref int strLen);
}
/// <summary>
/// 初始化加载数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ReadID_Load(object sender, EventArgs e)
{
//启动读卡器
try
{
//初始化启动身份证读卡器
bool isok = this.InitCRVSDK();
if (!isok) { return; }
//是否放置好卡片
int authenticate = CVRSDK.CVR_Authenticate();
if (authenticate == 1)
{
//是否读到内容
int readContent = CVRSDK.CVR_Read_Content(4);
if (readContent == 1)
{
FillData();
}
}
else
{
//关闭读卡器
CVRSDK.CVR_CloseComm();
}
}
catch
{
}
}
/// <summary>
/// //初始化读卡器
/// </summary>
/// <returns></returns>
public bool InitCRVSDK()
{
try
{
int iRetUSB = 0, iRetCOM = 0;
int iPort;
// USB 口 1001~1016
for (iPort = 1001; iPort <= 1016; iPort++)
{
iRetUSB = CVRSDK.CVR_InitComm(iPort);
if (iRetUSB == 1)
{
break;
}
}
//连接串口 COM1~COM16
for (iPort = 1; iPort <= 4; iPort++)
{
iRetCOM = CVRSDK.CVR_InitComm(iPort);
if (iRetCOM == 1)
{
break;
}
}
if ((iRetCOM == 1) || (iRetUSB == 1))
{
//MessageBox.Show("读卡器初始化成功!");
return true;
}
else
{
// MessageBox.Show("未连接读卡器!");
return false;//启动失败就不启动定时器
}
}
catch
{
return false;
}
}
/// <summary>
/// // 填充数据
/// </summary>
public void FillData()
{
try
{
//相片 zp.bmp(解码)
this.pic_Photo.ImageLocation = Application.StartupPath + "\zp.bmp";
byte[] name = new byte[30];
int length = 30;
//得到姓名信息 //得到性别信息 //得到民族信息 //得到出生日期 //得到地址信息 //得到卡号信息
//得到发证机关信息 //得到有效开始日期(签发日期) //得到有效截止日期 //读取设备模块号码 参数 返回值 strTmp[out]:
CVRSDK.GetPeopleName(ref name[0], ref length);
//MessageBox.Show();
byte[] number = new byte[30];
length = 36;
CVRSDK.GetPeopleIDCode(ref number[0], ref length);
byte[] people = new byte[30];
length = 3;
CVRSDK.GetPeopleNation(ref people[0], ref length);
byte[] validtermOfStart = new byte[30];
length = 16;
CVRSDK.GetStartDate(ref validtermOfStart[0], ref length);
byte[] birthday = new byte[30];
length = 16;
CVRSDK.GetPeopleBirthday(ref birthday[0], ref length);
byte[] address = new byte[30];
length = 70;
CVRSDK.GetPeopleAddress(ref address[0], ref length);
byte[] validtermOfEnd = new byte[30];
length = 16;
CVRSDK.GetEndDate(ref validtermOfEnd[0], ref length);
byte[] signdate = new byte[30];
length = 30;
CVRSDK.GetDepartment(ref signdate[0], ref length);
byte[] sex = new byte[30];
length = 3;
CVRSDK.GetPeopleSex(ref sex[0], ref length);
this.txt_Address.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(address);
//this.txt_RegAddrres.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(address);
this.cbo_Sex.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(sex);
this.dt_birth.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(birthday);
// lblDept.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(signdate);
this.txt_IdCard.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(number);
this.txt_Name.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(name);
this.cbo_Nation.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(people);
// lblValidDate.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(validtermOfStart) + "-" + System.Text.Encoding.GetEncoding("GB2312").GetString(validtermOfEnd);
}
catch
{
}
}
}
}