1
votes

I'm reading 12 bytes from a serial port at a time using the read method. 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 .

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. 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. Does anyone have any advice for me as to what I'm doing wrong when I reassemble these?

1
what about the ReadAllBytes Method aslo can't see where you are using the read method.. are you missing relevant code ..?MethodMan
I added the read method code. Thanks for pointing out that I didn't have that to begin with. I decided to use the read method instead of the readbyte method because I needed to treat the 11th and 12th byte differently than the other 10.leidholmt
Post the contents of the com buffer (a list of the hex values of the 12 bytes), and also post the expected results.Jim Mischel

1 Answers

0
votes

Use BitConverter to convert from byte array to other types. Something along the lines of

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