Send Hex Data To Serial Port Java
Does the device receive the data you send across the serial port? Java inheritance for loop question/issue - 2 replies. Gurux.serial.java is easy to use Open Source serial port library for Windows and Linux] Serial Programming. Sending/reading hex data to a serial port in Powershell. I have a device which responds to serial port requests.
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. Best regards Didier.
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. 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. 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. OK, Here it is, damn what pain. Anyway the default decoding only supports 128 character so value above 127 will be displayed as a '?' To change this the SerialPort encoding needs to be changed. 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. Land surveying graduate program. In the mean time I would look a the syntax of the line: myPort.write(marTxBfx,mInputLength,4); MSDN Link: In short, myPort.write(arraytosend, firstbyteofarray, numberofbytestosend). When answering a question please:.
Read the question carefully. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Java Serial Port Example
Let's work to help developers, not make them feel stupid.