简体   繁体   English

从串口读取带符号整数

[英]Reading signed integers from serial port

I'm reading 12 bytes from a serial port at a time using the read method. 我使用read方法一次从一个串行端口读取12个字节。 I'm receiving the data in the form of bytes correctly. 我正在正确接收字节形式的数据。 However i'm trying to convert these bytes to signed integers once I have them. 但是,一旦我拥有这些字节,我将尝试将这些字节转换为带符号的整数。 The order of the bytes is (least significant byte 1) , (most significant byte 1) ,(least significant byte 2) , (most significant byte 2) etc. Then i'm trying to reassemble them using this for each . 字节的顺序是(最低有效字节1),(最高有效字节1),(最低有效字节2),(最高有效字节2)等。然后,我尝试对每个字节使用它们进行重组。

while ((intErg = serialPort1.Read(comBuffer, 0, comBuffer.Length)) > 0)
      {
          string strErg2 = "";          
          double vbatmsb = 0;
          double vbatlsb = 0;

          short imsb = 0;

          double vbattotal = 0.0;



          for (int i = 0; i < comBuffer.Length; i++)
          {

              switch (i)
              {
                  case 0:
                       imsb = (short)(comBuffer[i+1] << 8 + comBuffer[i]);
                       strErg2 += imsb.ToString()+ ", ";
                      break;                
                  case 2:
                       imsb = (short)(comBuffer[i+1] << 8 + comBuffer[i]);
                       strErg2 += imsb.ToString()+ ", ";
                      break;
                  case 4:
                       imsb = (short)(comBuffer[i+1] << 8 + comBuffer[i]);
                       strErg2 += imsb.ToString()+ ", ";
                      break;
                  case 6:
                      imsb = (short)(comBuffer[i] << 8 + comBuffer[i + 1]);
                       strErg2 += imsb.ToString()+ ", ";
                      break;                    
                  case 8:
                       imsb = (short)(comBuffer[i+1] << 8 + comBuffer[i]);
                       strErg2 += imsb.ToString()+ ", ";
                      break;

                  case 10:
                      vbatlsb = comBuffer[i];
                      break;
                  case 11:
                      strErg2 += ", ";
                      vbatmsb = comBuffer[i];
                      vbattotal = ((vbatmsb * 256.0 + vbatlsb) * 6.6) / 4095.0;
                      strErg2 += vbattotal.ToString();
                      break;

              }



          }

          this.BeginInvoke(new EventHandler(delegate
          {

              SetTheText(strErg2);

          }));

      }

The comBuffer is a byte array that is filled by the serialPort1.Read command. comBuffer是由serialPort1.Read命令填充的字节数组。 The last 2 bytes are treated differently because they are unsigned integers and are correct. 后两个字节的处理方式有所不同,因为它们是无符号整数并且是正确的。 However the first 10 bytes that I try to reassemble into signed integers are not even close to the values they should be. 但是,我尝试将其重组为带符号整数的前10个字节与它们应有的值甚至都不接近。 Does anyone have any advice for me as to what I'm doing wrong when I reassemble these? 当我重新组装这些时,有人对我有什么建议吗?

Use BitConverter to convert from byte array to other types. 使用BitConverter从字节数组转换为其他类型。 Something along the lines of 遵循以下原则

while....
{
  if (comBufferLength >= 2)
  {
     imsb1 = BitConverter.ToInt16(comBuffer, 0);
     if (comBufferLength >= 4)
       imsb2 = BitConverter.ToInt16(comBuffer, 2);
       ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM