Click here to Skip to main content
12,621,492 members (35,187 online)
Rate this:
Please Sign up or sign in to vote.
See more: Visual-Studio

I have a problem , i dont know how to write the correct way to the serial port Hex code's.

I have to write the following to the serialport.

&H02 &H00 &H09 &H35 &H32 &H08 &H99

I have a demo programm and there i can see what codes will be written to the serial port.

Send : 02 00 09 35 32 08 99
Answer : 06
Send : 05
Answer : 02 00 04 035 32 08 59 03 53

I have tried to do the following :


Dim code As String = ""
Dim receiveddata As Byte = 0

code = Chr(&H2) + Chr(&H0) + Chr(&H9) + Chr(&H35) + Chr(&H32) + Chr(&H8) + Chr(&H99)


receiveddata = SerialPort1.ReadByte() ' Normal i should receive 06 , i receive 21 so it is not correct.

I think that i send the wrong code this becaude of the chr(&Hxx) on the manual it says for example send 0x02 0x00 0x09 0x35 0x32 0x08 0x99
So i think that maybe my system is not correct.


Best regards

Posted 3-Aug-11 22:02pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It looks a though you are sending data and getting a response.
receiveddata = SerialPort1.ReadByte()
You received a 21, this is Ascii decimal version of NAK (Hex 15). This indicates the device does not understand what you are sending it. To receive the reply as a hex value you can do this:
receiveddata = Hex(SerialPort1.ReadByte())
For sending a data string to the device I would use:


WriteLine appends a line feed to the string, so maybe not so good to use.
Send each one out individually using Write.

I would check on the 0x99 code, that is part of the extended ascii format and when I send that out the comport it reads something different all the rest look OK. Check the device manual again .
Didier Cauberghe 4-Aug-11 15:32pm

Thanks for your answer.

Can you give me a tip how i need to send the codes like 0x02 0x00 0x09 0x35 0x32 0x08 0x99

Is use chr(&h02) + chr(&H0)+ ........ but i am not sure that this is the correct way.



S Houghtelin 4-Aug-11 15:42pm
I think the string "code" you have should work, use WriteLine instead of Write.
Like this:
code = Chr(&H2) + Chr(&H0) + Chr(&H9) + Chr(&H35) + Chr(&H32) + Chr(&H8) + Chr(&H99)

S Houghtelin 4-Aug-11 16:05pm
See my updated answer.
Didier Cauberghe 4-Aug-11 16:13pm

This is also not working.

What is strange when i set a breakline and go with the cursor on the code value the numbers are not visual there are strange signs " 52™œlœhˆ" instead of 02 00 09 35 32 08 ......

Can this be the problem ?

Thanks for your advise.


S Houghtelin 4-Aug-11 17:42pm
That is because they are ascii characters and the display doesn't have a correct symbol.

Look up ASCII table on google and you will see what I mean.

02 = STX = Start of text

00 = Nul = Null

09 = TAB - Horizontal tab

And so on.

Additionally look up sysinternals and download the portmon utility. This will allow you to monitor the comm port in action.

Good luck.

If you find this information helpful please vote on the rating stars by the answer. Thank you.
Didier Cauberghe 5-Aug-11 2:54am

Thanks, now i can see what is going out.

But i have seen that the code that i need to give , there it seems to be the problem the code is staring with &H99 &H9C .....
I use also chr(&H99) + chr(&H9C)

When i check the serial port monitor there are other codes 3F ,3F instead of &H99 and &H9C in the code i send Chr(&H99) + chr(&H9C) but they are translated do 3F 3F so i think that is the reason why the RFID is not responding like it should be .

The only question is why is it changing the codes

Do you have a suggestion.



S Houghtelin 5-Aug-11 11:44am
As I said, those are a part of the extended ASCII set and are not being interpreted correctcly. This from MSDN.

By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater then 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.

Just because it is another case of MS improving my life that pisses me off, I have to figure this out because it part of my job. I'll get back to you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

OK, Here it is, damn what pain. Anyway the default decoding only supports 128 character so value above 127 will be displayed as a "?" or 3F.\

To change this the SerialPort encoding needs to be changed. (See below)

'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Encoding = System.Text.Encoding.GetEncoding(28605)

'Then for your output array
'(Note I am ussing an array here this will only work with integers)
Dim arCommand(6) As Int16
 arCommand(0) = &H2
 arCommand(1) = &H0
 arCommand(2) = &H9
 arCommand(3) = &H35
 arCommand(4) = &H32
 arCommand(5) = &H8
 arCommand(6) = &H99
Dim x As Int16 'This one and the array should be declared at the class level

For x = 0 To 6
For a complete list of encoding capabilities see this MSDN link.[^]

Alright Didier, Vote this one a 5 please.

Didier Cauberghe 6-Aug-11 7:20am
Hello, Thanks for your support. That was the solution to the wrong characters send to the RFID device, now the returned values are like they are in the technical description. Maybe just one more question , what do i need to do for reading based on the same system (above 127) Can i do just I dont know all the values that come back , maybe they are also above 127 Many thanks Best regards Didier
S Houghtelin 7-Aug-11 19:37pm
receiveddata = SerialPort1.ReadByte() should do the trick. By now you have read the ascii table and can use the decimal (integer) values to decipher what the RFID card reader is sending.

Or you can use convert.tohex or HEX(n).

Good luck and Thanks!
Member 11720767 12-Jul-15 15:15pm
Hello Dear All,

I am facing a problem with convert delphi code to any body help me to get the solution please... please check the code below

m_arTxBfx:Array [0..255] of Byte; //Output Buffer
m_arRxBfx:Array [0..255] of Byte; //Input Buffer


Now I need to convert it

I have tried this way...

but its showing error.......

Please Help me ...

Thanks in Advance

S Houghtelin 13-Jul-15 7:42am
Your question has a better chance of being answered if you post it in the Q&A section

When you ask be sure to add what the error is showing, the error can provide a clue to the person who is willing to help.

In the mean time I would look a the syntax of the line:

MSDN Link:

In short,
myPort.write(array_to_send, first_byte_of_array, number_of_bytes_to_send)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 5 Aug 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100