Question

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?

Était-ce utile?

La solution

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);
       ....
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top