Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,


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 :

SerialPort1.Open()

VB
Dim code As String = ""

Dim receiveddata As Byte = 0




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

SerialPort1.Write(code)

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.

Thanks.


Best regards


Didier
Posted
Updated 29-Mar-20 14:18pm

It looks a though you are sending data and getting a response.
VB
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:
VB
receiveddata = Hex(SerialPort1.ReadByte())

For sending a data string to the device I would use:
VB
SerialPort1.WriteLine(code)


Regards

[Edit]
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 .
 
Share this answer
 
v2
Comments
Didier Cauberghe 4-Aug-11 15:32pm    
Hello,

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.

Thanks.

br.

Didier
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)

SerialPort1.WriteLine(code)
S Houghtelin 4-Aug-11 16:05pm    
See my updated answer.
Didier Cauberghe 4-Aug-11 16:13pm    
Hello,

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.

br.

Didier
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.
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)

VB
'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
    SerialPort1.Write(Convert.ToChar(arCommand(x)))
Next

For a complete list of encoding capabilities see this MSDN link.
http://msdn.microsoft.com/en-us/library/system.text.encoding.getencodings.aspx[^]

Alright Didier, Vote this one a 5 please.

Regards
 
Share this answer
 
Comments
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 serialport1.read() 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 vb.net... 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


myPort.ClearBuffer(false,true);
myPort.Write(m_arTxBfx[0],m_InputLength+4);

Now I need to convert it vb.net

I have tried this way...
myPort.write(m_arTxBfx,m_InputLength,4)

but its showing error.......

Please Help me ...

Thanks in Advance

Moseur
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 http://www.codeproject.com/Questions/ask.aspx

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:
myPort.write(m_arTxBfx,m_InputLength,4);

MSDN Link: https://msdn.microsoft.com/en-us/library/1050fs1h(v=vs.110).aspx

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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900