using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Threading; namespace demo.ExternalDevices { class IDCard { #region "dll声明" /// /// 打开串口/USB。 /// /// [in] 整数, 表示端口号。 1-16(十进制)为串口,1001-1016(十进制)为USB口,缺省的一个USB 设备端口号是 1001。 /// 0x90 打开端口成功;1 打开端口失败/端口号不合法 [DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)] public static extern int SDT_OpenPort(int iPort); /// /// 关闭串口/USB。 /// /// [in] 整数,表示端口号。 /// 0x90 关闭端口成功。 0x01 端口号不合法 [DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)] public static extern int SDT_ClosePort(int iPort); /// /// 读卡 /// /// /// 证/卡芯片管理号, 4 个字节。 /// /// 0x9f 找卡成功;0x80 找卡失败 [DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)] public static extern int SDT_StartFindIDCard(int iPort, byte[] pucManaInfo, int iIfOpen); /// /// 选择卡 /// /// /// 卡体管理号,28 字节。 /// /// 0x90 读成功;其他 读失败(具体含义参见返回码表 [DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)] public static extern int SDT_SelectIDCard(int iPort, byte[] pucManaMsg, int iIfOpen); /// /// 读取基本数据 /// /// /// 读到的文字信息>=256 /// 文字信息长度<=256 /// 读到的相片信息 /// 相片信息长度 /// ,0表示 不在该函数内部打开和关闭串口,此时 不在该函数内部打开和关闭串口,此时 不在该函数内部打开和关闭串口,此时 不在该函数内部打开和关闭串口,此时 不在该函数内部打开和关闭串口,此时 不在该函数内部打开和关闭串口,此时 不在该函数内部打开和关闭串口,此时 应确保之前调用了 确保之前调用了 确保之前调用了 确保之前调用了 SDT_OpenPort 打开端口,并且 打开端口,并且 应在不需要与端口通信时,调用 在不需要与端口通信时,调用 SDT_ClosePort DT_ClosePort 关闭端口;非 关闭端口;非 0表示在 API 函数内部包含了打开端口和关闭,之前不 函数内部包含了打开端口和关闭,之前不 函数内部包含了打开端口和关闭,之前不 需要调用 SDT_OpenPort ,也不用 再调,也不用 再调SDT_ClosePort 。 /// [DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)] public static extern int SDT_ReadBaseMsg(int iPort, byte[] pucCHMsg, ref UInt32 puiCHMsgLen, byte[] pucPHMsg, ref UInt32 puiPHMsgLen, int iIfOpen); /// /// 读取图片; /// /// wlt文件名,后缀是.wlt /// 阅读设备通讯接口类型1串口 2USB /// 1,正确; 0调用stdapi.dll错误,-1相片解码错误,-2wlt文件后最错误,-3wlt文件打开错误,-4wlt文件格式错误,-5软件未授权,-6设备连接错误 [DllImport("WltRS.dll", CallingConvention = CallingConvention.StdCall)] public static extern int GetBmp(String file_name, int Port); [DllImport("sdtapi.dll")] public static extern int SDT_ReadBaseMsgToFile(int iPortID, string fileName1, ref UInt32 puiCHMsgLen, string fileName2, ref UInt32 puiPHMsgLen, int iIfOpen); #endregion //public static object thisLock = new object(); public static int hsection_ReadMsg = sys.sys_InitializeCriticalSection(); public static bool ReadMsg(int portNo, ref string name, ref string IDNo) { sys.sys_EnterCriticalSection(hsection_ReadMsg); //lock (thisLock) //{ bool flag = false; flag = OpenPort(portNo); flag = ReadIDNameAndNo(portNo, ref name, ref IDNo); ClosePort(portNo); sys.sys_LeaveCriticalSection(hsection_ReadMsg); return flag; //} } public static int hsection_OpenPort = sys.sys_InitializeCriticalSection(); public static bool OpenPort(int portNo) { //sys.sys_EnterCriticalSection(hsection_OpenPort); bool flag = false; int ret = 0x90; try { ret = SDT_OpenPort(portNo); if (ret == 144) { flag = true; } } catch { } //sys.sys_LeaveCriticalSection(hsection_OpenPort); return flag; } public static int hsection_ClosePort = sys.sys_InitializeCriticalSection(); public static void ClosePort(int portNo) { //sys.sys_EnterCriticalSection(hsection_ClosePort); SDT_ClosePort(portNo); //sys.sys_LeaveCriticalSection(hsection_ClosePort); } public static int hsection_ReadIDC = sys.sys_InitializeCriticalSection(); public static bool ReadIDNameAndNo(int portNo, ref string name, ref string IDNo) { //sys.sys_EnterCriticalSection(hsection_ReadIDC); bool flag = false; int iRet = 0x90; //返回码 int iIfOpen = 0; //是否需要打开端口 byte[] pubManaInfo = new byte[255]; //身份证管理号信息 byte[] pubManaMsg = new byte[255]; // byte[] pubCHMsg = new byte[512]; //文字信息 byte[] pubPHMsg = new byte[3024]; //照片信息 UInt32 puiCHMsgLen = 0; //文字信息的长度 UInt32 puiPHMsgLen = 0; //照片信息的长度 iRet = SDT_StartFindIDCard(portNo, pubManaInfo, iIfOpen); if (iRet != 159) { sys.sys_LeaveCriticalSection(hsection_ReadIDC); return flag; } iRet = SDT_SelectIDCard(portNo, pubManaMsg, iIfOpen); if (iRet != 144) { sys.sys_LeaveCriticalSection(hsection_ReadIDC); return flag; } iRet = SDT_ReadBaseMsg(portNo, pubCHMsg, ref puiCHMsgLen, pubPHMsg, ref puiPHMsgLen, iIfOpen); if (iRet != 144) { sys.sys_LeaveCriticalSection(hsection_ReadIDC); return flag; } iRet = SDT_ClosePort(portNo); if (iRet != 144) { sys.sys_LeaveCriticalSection(hsection_ReadIDC); return flag; } else { flag = true; string cardMsg = System.Text.ASCIIEncoding.Unicode.GetString(pubCHMsg); name = System.Text.ASCIIEncoding.Unicode.GetString(pubCHMsg, 0, 30).Trim(); IDNo = System.Text.ASCIIEncoding.Unicode.GetString(pubCHMsg, 122, 36).Trim(); } //sys.sys_LeaveCriticalSection(hsection_ReadIDC); return flag; } } }