Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. SerialPort sp = (SerialPort)sender;
  2.  
  3.                 while (sp.BytesToRead !=  0)
  4.                 {
  5.                     byte startByte = (byte)sp.ReadByte();
  6.              
  7.                     if (startByte == 0xEE)
  8.                     {
  9.                        Console.WriteLine("Start of Message");
  10.  
  11.                         byte srcAdr = (byte)sp.ReadByte();
  12.  
  13.                         if (srcAdr < (int)32) // There is only 32 nodes available on CAN
  14.                         {
  15.                             byte length = (byte)sp.ReadByte();
  16.  
  17.                             if (length < (int)14) // Length will never exceed 14 bytes
  18.                             {
  19.                                 byte[] message = new byte[length - 3];
  20.  
  21.  
  22.  
  23.                                 sp.Read(message, 0, length - 3);
  24.  
  25.                                 for (int i = 0; i < message.Length; i++)
  26.                                 {
  27.                                     Console.Write(message[i].ToString("X2") + " - ");
  28.                                 }
  29.                                 Console.WriteLine();
  30.                             }
  31.                             else
  32.                             {
  33.                                 Console.WriteLine("Error: Length : " + length);
  34.                             }
  35.                         }
  36.                         else
  37.                         {
  38.                             Console.WriteLine("Error: Source address : " + srcAdr);
  39.                         }
  40.  
  41.                     }
  42.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement