读取操作
int nRet;
string sMsg = "";
IDCardData idcardData = new IDCardData();
byte[] ctmp = new byte[255];
ReadCardAPI.Syn_SetPhotoPath(1, ref ctmp);
setupDataFormate(1);
nRet = ReadCardAPI.Syn_OpenPort(Main_Form.m_iPort);
if (nRet == 0)
{
DateTime startTime = DateTime.Now;
nRet = ReadCardAPI.Syn_ReadMsg(Main_Form.m_iPort, 0, ref idcardData);
int dTime = (int)(DateTime.Now - startTime).TotalMilliseconds;
if (Main_Form.m_iReadStart == 1)
{
Main_Form.m_iReadSum++;
}
if (nRet == 0 || nRet == 1) //0读取身份证信息成功,1读取身份证信息成功,解码照片不成功,其他.读取身份证信息失败
{
if (Main_Form.m_iReadStart == 1)
{
Main_Form.m_iReadOKSum++;
}
ShowIDcardInfo(idcardData);
sMsg = "读取身份证信息成功," + dTime + "ms";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
}
else
{
sMsg = "读取身份证信息错误," + dTime + "ms";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
}
if (Main_Form.m_iReadStart == 1)
{
sMsg = "第" + Main_Form.m_iReadSum + "次读二代证";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
}
}
else
{
sMsg = "打开端口错误";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
}
if (Main_Form.m_iPort > 0)
{
ReadCardAPI.Syn_ClosePort(Main_Form.m_iPort);
}
解析二代证
///
/// 显示读取到的身份证的信息
///
///
private void ShowIDcardInfo(IDCardData idcardData)
{
string sMsg = "";
sMsg = "照片路径:" + idcardData.PhotoFileName;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
if (idcardData.CardType[0] == 'J')//港澳台通行证
{
sMsg=("签发次数:"+ idcardData.IssuesTimes);
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + (sMsg));
sMsg="通行证号码:"+ idcardData.PassID;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + (sMsg));
}
sMsg = "有效期结束:" + idcardData.UserLifeEnd;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "有效期开始:" + idcardData.UserLifeBegin;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "发证机关:" + idcardData.GrantDept;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "身份证号:" + idcardData.IDCardNo;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "住址:" + idcardData.Address;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "出生:" + idcardData.Born;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "民族:" + idcardData.Nation;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "性别:" + idcardData.Sex;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
if (idcardData.CardType == "I")
{//外国人居住证
sMsg = "证件版本:" + idcardData.CertVol;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
sMsg = "英文姓名:" + idcardData.EngName;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
}
sMsg = "姓名:" + idcardData.Name;
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
if (idcardData.CardType[0] == 'I')
{
sMsg="证件类型:外国人永久居住证";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + sMsg);
}
else if (idcardData.CardType[0] == 'J')
{
sMsg="证件类型:港澳台居民居住证";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + (sMsg));
}
else
{
sMsg="证件类型:居民身份证";
mainForm.listBox_log.Items.Insert(0, DateTime.Now.ToString() + "\t" + (sMsg));
}
//这样做可以及时释放文件占用,否则第二次刷卡会报错
Image tmp = Image.FromFile(idcardData.PhotoFileName);
Bitmap img = new Bitmap(tmp);
tmp.Dispose();
mainForm.pictureBox_img.Image = img;
}
封装读卡类
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace SynjonesReader
{
public static class ReadCardAPI
{
//
// SAM端口函数
//
//
///
/// 查看串口当前波特率(该函数只用于SAM采用RS232串口的情形,如果采用USB接口则不支持该API)
///
/// [in] 整数,表示端口号。此处端口号必须为1-16,表示串口
/// [out] 无符号整数指针,指向普通串口当前波特率, 默认情况下为115200
/// 0成功,1失败/不合法,5无法获得该SAM的波特率,该SAM串口不可用
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_GetCOMBaud")]
public static extern int Syn_GetCOMBaud(int iPort, ref int puiBaudRate);
///
/// 设置SAM的串口的波特率(该函数只用于SAM采用RS232串口的情形,如果采用USB接口则不支持该API),
/// 设置成功后,在该SAM和主机注册表中都记录设置后的波特率,保证在SAM重新启动和该套API被重新调用时采用设置后的波特率。
/// 该函数调用成功后,需要延时5毫秒,然后才能继续与SAM通信
///
/// [in] 整数,表示端口号。此处端口号必须为1-16,表示串口。
///
/// [in] 无符号整数,调用该API前已设置的业务终端与SAM通信的波特率(SAM出厂时默认,业务终端与SAM通信的波特率为115200).
/// 业务终端以该波特率与SAM通信,发出设置SAM新波特率的命令.。
/// uiCurrBaud只能为下列数值之一:115200,57600,38400,19200,9600.如果uiCurrBaud数值不是这些值之一,函数返回0X21;
/// 如果已设置的波特率与uiCurrBaud不一致, 则函数返回0X02,表示不能设置,调用API不成功。
///
///
/// [in] 无符号整数,将要设置的SAM与业务终端通信波特率。
/// uiSetBaud只能取下列值之一::115200,57600,38400,19200,9600,
/// 如果输入uiSetBaud参数不是这些数值之一,,函数返回0X21,设置不成功,保持原来的波特率不变。
///
///
/// 0成功,1端口打开失败/端口号不合法,2超时,设置不成功,33 uiCurrBaud 、uiSetBaud输入参数数值错误
///
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetCOMBaud")]
public static extern int Syn_SetCOMBaud(int iPort, int uiCurrBaud, int uiSetBaud);
///
/// 打开端口
///
/// 端口号。1-16(十进制)为串口,1001-1016(十进制)为USB口,9999为USBHID
/// 0成功,1打开端口失败/端口号不合法
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_OpenPort")]
public static extern int Syn_OpenPort(int iPort);
///
/// 关闭端口
///
/// [in]整数,表示端口号
/// 0成功,1端口不合法
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ClosePort")]
public static extern int Syn_ClosePort(int iPort);
//
// SAM类函数
//
//
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetMaxRFByte")]
public static extern int Syn_SetMaxRFByte(int iPort,byte ucByte, int bIfOpen);
///
/// 对SAM复位
///
///
/// [in] 整数,表示端口号。
/// 根据SAM使用的接口不同(分为普通串口SAM和USB口SAM),
/// 分别使用不同的端口号(目前串口和USB都只支持16个,即串口0001-0016和USB1001-1016)
///
///
/// [in] 整数,0表示不在该函数内部打开和关闭串口,
/// 此时确保之前调用了Syn_OpenPort来打开端口,并且在不需要与端口通信时,调用Syn_ClosePort关闭端口;
/// 非0表示在API函数内部包含了打开端口和关闭端口函数,之前不需要调用Syn_OpenPort,也不用再调用Syn_ClosePort
///
/// 0成功,其他失败(具体参见返回码表)
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ResetSAM")]
public static extern int Syn_ResetSAM(int iPort, int iIfOpen);
///
/// 对SAM进行状态检测
///
/// [in] 整数,表示端口号。参见Syn_ResetSAM
/// [in] 整数,参见Syn_ResetSAM
/// 0SAM正常,96自检失败,不能接收命令,其他,命令失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_GetSAMStatus")]
public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen);
///
/// 读取SAM的编号
///
/// [in] 整数,表示端口号。参见Syn_ResetSAM
/// [out] 无符号字符串指针,指向读到的SAM编号, 16字节
///
/// 0成功,其他,失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_GetSAMID")]
public static extern int Syn_GetSAMID(int iPort, byte[] pucSAMID, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_GetSAMIDToStr")]
public static extern int Syn_GetSAMIDToStr(int iPort, ref char[] pucSAMID, int iIfOpen);
//
// 身份证卡类函数
//
//
///
/// 开始寻卡
///
/// [in] 整数,表示端口号。参见Syn_ResetSAM
/// [out] 无符号字符指针,指向读到的IIN
/// [in] 整数,参见Syn_ResetSAM
/// 0成功,128失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_StartFindIDCard")]
public static extern int Syn_StartFindIDCard(int iPort, ref byte[] pucIIN, int iIfOpen);
///
/// 选卡
///
/// [in] 整数,表示端口号。参见Syn_ResetSAM
/// [out] 无符号字符指针,指向读到的SN
/// [in] 整数,参见Syn_ResetSAM
/// 0选卡成功,129选卡失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SelectIDCard")]
public static extern int Syn_SelectIDCard(int iPort, ref byte[] pucIIN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadBaseMsg")]
public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref int puiCHMsgLen,ref byte pucPHMsg,ref int puiPHMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadBaseMsgToFile")]
public static extern int Syn_ReadBaseMsgToFile(int iPort, ref char[] pcCHMsgFileName, ref int puiCHMsgLen, ref char[] pcPHMsgFileName, ref int puiPHMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadBaseFPMsg")]
public static extern int Syn_ReadBaseFPMsg(int iPort, ref byte pucCHMsg, ref int puiCHMsgLen, ref byte pucPHMsg, ref int puiPHMsgLen, ref byte pucFPMsg, ref int puiFPMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadBaseFPMsgToFile")]
public static extern int Syn_ReadBaseFPMsgToFile(int iPort, ref char[] pcCHMsgFileName, ref int puiCHMsgFileLen, ref char[] pcPHMsgFileName, ref int puiPHMsgFileLen, ref char[] pcFPMsgFileName, ref int puiFPMsgFileLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadNewAppMsg")]
public static extern int Syn_ReadNewAppMsg(int iPort, ref byte pucAppMsg, ref int puiAppMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_GetBmp")]
public static extern int Syn_GetBmp(int iPort, ref char[] Wlt_File);
///
/// 读取身份证中的基本信息和照片信息,并按设置转化信息和照片
///
/// [in] 整数,表示端口号。参见Syn_ResetSAM
/// [in] 整数,参见Syn_ResetSAM
/// [out] IDCardData信息
/// 0读取身份证信息成功,1读取身份证信息成功,解码照片不成功,其他.读取身份证信息失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadMsg")]
public static extern int Syn_ReadMsg(int iPort, int iIfOpen, ref IDCardData pIDCardData);
///
/// 读取身份证中的基本信息和照片信息,并按设置转化信息和照片
///
/// [in] 整数,表示端口号。参见Syn_ResetSAM
/// [in] 整数,参见Syn_ResetSAM
/// [out] IDCardData信息
/// cFPhotoName返回指纹信息文件名
/// 0读取身份证信息成功,无指纹信息,1读取身份证信息成功,指纹读取成功,其他.读取身份证信息失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_ReadFPMsg")]
public static extern int Syn_ReadFPMsg(int iPort, int iIfOpen, ref IDCardData pIDCardData, byte[] cFPhotoname);
///
/// 自动寻找读卡器
///
/// 0失败,大于0为串口号或者端口号
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_FindReader")]
public static extern int Syn_FindReader();
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_FindUSBReader")]
public static extern int Syn_FindUSBReader();
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_BmpToJpeg")]
public static extern int Syn_BmpToJpeg(ref char[] cBmpName, ref char[] cJpegName);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_PhotoToStrBase64")]
public static extern int Syn_PhotoToStrBase64(ref char[] cBase64, ref int iLen, ref char[] cPhotoName);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_StrBase64ToPhoto")]
public static extern int Syn_StrBase64ToPhoto(ref char[] cBase64, int iLen, ref char[] cPhotoName);
//
// 设置附加功能函数
//
//
///
/// 设置照片文件存储的路径
///
/// [in] 整形,0=C:根目录, 1=当前路径 ,2=指定路径
/// [in] 字符指针。路径名
///
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetPhotoPath")]
public static extern int Syn_SetPhotoPath(int iOption, ref byte[] cPhotoPath);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetPhotoType")]
public static extern int Syn_SetPhotoType(int iType);
///
/// 设置照片文件的文件名
///
/// [in]整形。0=tmp,1=姓名,2=身份证号,3=姓名_身份证号
/// 0成功,-1失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetPhotoName")]
public static extern int Syn_SetPhotoName(int iType);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetPhotoNameEx")]
public static extern int Syn_SetPhotoNameEx(int iType, ref char[] cPhotoname);
///
/// 设置返回性别的格式
///
/// [in] 整形。0=卡内存储的数据, 1=解释之后的数据
/// 0成功,-1失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetSexType")]
public static extern int Syn_SetSexType(int iType);
///
/// 设置返回民族的格式
///
/// [in]整形。0=卡内存储的数据,1=解释之后的数据,2=解释之后+“族”
/// 0成功,-1失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetNationType")]
public static extern int Syn_SetNationType(int iType);
///
/// 设置返回出生日期的格式
///
/// in] 整形。0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD
/// 0成功,-1失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetBornType")]
public static extern int Syn_SetBornType(int iType);
///
/// 设置返回有效期开始日期的格式
///
/// [in] 整形。0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD
/// 0成功,-1失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetUserLifeBType")]
public static extern int Syn_SetUserLifeBType(int iType);
///
/// 设置返回有效期结束日期的格式
///
/// [in] 整形。0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD
/// [in] 整形。0=长期不转换 1=长期转换为 有效期开始加50年
/// 0成功,-1失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SetUserLifeEType")]
public static extern int Syn_SetUserLifeEType(int iType, int iOption);
//
// USBHID函数
//
//
///
/// 复位M1卡片,返回M1卡片的卡号和卡类型
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [out]无符号四字节,返回M1卡片卡号
/// [out] 无符号字节数组,返回M1卡卡类型
/// 0成功,其他失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDM1Reset")]
public static extern int Syn_USBHIDM1Reset(int iPort, ref UInt32 pdwCardSN, ref byte pbSize);
///
/// 用密钥验证M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [in]无符号字节,验证类型,0—密钥A验证 1—密钥B验证
/// [in]无符号字节,M1卡块号
/// [in]无符号字节数组,6字节,密钥数组
/// 0成功,128失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDM1AuthenKey")]
public static extern int Syn_USBHIDM1AuthenKey(int iPort, byte KeyType, byte BlockNo, byte[] pKey);
///
/// 读M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [in]无符号字节,M1卡块号
/// [out]无符号字节数组,16字节,读取的数据
/// 0成功,其他失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDM1ReadBlock")]
public static extern int Syn_USBHIDM1ReadBlock(int iPort, byte BlockNo, byte[] pBlock);
///
/// 写M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [in]无符号字节,M1卡块号
/// [in]无符号字节数组,16字节,读取的数据
/// 0成功
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDM1WriteBlock")]
public static extern int Syn_USBHIDM1WriteBlock(int iPort, byte BlockNo, byte[] pBlock);
///
/// 挂起M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// 0成功
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDM1Halt")]
public static extern int Syn_USBHIDM1Halt(int iPort);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDReadFactoryInfo")]
public static extern int Syn_USBHIDReadFactoryInfo(int iPort, ref byte pFactory, ref byte pVol, ref byte pSerialNo, ref byte pReserved);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDWriteFactoryInfo")]
public static extern int Syn_USBHIDWriteFactoryInfo(int iPort, ref byte pFactory, ref byte pVol, ref byte pSerialNo, ref byte pReserved);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDReadUserInfo")]
public static extern int Syn_USBHIDReadUserInfo(int iPort, ref byte pInfo);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDWriteUserInfo")]
public static extern int Syn_USBHIDWriteUserInfo(int iPort, ref byte pInfo);
///
/// 获得读卡器支持的最大通讯字节数和版本
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [out]无符号字节,读卡器支持的最大通讯字节数
/// [out]无符号字节数组,版本号
///
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDGetMaxByte")]
public static extern int Syn_USBHIDGetMaxByte(int iPort, ref byte pSAMMaxByte, ref byte pARMVol);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDSendSound")]
public static extern int Syn_USBHIDSendSound(int iPort, byte ucSound);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDControlLED")]
public static extern int Syn_USBHIDControlLED(int iPort, byte ucLed);
///
/// 控制蜂鸣器命令(仅A16D-HF)
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [in]无符号整形,命令,暂定为0X04
/// [in]整形,蜂鸣器响时间,毫秒计算
/// 0成功
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDControlBeep")]
public static extern int Syn_USBHIDControlBeep(int iPort, byte ucType, int usTime);
///
/// 读取二代证ID号(仅A16D-HF)
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [out] 无符号字节数组,长度8字节
/// 0成功,128失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDGetSecCardID")]
public static extern int Syn_USBHIDGetSecCardID(int iPort, byte[] pdwCardSN);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDSIMpowerOn")]
public static extern int Syn_USBHIDSIMpowerOn(int iPort, ref byte ucATR,ref byte ucLen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDSIMpowerOff")]
public static extern int Syn_USBHIDSIMpowerOff(int iPort);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDSIMAPDU")]
public static extern int Syn_USBHIDSIMAPDU(int iPort, int slen, ref byte datasend, ref int rlen, ref byte datareceive);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDSetUserIDCode")]
public static extern int Syn_USBHIDSetUserIDCode(int iPort, ref byte code, int len);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDSetUserIDCode")]
public static extern int Syn_USBHIDWriteUserMac(int iPort, ref byte pMac);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDIsEncryptMode")]
public static extern int Syn_USBHIDIsEncryptMode(int iPort);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_USBHIDIsEncryptMode")]
public static extern int Syn_USBHIDdoUpdate(int iPort, ref byte updateBuffer, int bufferLen, int progressCallback);
//
// 串口操作M1卡函数
//
//
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialFindReader")]
public static extern int Syn_SerialFindReader(int iPort, ref int badurate);
///
/// 打开串口
///
/// 串口号(1-16)
/// 波特率(可使用Syn_GetCOMBaud获得)
/// 0成功,其他失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialOpen")]
public static extern int Syn_SerialOpen(int iPort, int badurate);
///
/// 关闭串口
///
/// 串口号(1-16)
///
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialClose")]
public static extern int Syn_SerialClose(int iPort);
///
/// 复位M1卡片,返回M1卡片的卡号和卡类型
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [out]无符号四字节,返回M1卡片卡号
/// [out] 无符号字节数组,返回M1卡卡类型
/// 0成功,其他失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialM1Reset")]
public static extern int Syn_SerialM1Reset(int iPort, ref UInt32 pdwCardSN, ref byte pbSize);
///
/// 用密钥验证M1卡
///
/// [in]整形,读卡器的串口号(1-16),可以使用Syn_FindReader获得
/// [in]无符号字节,验证类型,0—密钥A验证 1—密钥B验证
/// [in]无符号字节,M1卡块号
/// [in]无符号字节数组,6字节,密钥数组
/// 0成功,128失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialM1AuthenKey")]
public static extern int Syn_SerialM1AuthenKey(int iPort,byte KeyType, byte BlockNo, byte[] pKey);
///
/// 读M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [in]无符号字节,M1卡块号
/// [out]无符号字节数组,16字节,读取的数据
/// 0成功,其他失败
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialM1ReadBlock")]
public static extern int Syn_SerialM1ReadBlock(int iPort, byte BlockNo, byte[] pBlock);
///
/// 写M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// [in]无符号字节,M1卡块号
/// [in]无符号字节数组,16字节,读取的数据
/// 0成功
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialM1WriteBlock")]
public static extern int Syn_SerialM1WriteBlock(int iPort, byte BlockNo, byte[] pBlock);
///
/// 挂起M1卡
///
/// [in]整形,读卡器的端口号,可以使用Syn_FindReader获得
/// 0成功
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_SerialM1Halt")]
public static extern int Syn_SerialM1Halt(int iPort);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_StartFindCPUCard")]
public static extern int Syn_StartFindCPUCard(int iPort, byte[] ATS, ref int rlen);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_StartRemoveCPUCard")]
public static extern int Syn_StartRemoveCPUCard(int iPort, byte[] ATS);
[DllImport("SynIDCardAPI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, EntryPoint = "Syn_PCTOCPUCard")]
public static extern int Syn_PCTOCPUCard(int iPort, int slen, byte[] datasend, ref int rlen, byte[] datareceive);
///
/// 寻找USB读卡器
///
///
public static int findUSBDriverDevice()
{
int nRet;
for (int i = 1001; i < 1017; i++)
{
nRet = ReadCardAPI.Syn_OpenPort(i);
if (nRet == 0)
{
nRet = ReadCardAPI.Syn_GetSAMStatus(i, 0);
if (nRet == 0)
{
ReadCardAPI.Syn_ClosePort(i);
return i;
}
ReadCardAPI.Syn_ClosePort(i);
}
}
return 0;
}
///
/// 寻找USBHID读卡器
///
///
public static int findUSBHIDDevice()
{
int nRet;
nRet = Syn_OpenPort(9999);
if (nRet == 0)
{
Syn_ClosePort(9999);
return 9999;
}
return 0;
}
///
/// 寻找串口读卡器
///
///
public static int findSerialDevice()
{
int nRet = 0;
int m_iBaud = 0;
for (int k = 1; k < 50; k++)
{
nRet = Syn_SerialFindReader(k, ref m_iBaud);
if (nRet == 0)
{
return k;
}
}
for (int j = 1; j < 17; j++)
{
nRet = Syn_GetCOMBaud(j, ref m_iBaud);
if (nRet == 0)
{
nRet = Syn_SetCOMBaud(j, m_iBaud, m_iBaud);
if (nRet == 0)
{
Syn_ClosePort(j);
return j;
}
Syn_ClosePort(j);
}
}
return 0;
}
}
#region 结构体
//[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct IDCardData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] //ByValTStr ByValArray CertVol
public string Name; //姓名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string Sex; //性别
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string Nation; //民族
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string Born; //出生日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 72)]
public string Address; //住址
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)]
public string IDCardNo; //身份证号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string GrantDept; //发证机关
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string UserLifeBegin; // 有效开始日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string UserLifeEnd; // 有效截止日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string PassID; //通行证号码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string IssuesTimes; //签发次数
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)]
public string reserved; // 保留
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string PhotoFileName;// 照片路径
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string CardType;// 证件类型
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 122)]
public string EngName;// 英文名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string CertVol;// 证件版本号
}
#endregion
}