Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VisualStudio
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()
 
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 3-Aug-11 21:02pm
Rate this: bad
good
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:
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 .
  Permalink  
v2
Comments
Didier Cauberghe at 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 at 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 at 4-Aug-11 16:05pm
   
See my updated answer.
Didier Cauberghe at 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 at 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 at 5-Aug-11 2:54am
   
Hello,
 
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.
 
Thanks
 
Br.
 
Didier
S Houghtelin at 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
good
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
    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
  Permalink  
Comments
Didier Cauberghe at 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 at 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!

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



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