Identification of packets in a byte stream

后端 未结 2 1576
感动是毒
感动是毒 2020-11-27 23:07

I\'m having a bit of a problem with the communication to an accelerometer sensor. The sensor puts out about 8000 readings/second continuously. The sensor is plugged in to a

相关标签:
2条回答
  • 2020-11-27 23:35

    What you need to do is get some free SerialPortTerminal in c# import in your project and first check all the data and packets you are getting, unless you have already done that. Than just to read you will need to do something like...

       using System;
       using System.IO.Ports;
       using System.Windows.Forms;
    
       namespace SPE
       {
         class SerialPortProgram
         {
           // Create the serial port with basic settings
           private SerialPort port = new SerialPort("COM4",      9600, Parity.None, 8, StopBits.One);
    
           [STAThread]
           static void Main(string[] args)
           { 
             // Instatiate this class
             new SerialPortProgram();
           }
    
           private SerialPortProgram()
           {
             Console.WriteLine("Incoming Data:");
    
             // Attach a method to be called when there      // is data waiting in the port's buffer
             port.DataReceived += new         SerialDataReceivedEventHandler(port_DataReceived);
    
             // Begin communications
             port.Open();
    
             // Enter an application loop to keep this thread alive
             Application.Run();
           }
    
           private void port_DataReceived(object sender,      SerialDataReceivedEventArgs e)
           {
             // Show all the incoming data in the port's buffer
             Console.WriteLine(port.ReadExisting());
           }
         }
       }
    
    0 讨论(0)
  • 2020-11-27 23:53

    ... can't really grasp how the checksum can be used as an identifier for the start of the package (wouldn't the checksum change all the time?).

    Yes, the checksum would change since it is derived from the data.
    But even a fixed-value start-of-packet nibble would (by itself) not be sufficient to (initially) identify (or verify) data packets. Since this is binary data (rather than text), the data can take on the same value as any fixed-value start-of-packet. If you had a trivial scan for this start-nibble, that algorithm could easily misidentify a data nibble as the start-nibble.

    Is this a common way for identifying the start of a packet?

    No, but given the high data rate, it seems to be a scheme to minimize the packet size.

    Does anyone have any idea how to solve this problem?

    You probably have to initially scan every sequence of bytes five at a time (i.e. the length of a packet frame).
    Calculate the checksum of this frame, and compare it to the first nibble.
    A match indicates that you (may) have frame alignment.
    A mismatch means that you should toss the first byte, and test the next possible packet frame that would start with what was the second byte (i.e. shift the 4 remaining bytes and append a new 5th byte).

    Once frame alignment has been achieved (or assumed), you need to continually verify the checksum of every packet in order to confirm data integrity and ensure frame alignment. Any checksum error should force another hunt for correct frame alignment (starting at the 2nd byte of the current packet).

    0 讨论(0)
提交回复
热议问题