简体   繁体   中英

How to log the particular variable data which comes randomly at serial com port in C# from Arduino

I am working on a project in which I have set up a serial communication between Arduino and C# window form application. When I send a start command to Arduino, Arduino starts rotating the servo motor attached to it and sends the angle position to c# in a continuous manner.

Now while Arduino is sending the data to C#, I request a variable data stored in Arduino at a random time at the same serial port and I want to log that variable data in my c#.i can log whatever data is being received on serial port but I can't select and extract that particular variable.

Can anyone please help me with that?

I'll just lay this out here. Picking up where jdweng left off, your messages also need to contain messaging protocol state information. Once this information has been included, you will be able to de-serialize any message in any order because the information required to process it is contained in the message.

Here is an example protocol.

Message -

  • MessageLength - 2 bytes
  • MessageType - 2 bytes
  • MessageBody - n bytes

So your angle message would consist of three parts Length, Type, and Value

AngleMessage

  • MessageLength = 6
  • MessageType = (Int16)MessageTypes.Angle
  • MessageBody = (Int16)0-359

ParameterMessage

  • MessageLength = 10 + size of value
  • MessageType = (Int16)MessageTypes.Parameter
  • MessageBody = {

    • (Int16)ParameterOperation.Updated,
    • (Int16)ParameterNumber,
    • (Int16)ParameterType,
    • (Byte[])ParameterValue

    }

    Using this scheme your message contains all the information you need to process it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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