比如 串口有时候并不会一次性把你想要的数据全部传输给你,可能会分为1次,2次,3次分别传送一部分数据给你,这时候一般会设置字符串的结束符来判定是否传输完毕(一般设置为\n ,\r)

 传输过程中字符串的后面都会跟着无数个\0\0\0;经过同事测试,每次都从串口缓存中取出所有的传输数据,然后再进行判断,可以获取完整的字符串;代码如下:

          //读取完整字符串 硬件扫描枪设置传输结束符为\r

            byte[] readBuffer = new byte[serialPortScan.BytesToRead];
            tempCode += serialPortScan.ReadExisting().Trim( '\0' );

            if ( tempCode.IndexOf( '\r' ) > 0 )
            {
                singleCode = tempCode.Replace( REPLACE_URL, "" ).Trim( '\r' );
                serialPortScan.DiscardInBuffer();
                tempCode = string.Empty;
            }
            else
            {
                return;
            }
View Code

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2022-02-08
  • 2021-07-11
  • 2022-12-23
  • 2021-12-21
猜你喜欢
  • 2022-02-10
  • 2022-02-08
  • 2021-06-30
  • 2022-12-23
  • 2022-02-08
  • 2022-02-08
相关资源
相似解决方案