0

I have a system capable of sending hex commands as is, meaning that if I send A0 02 50 0E the receiver should receive A0 02 50 0E as a hex. It is now working fine, thanks to StackOverlfow.

My problem now is to receive hex data and convert it as string equivalent of that hex, it means that if I receive the hex A0 03 82 04 D7 my system can receive it as string of A0 03 82 04 D7.

This is my code: In this example I send a hex value of A0 03 82 04 D7

Dim s As String = ""
s += CStr(ComPort.ReadExisting)
'Just to check the output
Console.WriteLine("s: " & s) 'Outputs "s: ?P" instead of "s: P", this is my first problem

Dim result As String = Nothing
Dim c As Char = Nothing
For Each c In s
   Console.WriteLine("haha: " & c)

   Dim asd As String = Convert.ToUInt16(c).ToString("D2")


   Console.WriteLine("Uint16: " & Convert.ToUInt16(c).ToString("D2"))
   Console.WriteLine("Uint32: " & Convert.ToUInt32(c).ToString("D2"))
   Console.WriteLine("Uint64: " & Convert.ToUInt64(c).ToString("D2"))
   Console.WriteLine("Byte: " & Convert.ToByte(c).ToString("D2"))
   Console.WriteLine("Hex: " & c)

   result &= asd
Next
message_command(msg_usage_enum.serial_received_ascii, result)
Console.WriteLine(result) 'Output is 63028014

The result of the loop

haha: ?
Uint16: 63
Uint32: 63
Uint64: 63
Byte: 63
Hex: ?
haha: 
Uint16: 02
Uint32: 02
Uint64: 02
Byte: 02
Hex: 
haha: P
Uint16: 80
Uint32: 80
Uint64: 80
Byte: 80
Hex: P
haha: 
Uint16: 14
Uint32: 14
Uint64: 14
Byte: 14
Hex: 

The last code, which is Console.WriteLine(result) 'Output is 63028014 is partially correct, 028014 is the correct decimal value the only problem is the first 2 digits which is 63.

My approach here is to convert it first to decimal value, then from decimal to it's hex equivalent then make it string, but I'am not sure if it will work.

Edit: Thanks to Ian I am now having progress.

It's working now, Im just having a problem.Whenever I send A0038204D7 then my system receives 3F 03 3F 04 3F which is partially wrong 3F should be A0, 82 and D7, while sending A002500E my system receives 3F 02 50 0E which is correct except for 3F. It seems that my system can't process other so he outputs 3F.

This is the latest code I have so far

Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(ComPort.ReadExisting())

Dim strs As String() = (From b In bytes
                                        Select b.ToString("X2")).ToArray()
Dim str As String = String.Join(" ", strs)

Console.WriteLine("Result " & str)

This is the output in console.writeline:

Result 3F 02 50 0E
Community
  • 1
  • 1
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60

1 Answers1

1

Use ToString("X2") for every byte you receive. You can use LINQ to do that, and String.Join to combine them back:

Dim bytes As Byte() = {&HA0, &H3, &H82, &H4, &HD7}
Dim strs As String() = (From b In bytes
                    Select b.ToString("X2")).ToArray()
Dim str As String = String.Join(" ", strs)
'Your result is in str
Ian
  • 30,182
  • 19
  • 69
  • 107
  • I am not familiar with LINQ is it possible to avoid using LINQ? – Cary Bondoc Feb 26 '16 at 05:02
  • Ah yes, thanks! it is working fine. I'm just having a trouble on how can I merge my code `s += ComPort.ReadExisting()` to your code `Dim bytes As Byte() = {&HA0, &H3, &H82, &H4, &HD7}` so every hex data that my system receive will pass through your code – Cary Bondoc Feb 26 '16 at 05:10
  • I see... mine is just showing example of `Byte Array`. In your `For Each c In s` you could possibly use `Convert.ToByte()` rather than `Convert.ToInt16()` and the others. This way, you get each `c` in `s` as `Byte`. But using `Convert.ToInt16()` is also **okay**. The key here is to use `ToString("X2")` actually. LINQ above is just for simplification of `For Each` loop that you have done. Basically you have done the same using `For Each` loop – Ian Feb 26 '16 at 05:37
  • Thanks Ian, please see the edit in my question (in the bottom part) I really feel that I am near on making this code work, thank you so much for your assistance. – Cary Bondoc Feb 26 '16 at 05:46
  • 1
    @CaryBondoc I just read your updated question: isn't `A002500E` and `A002500E` the same? Also, when you say okay... does it mean you print correctly? I mean, how do you check that you receive: `3F 03 3F 04 3F`? From the debugger? from the `Console.Writeline`? – Ian Feb 26 '16 at 05:58
  • My fault, I've edit the bottom part of my question to make it clear. Yeah, I say ok because it outputs partially correct. Please see my edited question – Cary Bondoc Feb 26 '16 at 06:10
  • Do you use Visual Studio? If you do, I am interested to know what do you see in the debugger for `hexByte As String` since you wrote it like this `For Each hexByte As String In s` while `s` itself is of unknown data type to me. What is the data type for `s`? Can you check for me in your debugger? – Ian Feb 26 '16 at 06:14
  • Yes, I am using Visual Studio 2015 (vb.net). Please see my edited question sir. Thank you so much. – Cary Bondoc Feb 26 '16 at 06:20
  • By the way I am using `For Each hexByte As Char In s` not string, because it outputs error ` Input string was not in a correct format.` if I use `As String` – Cary Bondoc Feb 26 '16 at 06:23
  • No, no, *don't* print it using `Console.WriteLine` - it is not going to help. Just use your debugger to check what is the data type and the value of `s` and of `hexByte` – Ian Feb 26 '16 at 06:23
  • Oh, sorry. Wait sir. – Cary Bondoc Feb 26 '16 at 06:23
  • I'm having a hard time on finding a tutorial on how to use debugger sir. :( It's because I am not aware of it. – Cary Bondoc Feb 26 '16 at 06:31
  • @CaryBondoc some basics debugging: https://www.youtube.com/watch?v=C0vDKXIq_9A, https://www.youtube.com/watch?v=eEJ5xKO7c3o – Ian Feb 26 '16 at 06:32
  • hexbyte value is `"?"c` while s value is `"?" & ChrW(2) & "P" & ChrW(14)`. Is that what you are looking for sir? – Cary Bondoc Feb 26 '16 at 06:52
  • The values are shown in string type, are they? How about the `data type`? especially `s`. What data type is `s`? You mention that `For Each hexByte As Char In s` gives you error which is interesting. Actually that gives a clue of what could possibly be wrong. Likely, your `s` is of wrong data type to be put in the first place. I wonder what is the return for `ComPort.ReadExisting` in the first place? – Ian Feb 26 '16 at 06:57
  • If your system, by `ComPort.ReadExisting` already gives you `Byte()` for example, then you do not need to Convert it to `String` to Convert it back to byte again. Just use it to get `Byte` and convert each `Byte` with `ToString("X2")` – Ian Feb 26 '16 at 06:59
  • Oh sorry, my fault what I mean is I'm using `For Each hexByte As Char In s` and it is working fine, if I use `For Each hexByte As String In s` it gives an error. hexbyte dataType is `Char` while `s` dataType is `String` – Cary Bondoc Feb 26 '16 at 07:00
  • Yeah, `ComPort.ReadExisting` should give me `Byte()`. I've tried `Dim bytes As Byte() = ComPort.ReadExisting()` then it says `Value of type String cannot be converted to Byte` that's the reason why I converted it to string. – Cary Bondoc Feb 26 '16 at 07:09
  • Cary, really, - look - I cannot help you more than this... The thing is, the best person to check the problem apart from the conversion which I have shown you is yourself - since you have a full access to the program as well as the debugger while I cannot recreate all possible problems in my PC here. So, please check why the ComPort.ReadExisting reads `string` instead of `Byte()` if you are sure that it has to read `Byte()` actually - you have all the files and IDE needed to do that but I have not. Here, I only try to give you direction. – Ian Feb 26 '16 at 07:12
  • Thus, what I show you is that **if** you read `Byte()`, then you should use my method and it should work. As for **why** does it **not** read as `Byte()` **I cannot** tell why - I do not use `ComPort.ReadExisting` or having access to all your programs. And that's why I suggest you to use your debugger to check on the data type. Please understand this. – Ian Feb 26 '16 at 07:14
  • Ok, thank you for your guidance I'll check other possibilities regarding that. Thank you so much sir. You unstuck me in this problem. – Cary Bondoc Feb 26 '16 at 07:16
  • @CaryBondoc no problem, you are welcome. ;) hope you can find out why it is not `Byte()` as you expected. Once you know it, you can use my method. – Ian Feb 26 '16 at 07:45
  • Hi Sir, I've got an updated code in my question it's all thanks to you. The only problem that I got is any value larger than `hex 3F` or `decimal 63` doesnt seem to output correctly, I guess it's a whole new question. :) Thanks. – Cary Bondoc Feb 26 '16 at 07:54
  • 1
    @CaryBondoc ah, I see... that's why that happen... so no problem with the conversion at all, but you got to check why it cannot output something greater than 6-bit `111111 = 63`. Maybe, that 6-bit itself is a clue. Check it out! – Ian Feb 26 '16 at 07:56