using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct IDCardData
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string Name; //姓名   
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
            public string Sex;   //性别
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            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 = 38)]
            public string reserved; // 保留
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
            public string PhotoFileName; // 照片路径
        }
        /************************端口类API *************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetMaxRFByte", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetMaxRFByte(int iPort, byte ucByte, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetCOMBaud", CharSet = CharSet.Ansi)]
        public static extern int Syn_GetCOMBaud(int iPort, ref uint puiBaudRate);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetCOMBaud", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_OpenPort", CharSet = CharSet.Ansi)]
        public static extern int Syn_OpenPort(int iPort);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ClosePort", CharSet = CharSet.Ansi)]
        public static extern int Syn_ClosePort(int iPort);
        /**************************SAM类函数 **************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ResetSAM", CharSet = CharSet.Ansi)]
        public static extern int Syn_ResetSAM(int iPort, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMStatus", CharSet = CharSet.Ansi)]
        public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMID", CharSet = CharSet.Ansi)]
        public static extern int Syn_GetSAMID(int iPort, ref byte pucSAMID, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMIDToStr", CharSet = CharSet.Ansi)]
        public static extern int Syn_GetSAMIDToStr(int iPort, ref byte pcSAMID, int iIfOpen);
        /*************************身份证卡类函数 ***************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_StartFindIDCard", CharSet = CharSet.Ansi)]
        public static extern int Syn_StartFindIDCard(int iPort, ref byte pucIIN, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SelectIDCard", CharSet = CharSet.Ansi)]
        public static extern int Syn_SelectIDCard(int iPort, ref byte pucSN, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi)]
        public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDN", CharSet = CharSet.Ansi)]
        public static extern int Syn_ReadIINSNDN(int iPort, ref byte pucIINSNDN, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsgToFile", CharSet = CharSet.Ansi)]
        public static extern int Syn_ReadBaseMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen, ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDNToASCII", CharSet = CharSet.Ansi)]
        public static extern int Syn_ReadIINSNDNToASCII(int iPort, ref byte pucIINSNDN, int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadNewAppMsg", CharSet = CharSet.Ansi)]
        public static extern int Syn_ReadNewAppMsg(int iPort, ref byte  pucAppMsg ,ref uint puiAppMsgLen , int iIfOpen);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetBmp", CharSet = CharSet.Ansi)]
        public static extern int Syn_GetBmp(int iPort, ref byte Wlt_File);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi)]
        public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindReader", CharSet = CharSet.Ansi)]
        public static extern int Syn_FindReader();
        /***********************设置附加功能函数 ************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoPath", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetPhotoPath(int iOption, ref byte cPhotoPath);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoType", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetPhotoType(int iType);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoName", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetPhotoName(int iType);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetSexType", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetSexType(int iType);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetNationType", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetNationType(int iType);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetBornType", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetBornType(int iType);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeBType", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetUserLifeBType(int iType);
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeEType", CharSet = CharSet.Ansi)]
        public static extern int Syn_SetUserLifeEType(int iType,int iOption);
      
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string stmp;
            int i,nRet; 
            uint[] iBaud=new uint[1];
            i = Syn_FindReader();
            if (i>0)
            {
                if (i>1000)
                {
                    button3.Enabled = false;
                    textBox3.Enabled = false;
                    stmp = Convert.ToString(i);
                    textBox1.Text = stmp;
                    stmp = Convert.ToString(System.DateTime.Now) + "  读卡器连接在USB " + stmp;
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    System.Threading.Thread.Sleep(200); 
                    nRet = Syn_GetCOMBaud(i, ref iBaud[0]);
                    button3.Enabled = true;
                    textBox3.Enabled = true;
                    stmp = Convert.ToString(i);
                    textBox1.Text = stmp;
                    textBox3.Text = Convert.ToString(iBaud[0]);
                    stmp = Convert.ToString(System.DateTime.Now) + "  读卡器连接在COM " + stmp +";当前波特率为 " +Convert.ToString(iBaud[0]);
                    listBox1.Items.Add(stmp);

                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  没有找到读卡器";
                listBox1.Items.Add(stmp);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string stmp;
            int nPort,nRet;
            byte nMaxByte;
            nMaxByte = Convert.ToByte(textBox2.Text);
            nPort = Convert.ToInt32(textBox1.Text);
            nRet = Syn_OpenPort(nPort);
            if (nRet == 0)
            {
                nRet = Syn_SetMaxRFByte(nPort,nMaxByte,0);
                if (nRet == 0)
                {
                    stmp = Convert.ToString(System.DateTime.Now) + "  设置最大通讯字节数成功";
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    stmp = Convert.ToString(System.DateTime.Now) + "  设置最大通讯字节数失败";
                    listBox1.Items.Add(stmp);
                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  打开端口失败";
                listBox1.Items.Add(stmp);
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            byte[] cPath = new byte[255];
            nRet = Syn_SetPhotoPath(0, ref cPath[0]);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放路径设置为 C:,nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string stmp;
            int nPort,nRet;
            uint  uSetBaud;
            uint[] iBaud=new uint[1];
            nPort = Convert.ToInt32(textBox1.Text);
            nRet = Syn_GetCOMBaud(nPort,ref iBaud[0]);
            if (nRet !=0)
            {
                stmp = Convert.ToString(System.DateTime.Now) + "获取当前读卡器波特率失败";
                listBox1.Items.Add(stmp);
                return;
            }
            uSetBaud = Convert.ToUInt32(textBox3.Text);
            if (Syn_OpenPort(nPort)==0)
            {
                nRet = Syn_SetCOMBaud(nPort,iBaud[0],uSetBaud);
                if (nRet == 0)
                {
                    stmp = Convert.ToString(System.DateTime.Now) + " 设置读卡器波特率成功,当前波特率为：" + textBox3.Text ;
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    stmp = Convert.ToString(System.DateTime.Now) + " 设置读卡器波特率失败";
                    listBox1.Items.Add(stmp);
                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + " 打开端口失败";
                listBox1.Items.Add(stmp);
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void button19_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            byte[] cPath = new byte[255];
            nRet = Syn_SetPhotoPath(1, ref cPath[0]);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放路径设置为当前路径,nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoType(0);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放格式为Bmp，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoType(1);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放格式为Jpeg，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoType(2);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放格式为base64，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoName(0);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 tmp，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoName(1);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 姓名，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoName(2);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 身份证号，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = Syn_SetPhotoName(3);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 姓名_身份证号，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            int nRet,nPort;
            string stmp;
            nPort = Convert.ToInt32(textBox1.Text);
            if ( Syn_OpenPort(nPort)==0)
            {
                nRet = Syn_ResetSAM(nPort, 0);
                if (nRet == 0)
                {
                    stmp = Convert.ToString(System.DateTime.Now) + " 复位SAM模块成功";
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    stmp = Convert.ToString(System.DateTime.Now) + " 复位SAM模块失败";
                    listBox1.Items.Add(stmp);
                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  打开端口失败";
                listBox1.Items.Add(stmp);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            byte[] cPath = new byte[255];
            folderBrowserDialog1.SelectedPath="c:\\"; 
            folderBrowserDialog1.ShowDialog();
            stmp = folderBrowserDialog1.SelectedPath;
            cPath = System.Text.Encoding.Default.GetBytes(stmp);
            nRet = Syn_SetPhotoPath(2, ref cPath[0]);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放路径设置为"+folderBrowserDialog1.SelectedPath+",nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            int nRet, nPort;
            string stmp;
            nPort = Convert.ToInt32(textBox1.Text);
            if (Syn_OpenPort(nPort) == 0)
            {
                nRet = Syn_GetSAMStatus(nPort, 0);
                if (nRet == 0)
                {
                    stmp = Convert.ToString(System.DateTime.Now) + " SAM状态正常，nRet =" + Convert.ToString(nRet) ;
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    stmp = Convert.ToString(System.DateTime.Now) + " SAM状态正常，nRet =" + Convert.ToString(nRet);
                    listBox1.Items.Add(stmp);
                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  打开端口失败";
                listBox1.Items.Add(stmp);
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            int nRet, nPort;
            byte[] uSAMID = new byte[128];
            string stmp;
            nPort = Convert.ToInt32(textBox1.Text);
            if (Syn_OpenPort(nPort) == 0)
            {
                nRet = Syn_GetSAMIDToStr(nPort, ref uSAMID[0], 0);
                if (nRet ==0)
                {
                    stmp = System.Text.Encoding.Default.GetString(uSAMID);
                    stmp = Convert.ToString(System.DateTime.Now) + "  获得SAMID成功，ID：" + stmp;
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    stmp = Convert.ToString(System.DateTime.Now) + "  获得SAMID失败";
                    listBox1.Items.Add(stmp);
                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  打开端口失败";
                listBox1.Items.Add(stmp);
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            IDCardData CardMsg = new IDCardData();
            int nRet,nPort;
            string stmp;
            byte[] pucIIN = new byte[4];
            byte[] pucSN = new byte[8];
            nPort = Convert.ToInt32(textBox1.Text);
            if (Syn_OpenPort(nPort) == 0)
            {
                if (Syn_SetMaxRFByte(nPort,80,0)==0)
                {
                    nRet = Syn_StartFindIDCard(nPort, ref pucIIN[0], 0);
                    nRet = Syn_SelectIDCard(nPort,ref pucSN[0], 0);
                    nRet = Syn_ReadMsg(nPort, 0, ref CardMsg);
                    if (nRet == 0)
                    {
                        stmp = Convert.ToString(System.DateTime.Now) + "  姓名:" + CardMsg.Name;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  性别:" + CardMsg.Sex;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  民族:" + CardMsg.Nation;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  出生日期:" + CardMsg.Born;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  地址:" + CardMsg.Address;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  身份证号:" + CardMsg.IDCardNo;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  发证机关:" + CardMsg.GrantDept;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  有效期开始:" + CardMsg.UserLifeBegin;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  有效期结束:" + CardMsg.UserLifeEnd;
                        listBox1.Items.Add(stmp);
                        stmp = Convert.ToString(System.DateTime.Now) + "  照片文件名:" + CardMsg.PhotoFileName;
                        listBox1.Items.Add(stmp);
                    }
                    else
                    {
                        stmp = Convert.ToString(System.DateTime.Now) + "  读取身份证信息错误";
                        listBox1.Items.Add(stmp);
                    }
                }
            }
            else
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  打开端口失败";
                listBox1.Items.Add(stmp);
            }
        }
    }
}
