-1

I am trying to develop Windows application for Modbus RTU mode (RS-485) sensor in C#.

While reading sensor data there is no problem but main problem is when I try to read version of sensor the result is showing in:

01041A4350532D524D2056312E303020323031383033323900000000007B00

But I need to show the result is like

CPS-RM V1.00 20180329

I searched for this in internet I think I should have to convert to ascii code but I am not finding any solution do you have any idea for this.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
niksan karkee
  • 157
  • 1
  • 17

2 Answers2

1

It looks like only part of the string is actually text. I suspect the third byte is the number of bytes to treat as text following it (so the final two bytes aren't part of the text). Note that it's padded with Unicode NUL characters (U+0000) that you may want to trim.

So if you have your data in a variable called bytes:

string text = Encoding.ASCII
    // Decode from the 4th byte, using the 3rd byte as the length
    .GetString(bytes, index: 3, count: bytes[2])
    // Trim any trailing U+0000 characters
    .TrimEnd('\0');
Console.WriteLine(text);

I would mention that that's based on guesswork though. I would strongly advise you to try to find a specification for the data format to check my assumption about the use of the third byte as a length.

If you haven't already got the data as bytes (instead having it in hex) I would suggest you convert it to a byte array first. There are lots of pieces of code on Stack Overflow to do that already, e.g. here and here.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

I found a answer and it worked

 public static string ConvertHex(String hexString)
        {
            try
            {
                string ascii = string.Empty;
                for (int i = 0; i < hexString.Length; i += 2)
                {
                    String hs = string.Empty;

                    hs = hexString.Substring(i, 2);
                    uint decval = System.Convert.ToUInt32(hs, 16);
                    char character = System.Convert.ToChar(decval);
                    ascii += character;
                }
                return ascii;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return string.Empty;
        }
niksan karkee
  • 157
  • 1
  • 17
  • You said in the comments yesterday that you already had the data as bytes, not just hex. That's why I didn't include any hex conversion in my answer. I would advise you to separate out "convert hex to binary" and "use the binary data to extract a string" as different operations. (And avoid the kind of repeated string concatenation you've got here - and exception handling that is just reporting an error and continuing as if it didn't happen.) – Jon Skeet Jun 06 '18 at 08:16
  • @DaisyShipton sorry that's my fault. – niksan karkee Jun 07 '18 at 00:41
  • I've added references in my answer to other questions about hex parsing. Note that the code you've got here will have additional spurious characters at the beginning and end that aren't part of the string "CPS-RM V1.00 20180329" - that's why my answer starts at a specific point, only decoding a specific chunk of the data. – Jon Skeet Jun 07 '18 at 05:19