Main Site Documentation

Message class, need help


#1

Hi guys,

I could use some coding on the following:

I want to pass messages between a pc, cobra and panda. The pc and cobra are both networked, while the panda is serially connected to the cobra. I’m trying to pass messages amongst them, but I’m a bit stuck on my message class.

The Messages are constructed in the following way:

SenderID|TargetID|Command|NumParams|(optional)Params\r\n

where eventual Params are separated by a | (pipe).

Examples:


// from PC to COBRA
PC|COBRA|getdatetime|0\r\n

// Cobra replies with:
COBRA|PC|datetime|1|24/05/2011 13:50:15\r\n

// from PC to PANDA
PC|PANDA|getcounters|0\r\n

// COBRA looks at the TargetID and forwards the message to the PANDA
PC|PANDA|getcounters|0\r\n

// PANDA replies to COBRA
PANDA|PC|counters|3|123|456|789\r\n

// Cobra looks at TargetID and forwards the message to the PC
PANDA|PC|counters|3|123|456|789\r\n


All messages passes at least the Cobra, and the Cobra decides what happens with the messages.

So at some point on the COBRA I need to decide what transport (serial or tcp) to use to send the message. And this is where I’m having some problems.

I have a class called Target:


  class Target
  {
    public const string PANDA = "PANDA";
    public const string COBRA = "COBRA";
    public const string PC = "PC";
  }

And another class Command:


  class Command
  {
    public const string SetDateTime       = "setdatetime";
    public const string GetDateTime       = "getdatetime";
    public const string SetCounters       = "setcounters";
    public const string GetCounters       = "getcounters";
  }

And a class Transport:


  class Transport
  {
    public const string SER = "COM";
    public const string NET = "NET";
  }

The Message Class:


  class Message
  {
    const string sep = "|";
    const string end = "\r\n";

    public static string SenderID; //should be replaced with Target class
    public static string TargetID; //should be replaced with Target class
    public static string Command; //should be replaced with Command class
    public static int ParamsCount;
    public static string[] Params;

    public override string ToString()
    {
      string ts = string.Empty;
      if (ParamsCount == 0)
        ts = SenderID + sep + TargetID + sep + Command + sep + ParamsCount + end;
      else
      {
        ts = SenderID + sep + TargetID + sep + Command + sep + ParamsCount + sep;
        for (int x = 0; x < ParamsCount; x++)
        {
          ts += Params[x] + (x < ParamsCount ? "|" : "");
        }
        ts += end;
      }
      return ts;
    }
  }

Now I would like to have a ProcessMessage class that I can return to my Main that contains a field with Transport and a field with the Message.

The main purpose is that I can use these classes on all 3 systems (PC, COBRA, PANDA).

Can somebody give me some help? Or provide me with a cleaner/nicer/leaner sollution?

Greetings,
Eric


#2

How are you with XML? I would suggest moving away from delimited strings in favor of a simple xml schema to define your parameters. This is very straightforward on the PC side. Parsing XML in netmf is more tedious, but not any more technically challenging than parsing delimited strings. The only downside is that on devices such as a Panda, you have to keep track of available memory. If you have a bunch of data to send at once, you can chew it up quickly (the metadata can actually be larger than the data itself from a raw byte count perspective). If your data doesn’t get any more complicated than what you posted, you should be fine.