• 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
    

      

  • 相关阅读:
    【GitHub】在Mac上配置/使用Github
    【IOS开发】《多线程编程指南》笔记
    【设计模式】二、观察者模式
    php 接受json数据时有转义字符处理办法
    highcharts 常用配置
    apache虚拟主机配置
    php,phpexcel插件导出excel使用
    json_decode转换json数据为数组出现的问题!
    redis 主从服务器
    linux 下安装redis
  • 原文地址:https://www.cnblogs.com/yanranziruo/p/10429997.html
Copyright © 2020-2023  润新知