0
votes

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?

1
You need an application layer that send/receive two type messages and puts a header on each message so receive end knows the type of message being received.jdweng
Thanks ! Can you please give me an example code or any reference link to achieve that ? i am newbie so does not know how to achieve that.Pavan Kumar
this is covered by most Arduino serial tutorials. you need to send standardized messages. simple example: send "on" to turn the motor on, "off" to turn it off and "?" to make Arduino send the value. check the received messages and once you recognize a command do something. you can add errors for wrong commands, checksums, terminators whatever... google serial protocol and ASCII command interface. you'll find plenty of examplesPiglet

1 Answers

0
votes

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.