Beispiel #1
0
        /// <summary>
        /// 成功返回非0,失败返回0
        /// </summary>
        /// <returns></returns>
        private unsafe static void GetCanData()
        {
            Dictionary <string, byte[]> barCodeData = new Dictionary <string, byte[]>();
            var CameraData = new Dictionary <uint, System.IO.MemoryStream>();
            Dictionary <uint, System.IO.MemoryStream> YNZhiJingMaiData = new Dictionary <uint, System.IO.MemoryStream>();

            IntPtr pt  = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VCI_CAN_OBJ)) * 100);
            UInt32 res = 0;

            byte[] buf1 = new byte[8];

            while (m_bOpen)
            {
                for (uint m_canind = 0; m_canind < m_CanCount; m_canind++)
                {
                    try
                    {
                        res = AdvCan.VCI_GetReceiveNum(m_devtype, m_devind, m_canind);
                        if (res == 0)
                        {
                            continue;
                        }
                        res = AdvCan.VCI_Receive(m_devtype, m_devind, m_canind, pt, 100, 100);
                        for (uint i = 0; i < res; i++)
                        {
                            VCI_CAN_OBJ obj = (VCI_CAN_OBJ)Marshal.PtrToStructure((IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(VCI_CAN_OBJ))), typeof(VCI_CAN_OBJ));
                            //Package receiving ok
                            uint cmd = (obj.ID >> 20) & 0xFF;
                            if ((cmd == 0x01 || cmd == 0x02 || cmd == 0x05 || cmd == 0x06) && (obj.ID & 0xFF) >= 0x80)
                            {
                                //条码的数据,证卡的数据
                                uint   BiaoZhi = (obj.ID >> 8) & 0xFFF;
                                byte[] buf;
                                if (barCodeData.ContainsKey(BiaoZhi.ToString()))
                                {
                                    buf = barCodeData[BiaoZhi.ToString()];
                                }
                                else
                                {
                                    buf = new byte[1024];
                                    barCodeData.Add(BiaoZhi.ToString(), buf);
                                }
                                uint j = obj.ID & 0xFF;
                                System.Runtime.InteropServices.Marshal.Copy((IntPtr)obj.Data, buf1, 0, 8);
                                Array.Copy(buf1, 0, buf, (j - 0x80 + 1) * 8 + 4, 8);
                            }
                            else if (cmd == 0x01 || cmd == 0x02 || cmd == 0x05 || cmd == 0x06)
                            {
                                //条码,证卡的头部
                                uint   BiaoZhi = (obj.ID >> 8) & 0xFFF;
                                byte[] buf;
                                if (barCodeData.ContainsKey(BiaoZhi.ToString()))
                                {
                                    buf = barCodeData[BiaoZhi.ToString()];
                                    barCodeData.Remove(BiaoZhi.ToString());
                                }
                                else
                                {
                                    buf = new byte[1024];
                                }
                                buf[0] = (byte)((obj.ID >> 20) & 0xFF); //code
                                buf[1] = (byte)((obj.ID >> 0) & 0xFF);  //m0
                                buf[2] = (byte)((obj.ID >> 8) & 0xFF);  //m1
                                buf[3] = (byte)((obj.ID >> 16) & 0xF);  //m2
                                System.Runtime.InteropServices.Marshal.Copy((IntPtr)obj.Data, buf1, 0, 8);
                                Array.Copy(buf1, 0, buf, 4, 8);
                                OnCanGetData(buf);
                            }
                            else if (cmd == 0x0A)
                            {
                                //燕南指静脉
                                uint BN = obj.ID & 0xFFFFF;
                                System.IO.MemoryStream ms;
                                if (YNZhiJingMaiData.ContainsKey(BN))
                                {
                                    ms = YNZhiJingMaiData[BN];
                                }
                                else
                                {
                                    ms = new System.IO.MemoryStream();
                                    YNZhiJingMaiData.Add(BN, ms);
                                }
                                System.Runtime.InteropServices.Marshal.Copy((IntPtr)obj.Data, buf1, 0, 8);
                                ms.Write(buf1, 0, 8);
                            }
                            else if (cmd == 0x0B)
                            {
                                //燕南指静脉
                                uint BN = obj.ID & 0xFFFFF;
                                System.IO.MemoryStream ms;
                                if (YNZhiJingMaiData.ContainsKey(BN))
                                {
                                    ms = YNZhiJingMaiData[BN];
                                    YNZhiJingMaiData.Remove(BN);
                                }
                                else
                                {
                                    ms = new System.IO.MemoryStream();
                                }
                                ms.Seek(0, System.IO.SeekOrigin.Begin);
                                int    getLen = (int)ms.Length;
                                byte[] buf    = new byte[getLen + 12];
                                ms.Read(buf, 12, getLen);
                                ms.Close();

                                int  datalen  = (int)(obj.Data[1] | (obj.Data[2] << 8));
                                uint checkSum = (uint)(obj.Data[3] | (obj.Data[4] << 8) | (obj.Data[5] << 16) | (obj.Data[6] << 24));
                                if ((datalen > getLen) || (getLen - datalen) > 8)
                                {
                                    LogInfo.Log.WriteInfo(LogInfo.LogType.Error, "燕南指静脉,接收数据不完整。收到:" + getLen.ToString() + ",应该:" + datalen.ToString());
                                }
                                else
                                {
                                    uint sum = 0;
                                    for (int ii = 0; ii < datalen; ii++)
                                    {
                                        sum += buf[12 + ii];
                                    }
                                    if (sum == checkSum)
                                    {
                                        buf[0] = (byte)((obj.ID >> 20) & 0xFF);                                         //code
                                        buf[1] = (byte)((obj.ID >> 0) & 0xFF);                                          //m0
                                        buf[2] = (byte)((obj.ID >> 8) & 0xFF);                                          //m1
                                        buf[3] = (byte)((obj.ID >> 16) & 0xF);                                          //m2
                                        for (int ii = 0; ii < 8; ii++)
                                        {
                                            buf[4 + ii] = obj.Data[ii];
                                        }

                                        OnCanGetData(buf);
                                    }
                                    else
                                    {
                                        LogInfo.Log.WriteInfo(LogInfo.LogType.Error, "燕南指静脉,接收数据,校验失败。");
                                    }
                                }
                            }
                            else if (cmd == 0xE7)
                            {
                                //串口摄像头图片数据
                                uint BN = obj.ID & 0xFFFFF;
                                System.IO.MemoryStream ms;
                                if (CameraData.ContainsKey(BN))
                                {
                                    ms = CameraData[BN];
                                }
                                else
                                {
                                    ms = new System.IO.MemoryStream();
                                    CameraData.Add(BN, ms);
                                }
                                Marshal.Copy((IntPtr)obj.Data, buf1, 0, 8);
                                ms.Write(buf1, 0, 8);
                            }
                            else if (cmd == 0xE8)
                            {
                                //串口摄像头图片传输完成,长度
                                uint bn = obj.ID & 0xFFFFF;
                                System.IO.MemoryStream ms;
                                if (CameraData.ContainsKey(bn))
                                {
                                    ms = CameraData[bn];
                                    CameraData.Remove(bn);
                                }
                                else
                                {
                                    ms = new System.IO.MemoryStream();
                                }
                                ms.Seek(0, System.IO.SeekOrigin.Begin);
                                byte[] buf = new byte[ms.Length + 12];
                                ms.Read(buf, 12, buf.Length - 12);
                                ms.Close();

                                buf[0] = (byte)((obj.ID >> 20) & 0xFF); //code
                                buf[1] = (byte)((obj.ID >> 0) & 0xFF);  //m0
                                buf[2] = (byte)((obj.ID >> 8) & 0xFF);  //m1
                                buf[3] = (byte)((obj.ID >> 16) & 0xF);  //m2
                                buf[4] = obj.Data[0];
                                buf[5] = obj.Data[1];
                                buf[6] = obj.Data[2];
                                buf[7] = obj.Data[3];
                                OnCanGetData(buf);
                            }
                            else
                            {
                                byte[] buf = new byte[12];
                                buf[0] = (byte)((obj.ID >> 20) & 0xFF); //code
                                buf[1] = (byte)((obj.ID >> 0) & 0xFF);  //m0
                                buf[2] = (byte)((obj.ID >> 8) & 0xFF);  //m1
                                buf[3] = (byte)((obj.ID >> 16) & 0xF);  //m2
                                Marshal.Copy((IntPtr)obj.Data, buf1, 0, 8);
                                Array.Copy(buf1, 0, buf, 4, 8);
                                OnCanGetData(buf);
                            }
                        }//for
                    }
                    catch (Exception ee)
                    {
                        Log.WriteFileLog("接收数据:" + ee);
                    }
                } //for
                Thread.Sleep(10);
            }     //while
        }