0

I am receiving data from a cnc machine every 5 seconds. Length of the data is 66 bytes. And every two byte has a special meaning according to the guide that I have. The device sends the data over socket to a specific ip and port. I have been told that I should read the data as hex instead of ascii.

This line of code returns

        string data = Encoding.ASCII.GetString(data.buffer,0,66);

this;

"\0\u0004\0\u0001\0\0\0\0\0\0\0\0\0\0\0\0\0\r\0\r\0\0\0\0\0\0:a\u0002@\0?\0`\u001b?\u0015U\0\0\0\0\u0001\u0010\0\u0018\0\0\u000f\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0u/"

and of course it is not useful to me.

I did tried to convert byte array to the hex string with that code;

  StringBuilder sb = new StringBuilder();
                foreach (byte b in buffer)
                    sb.Append(b.ToString("X2"));

                string hexString = sb.ToString();

And got result as

00040001000000000000000000020000000000000000000000003A9D023F00A000601B841555000000000110001800000F070000000000000000000000000000752F

And when I try to convert this result as string, no success, nothing meaningfull.

GOAL

What I am trying to achieve is, read the incoming socket data as hex and use every two byte as a word to match a value. For example first 2 byte should match either 0 or 1. With i have it returns ? (question mark)

Thank you.

MonkeyDLuffy
  • 556
  • 7
  • 17
  • What's wrong with the data that you received. You received 66 bytes. That string (`"00040001..."`) is 132 hex characters (i.e., 66 bytes). It looks like it might be correct (the first 16 bits could be a `short` 4, the next, a `short` 1. The last two bytes (just after all those zeros) look like something like a checksum. We don't know what your NC machine's data is supposed to look like, but that looks far from garbage. What do you mean by _"convert this result as string"_ - it is already a string? – Flydog57 Oct 29 '21 at 00:45
  • It sounds like maybe the cnc machine is sending an actual hex-encoded string, where every 2 hex characters represents 1 byte of data. If that is the case, then this is a duplicate of [How can I convert a hex string to a byte array?](https://stackoverflow.com/questions/321370/). It would help if you would show the actual data you are receiving, and *quote* the actual text of the programming guide you are using. – Remy Lebeau Oct 29 '21 at 05:21
  • As a test, take your code, and let it run, so it picks up a new packet every 5 seconds. Format the data as the 132 digit hex string you show and write it to the console (or a file, or...). You should see data in the same format, and likely very similar data - perhaps with one number increasing with each packet (I'm guessing here). Every time anything changes, the last four digits are likely to change a lot. If that's what you are seeing, then you are probably doing this correctly. – Flydog57 Oct 29 '21 at 16:08

1 Answers1

2

I have been told that I should read the data as hex instead of ascii

My gut feeling is this statement has been misquoted or misunderstood. There is no value in processing binary data as string hex representation just as there is no value in converting it to ascii... The only sane way to process binary data, is in binary unless you have a meaningful way to convert it.

You mention you need word (2byte) groupings, you could just convert this to an array of short, or ushort depending on your needs

var bytes = new byte[66];

var shortArray = new short[bytes.Length / 2];
Buffer.BlockCopy(bytes, 0, shortArray, 0, bytes.Length);

or

for (int i = 0; i < shortArray.Length; i++)
   shortArray[i] = BitConverter.ToInt16(bytes[(i*2)..(i*2+2)]);

Disclaimer : This is just an example, be very careful of the endianess of your data, there are other ways to do this

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • there's a high possibility that there is a misunderstanding between me and the other developer guy but this is what I've been instructed to. He also said that the first two byte when converted to a string either could be 1 or 0 which means the device is on or off. I couldn't even get there yet :/ Btw. thanks for your help. – MonkeyDLuffy Oct 28 '21 at 23:51
  • @MonkeyDLuffy Note, `shortArray[0] == 0` in your example `shortArray[1] == 4` if you get my drift, this may be helpful, however my guess its better to get the actual specs of the data stream – TheGeneral Oct 28 '21 at 23:53
  • I absolutely get what you meant with your code. From your example; shortArray[1] could be either 1 or 0 but I get 256. Like you said without correct explanation, it will be like chasing a tail. Thank you again. – MonkeyDLuffy Oct 29 '21 at 00:00
  • How do you get 256. The hex string starts with `"00040001"`. There are four hex characters in a 16-bit `short`. Depending on _endian-ness_, that could easily be a `4` followed by a `1`. When you are working with this kind of data, you need a complete description of what the data means (endian-ness, data lengths (it's not unusual for a packet like that to have a couple of shorts, a handful of bytes and a 32-bit quantity or two squished together)). Otherwise, you are guessing. However, as I mentioned above, it doesn't look like garbage. – Flydog57 Oct 29 '21 at 00:50