Пример #1
0
        private void TimerCallback(object state)
        {
            tmWaitTime.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);

            ScanCancel();

            _errcode = 0;

            receiveEventArgs eventArgs = new receiveEventArgs();

            eventArgs.MRZ1 = "";
            eventArgs.MRZ2 = "";
            CallReceivedData(new object(), eventArgs);
        }
Пример #2
0
        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string MRZ1 = "";
            string MRZ2 = "";

            byte[] tmpBuf  = new byte[1024];
            byte[] recvBuf = new byte[1024];

            Array.Clear(recvBuf, 0x00, recvBuf.Length);

            tmWaitTime.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);

            int tmpCnt  = 0;
            int readCnt = 0;
            int dataLen = 0;

            byte[] szMRZ1 = new byte[45];
            byte[] szMRZ2 = new byte[45];

            Array.Clear(szMRZ1, 0x00, szMRZ1.Length);
            Array.Clear(szMRZ2, 0x00, szMRZ2.Length);

            try
            {
                Array.Clear(tmpBuf, 0x00, tmpBuf.Length);
                if (_serialPort.BytesToRead >= 0)
                {
                    tmpCnt = _serialPort.Read(tmpBuf, 0, 1024);
                }

                Array.Copy(tmpBuf, 0, recvBuf, 0, tmpCnt);

                dataLen = tmpBuf[1] << 8 | tmpBuf[2];
                readCnt = tmpCnt;

                //MessageBox.Show(dataLen.ToString());

                if (dataLen + 4 != readCnt)
                {
                    //MessageBox.Show("readCnt :" + readCnt.ToString() + "         dataLen : " + (dataLen+4).ToString());

                    Array.Clear(tmpBuf, 0x00, tmpBuf.Length);
                    tmpCnt = _serialPort.Read(tmpBuf, 0, (dataLen + 4) - readCnt);

                    //MessageBox.Show("tmpCnt :" + tmpCnt.ToString());

                    Array.Copy(tmpBuf, 0, recvBuf, readCnt, tmpCnt);

                    readCnt += tmpCnt;
                }

                if (recvBuf[0] == 0x02 && recvBuf[readCnt - 2] == 0x03 &&
                    recvBuf[3] == 0xB1 && dataLen == 102)
                {
                    Array.Copy(recvBuf, 4, szMRZ1, 0, 44);
                    Array.Copy(recvBuf, 48, szMRZ2, 0, 44);


                    MRZ1 = Encoding.Default.GetString(szMRZ1, 0, szMRZ1.Length - 1);
                    MRZ2 = Encoding.Default.GetString(szMRZ2, 0, szMRZ2.Length - 1);

                    //MessageBox.Show(MSZ1 + MSZ2);

                    _errcode = 1;

                    receiveEventArgs eventArgs = new receiveEventArgs();
                    eventArgs.MRZ1 = MRZ1;
                    eventArgs.MRZ2 = MRZ2;
                    CallReceivedData(new object(), eventArgs);
                }
                else
                {
                    if (recvBuf[3] != 0xBC)
                    {
                        _errcode = -1;

                        receiveEventArgs eventArgs = new receiveEventArgs();
                        eventArgs.MRZ1 = "";
                        eventArgs.MRZ2 = "";
                        CallReceivedData(new object(), eventArgs);
                    }
                }

                if (_serialPort.IsOpen)
                {
                    _serialPort.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("#2 :" + ex.ToString());

                if (_serialPort.IsOpen)
                {
                    _serialPort.Close();
                }
            }
        }