zoukankan      html  css  js  c++  java
  • RS232通讯数据解析C#

     #region【方法:解析数据线程】
            private void DataParse()
            {
                int waittime = 0;//等待时间
                bool isExit = false;//是否超时退出
                byte[] headData = new byte[4];//命令头
                byte[] cmdData = new byte[19];//命令
                while (true)
                {
                    try
                    {
                        if (m_RevQueue.Count > headData.Length)
                        {
                            isExit = false;
                           
                                while (m_RevQueue.Count < 4)//小于命令头长度,进入等待状态,2s超时退出
                                {
                                    waittime++;
                                    if (waittime > 100)
                                    {
                                        isExit = true;
                                        break;
                                    }
                                    Thread.Sleep(1);
                                }
                            headData = new byte[4];//命令头
                            while (true)
                            {
                                while (m_RevQueue.Count < 1)//小于命令头长度,进入等待状态,2s超时退出
                                {
                                    waittime++;
                                    if (waittime > 100)
                                    {
                                        isExit = true;
                                        break;
                                    }
                                    Thread.Sleep(1);
                                }
    			//如果获取字符与头字符不一致则每位向后移一位
                                if (headData[0] != 0xAA|| headData[1] != 0x01 || headData[2] != 0x01 || headData[3] != 0x01 )
                                {
                                    headData[0] = headData[1];
                                    headData[1] = headData[2];
                                    headData[2] = headData[3];
                                    headData[3] = m_RevQueue.Dequeue();
                                    continue;
                                }
                                break;
                            }
                           
                            while (m_RevQueue.Count < 19)//小于命令长度,进入等待状态,2s超时退出
                            {
                                waittime++;
                                if (waittime > 100)
                                {
                                    isExit = true;
                                    break;
                                }
                                Thread.Sleep(1);
                            }
                            waittime = 0;
                            //Console.WriteLine(headData[3].ToString("X2"));
                            if (isExit) continue;
                            cmdData = new byte[23];
                           取出后面的19位
                            for (int i = 0; i < 19; i++)//取出命令
                            {
                                cmdData[i + 4] = m_RevQueue.Dequeue();
                            }
    		//把头字符复制到cmdData;
                            headData.CopyTo(cmdData, 0);
                            GCByteToModel(m_DeviceModel, cmdData);
                        }
    
                        else
                        {
                            Thread.Sleep(10);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            #endregion
    

      

  • 相关阅读:
    MSSQL错误1069解决方法
    Xamarin的Mono for Android目前可运行于MIPS上
    Oracle大数据机和连接器产品支持与Hadoop和Cloudera Manager集成
    百度1
    恒生电子
    IGT中国
    经典笔试题——a和&a有什么区别
    腾讯HTTP协议1
    腾讯http协议2
    面向对象的三大特征
  • 原文地址:https://www.cnblogs.com/yanranziruo/p/10429997.html
Copyright © 2011-2022 走看看