1. 程式人生 > >新中新二代身份證讀卡Syn_ReadBaseMsg方法

新中新二代身份證讀卡Syn_ReadBaseMsg方法

            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);
                    string cardMsg = new string(' ', 256);  //身份證基本資訊返回長度為256
                    string imgMsg = new string(' ', 1024);  //身份證圖片資訊返回長度為1024
                    IntPtr msg = Marshal.StringToHGlobalAnsi(cardMsg);  //身份證基本資訊
                    IntPtr img = Marshal.StringToHGlobalAnsi(imgMsg);   //身份證圖片
                    try
                    {
                        uint mLen = 0;
                        uint iLen = 0;
                        nRet = Syn_ReadBaseMsg(nPort, msg, ref mLen, img, ref iLen, 0);
                        if (nRet == 0)
                        {
                            string card = Marshal.PtrToStringUni(msg);
                            char[] cartb = card.ToCharArray();
                            CardMsg.Name = (new string(cartb, 0, 15)).Trim();
                            CardMsg.Sex = new string(cartb, 15, 1);
                            CardMsg.Nation = new string(cartb, 16, 2);
                            CardMsg.Born = new string(cartb, 18, 8);
                            CardMsg.Address = (new string(cartb, 26, 35)).Trim();
                            CardMsg.IDCardNo = new string(cartb, 61, 18);
                            CardMsg.GrantDept = (new string(cartb, 79, 15)).Trim();
                            CardMsg.UserLifeBegin = new string(cartb, 94, 8);
                            CardMsg.UserLifeEnd = new string(cartb, 102, 8);
                            return true;
                        }
                        else
                        {
                            errStr = "讀取身份證資訊錯誤";
                        }
                    }
                    catch (Exception e) { errStr = e.Message; }
                    finally
                    {
                        Marshal.FreeHGlobal(msg);
                        Marshal.FreeHGlobal(img);
                    }
                }
                else
                {
                    errStr = "SetMaxRFByte出錯";
                }
            }
            else
            {
                errStr = "開啟埠失敗";
            }
            return false;
        }