1

I receive bytecode and try to interpret it. Since this is a communication between two devices using the HART protocol (primary/secondary), I wanted to sort the bytes first.

A request which starts with 82 and the answer which starts with 86. Between them can be any number of FFs (Depending on how many come through), which serve as preambles.

Here is an example output:

82 05 C6 00 46 5D 03 00 59  FF FF FF FF FF 86 05 C6 00 46 5D 03 1A 00 00 41 4C BE B2 3B 40 F6 4D B7 24 C2 23 B2 40 20 41 B5 BA 3D AA 7F A0 00 00 20 FF FF FF FF 
82 05 C6 00 46 5D 03 00 59     FF FF FF FF 86 05 C6 00 46 5D 03 1A 00 00 41 4C BE D5 3B 40 F6 4D F5 24 C2 23 B2 40 20 41 B5 63 04 AA 7F A0 00 00 E5 FF FF FF FF 
82 05 C6 00 46 5D 03 00 59  FF FF FF FF FF 86 05 C6 00 46 5D 03 1A 00 00 41 4C BE F6 3B 40 F6 4E 2E 24 C2 23 B2 40 20 41 B5 76 E7 AA 7F A0 00 00 E8 FF FF FF FF 
82 05 C6 00 46 5D 03 00 59  FF FF FF FF FF 86 05 C6 00 46 5D 03 1A 00 00 41 4C BF 01 3B 40 F6 4E 41 24 C2 23 B2 40 20 41 B5 67 34 AA 7F A0 00 00 B3 FF FF FF FF

Here is my attempt to separate the requests and responses:

string[] delimiterChars = {
    "FF FF FF FF FF FF FF FF FF FF ",
    "FF FF FF FF FF FF FF FF FF ",
    "FF FF FF FF FF FF FF FF ",
    "FF FF FF FF FF FF FF ",
    "FF FF FF FF FF FF ",
    "FF FF FF FF FF ",
    "FF FF FF FF ",
    "FF FF FF ",
    "FF FF ",
    "FF "
};

string[] words = OnlyHex.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);

This works fine, but unfortunately there can be FFs in the messages, which my code does not catch.

I could add these to each string array entry of delimiterChars but I don't think that would be the prettiest solution and would make it impossible to separate the request from the response as the numbers would then be missing.

How could I make it so that the string is only spearated, if the following part starts with an 82 or 86? The 82 and 86 should be still in the split string. The FFs do not have to.

Probably the solution is Regex, but unfortunately I'm not a pro at it.

7
  • What do you mean by "there can be FFs in the messages, which my code does not catch"? Commented Dec 9, 2022 at 14:32
  • I would work with byte, but in any case I suggest you do some functional work in paper and the translate to code, test it and come with a question more clean Commented Dec 9, 2022 at 14:34
  • @Gec It can happen that FFs apear inside of the request or response and not only as the preambel. With my attempt, it would split the string at a wrong position. Commented Dec 9, 2022 at 14:42
  • @LeandroBardelli yes that would be ideal. This is my first time working with bytes in this kind of sense. I thought using regex and splitting the string would work out Commented Dec 9, 2022 at 14:44
  • 1
    Not every sequence of bytes is “bytecode”. Besides that, processing a sequence of bytes by performing string manipulations on a hexdump is neither straight-forward nor efficient. I think, that’s what Leandro Bardelli meant. Instead of using strings, interpret the bytes according to the protocol, you apparently already know. Commented Dec 9, 2022 at 17:06

1 Answer 1

2

You can certainly use regex for this. For example:

var match = Regex.Match(input[0], "(?<request>82.*)(?<answer>86.*)");
var request = match.Groups["request"].Value; // empty string for invalid input
var answer = match.Groups["answer"].Value;   // empty string for invalid input

That said, storing this type of data as strings is a bit unusual. A byte[] would be a more appropriate representation, but at that point regex is no longer an option.

If you plan to use the answer and response later on, your scenario may be a good candidate for a parser combinators. Libraries such as Sprache or Superpower can help you build one without much overhead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.