示例#1
0
        /// <summary>
        /// 串口接收数据处理事件
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="args">事件参数</param>
        public void OnRecData(object sender, SerialDataReceivedEventArgs args)
        {
            serialPort.ReceivedBytesThreshold = 8092;

            try
            {
                int recSize;
                recSize = serialPort.BytesToRead;
                SerialRecMsgEventArgs sme = new SerialRecMsgEventArgs();
                sme.MessageBuf = new byte[recSize];
                serialPort.Read(sme.MessageBuf, 0, recSize);
                OnRecMsg((object)this, sme);

            }
            catch (Exception e)
            {
                ReportException(e, 0);
            }
            finally
            {
                IsBufferLocked = false;
                serialPort.ReceivedBytesThreshold = 1;
            }
        }
示例#2
0
        void xbee_OnRecMsg(object sender, SerialRecMsgEventArgs args)
        {
            while (!mutex)
            {
                Thread.Sleep(1);
            }
            mutex = false;
            try
            {

                byte[] pack = CombineBytes(lastPack, args.MessageBuf);

                RecMsgArgs targs = new RecMsgArgs();
                Debug.WriteLine("上次:" + BytesToHexString(lastPack));
                Debug.WriteLine("新接收:" + BytesToHexString(args.MessageBuf));
                Debug.WriteLine("组合包:" + BytesToHexString(pack));
                lastPack = pack;
                while (pack != null)
                {
                    pack = GetPackage(lastPack, out lastPack);
                    try
                    {
                        if (lastPack != null)
                        {
                            Debug.WriteLine("剩余包:" + BytesToHexString(lastPack));
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("空引用--" + e.Message);
                    }

                    Debug.WriteLine("完整包:" + BytesToHexString(pack));
                    if (pack == null)
                    {
                        return;
                    }
                    Debug.WriteLine("数据总长度:" + pack.Length.ToString());
                    byte p = (byte)pack[5];

                    UInt16 l = (UInt16)(((UInt16)pack[4]) * 256 + (UInt16)pack[3]);

                    Debug.WriteLine("数据有效字段长度:" + l.ToString());

                    byte[] data = GetBytes(pack, 6, l);

                    switch (p)
                    {
                        case (byte)PackDes.LL_Status:
                            targs.RawData = args.Message;
                            targs.DataType = typeof(LL_Status);
                            targs.Data = StructBytesConvertor.BytesToStruts(data, typeof(LL_Status));
                            SetRecEvent(targs);
                            break;
                        case (byte)PackDes.IMU_CalcData:
                            targs.RawData = args.Message;

                            targs.DataType = typeof(IMU_CalcData);
                            byte[] tmp = new byte[4];
                            for (int k = 0; k < 4; k++)
                            {
                                tmp[k] = data[k + 76];
                            }
                            /*
                            byte tmp = data[76];
                            data[76] = data[78];
                            data[78] = tmp;
                            tmp = data[77];
                            data[77] = data[79];
                            data[79] = tmp;*/
                            targs.Data = StructBytesConvertor.BytesToStruts(data, typeof(IMU_CalcData));
                            SetRecEvent(targs);
                            break;

                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("接收线程异常:" + e.Message);
            }
            finally
            {
                mutex = true;
            }
        }