Reading Hex data from serial port

Tyler G.
Lititz, PA

Apr 27, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

I am using ComReceive to receive a response that has been sent to the com port. I am reading the data in as a string but the values are actually in hex. I am converting each char of the string into a byte to be able to read the data after I get it. My problem is that I am receiving the hex values 0x11 0x03 0x02 and it appears that the reading has stopped even though the message to be read is longer than that (I am using a serial port spy to confirm that the whole message is sent to the computer). I know that 0x02 and 0x03 are ASCII start of text and end of text respectively and was wondering if this was why my message gets cut off when reading these characters.

Can I read these values into a byte array or some other variable to avoid getting the message cut off? or is there a way to dump the entire buffer into a string?

Solution Available
DrATEasy (Ron Y.)
Mission Viejo, CA

Apr 27, 2011
358 Posts

1  |  0  

Re: Reading Hex data from serial port

Can you post the ComReceive  code here to see the arguments that you are passing in?

If you are not using a terminator you probably need to specify aioDisableComReceiveEarlyReturn as enMode argument, since otherwise a slight gap in the transmission will cause a return without reading the whole buffer.

After the 0x02, do you have a 0x00?

You can read to array of bytes  or any data type since the target buffer can except any data type.

Tyler G.
Lititz, PA

Apr 28, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

I do have a 0x00 after the 0x02. My complete response should be as follows:

0x11 0x03 0x02 0x00 0x20 0x78 0x5F

my ComReceive command started out looking like this:
ComReceive(COM_NUM, "\r", TIMEOUT, aioDefault, sResponse,)
but I already tried removing the terminator and setting it to a random character that wasn't in the response string. I have also tried all of the enMode arguments as well. In all cases I either get an error or just the first three characters of the full response.

Tyler G.
Lititz, PA

Apr 28, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

I am not sure why the 0x02 0x00 combination gave me problems using the ComReceive command but I found another solution to my problem that is a little less clean than desired but at least it works.

I right clicked on libraries in the program in the workspace view -> Insert Library Below -> .NET Assemblies Tab -> System, Version=2.0.0.0

I then had to rename the Library to SystemLib instead of System so it wouldn't conflict with the System in the project.

After adding the library a Systemlib.SerialPort is available as a variable type.

The code below will set up the COM port 1 and send out a message via a byte array. It then reads the response (first 8 bytes of the response), displaying each byte returned in a msgbox. Note: I did attempt to use the readexisting property of the SerialPort but I got the same three character response as when I used ComRecieve. It is also possible to loop using the ReadBufferSize property so responses of variable sizes can be read with no errors.


ComPort = New Systemlib.SerialPort
ComPort.PortName = "COM1"
ComPort.BaudRate = 9600
ComPort.Handshake = Systemlib.Handshake.None
ComPort.stopbits = Systemlib.StopBits.One
ComPort.Parity = Systemlib.Parity.None
Comport.ReadTimeout = 500
ComPort.WriteTimeout = 500
ComPort.Open()

ComPort.DiscardInBuffer()
ComPort.DiscardOutBuffer()

outBytes[0] = 0x48
outBytes[1] = 0x65
outBytes[2] = 0x6C
outBytes[3] = 0x6C
outBytes[4] = 0x6F
outBytes[5] = 0x20
outBytes[6] = 0x3A
outBytes[7] = 0x29

ComPort.Write(outBytes,0,8)

for a = 0 to 7
    try
        msgbox(convert.tostring(ComPort.ReadByte(),16))
    catch

    endtry
next

ComPort.Close()

DrATEasy (Ron Y.)
Mission Viejo, CA

Apr 28, 2011
358 Posts

0  |  0  

Re: Reading Hex data from serial port

If you open the monitor window, do you see all characters displayed? What is the length return by ComReceive?

Tyler G.
Lititz, PA

Apr 28, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

Using the serial port spy I can see that the entire message is in the buffer but when I use ComReceive I only get the first three characters.

DrATEasy (Ron Y.)
Mission Viejo, CA

Apr 28, 2011
358 Posts

0  |  0  

Re: Reading Hex data from serial port

What do you see using ATEasy Monitor Window?

Print ComReceive(...)

will print 3?

Tyler G.
Lititz, PA

Apr 28, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

it prints 7 despite the length of the string only being 3

DrATEasy (Ron Y.)
Mission Viejo, CA

Apr 28, 2011
358 Posts

0  |  0  

Re: Reading Hex data from serial port

As suspected. So the data is there. If you open the watch window on response and expand it do you see the data?

What is print GetLen(sResponse)? Maybe the length is not properly set?

Tyler G.
Lititz, PA

Apr 28, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

no the length of sResponse is only 3

DrATEasy (Ron Y.)
Mission Viejo, CA

Apr 28, 2011
358 Posts

0  |  0  

Re: Reading Hex data from serial port

What if you call SetLen(sResponse, 7) after call to ComReceive, is the data there?

Also If you change the sResponse type rom string to Byte[100]. Is it working?

Tyler G.
Lititz, PA

Apr 28, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

setting the length of the string does not help. Reading into a byte array does work.

DrATEasy (Ron Y.)
Mission Viejo, CA

Apr 29, 2011
358 Posts

0  |  0  

Re: Reading Hex data from serial port

Try to set the Len before calling calling ComReceive:

SetLen(sResponse, 1024)
ComReceive(... sResponse...)

That should solve the issue. If you don't specify the maximum # of bytes and the sResponse have data - ATEasy will use the size of sResponse to store the result and truncate and size the string.

Another solution is to set the lBytes argument to large number for example 1024.

Tyler G.
Lititz, PA

May 2, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

I have already tried setting the length of the string, and also setting the lbytes larger than the message would ever be. Neither of them worked when using a string. It also appears that the program isn't dealing well with the New Systemlib.SerialPort that I create.

I will have to stick with using comRecieve with a byte array to solve my problem.

DrATEasy (Ron Y.)
Mission Viejo, CA

May 2, 2011
358 Posts

0  |  0  

Re: Reading Hex data from serial port

OK. I will try to create a test case to see if I can replicate the issue.

Tyler G.
Lititz, PA

May 3, 2011
19 Posts

0  |  0  

Re: Reading Hex data from serial port

I tried to use the solution of reading the serial data to a byte array but it appears to only work when receiving the first message. After the first message the byte array suffers from the same problem as the string in that it only reads the first three bytes, 0x11 0x03 0x02. I did manage to get my code above to work reliably so I will have to use that solution.



Please Note
You need to have a M@GIC account to participate in the Forums.
Not yet registered on our website? Click here to register today!

All content, information and opinions presented on the Marvin Test Solutions User Forums are those of the authors of the posts and messages and not Marvin Test Solutions'. All attachments and files are downloaded at your own risk. [Read More]