Link to home
Create AccountLog in
Avatar of cguarino
cguarino

asked on

How to manipulate binary data recieved via a serial port - VB6

Visual Basic 6.0
I've searched the forum and although some threads come close, none answer my question. I have a data acq box that transmits data via RS232. It constantly transmits this data one byte at a time. Each of the 28 bytes between the headers and the EOM is a measured parameter value. The data is binary with two header bytes (250 or 11111010) and one EOM byte (254 or 11111110). The data that I'm interested in resides in the 28 bytes in between the headers and EOM. I can connect to the box and receive data but I can't figure out how to parse it and use it. If I try to put it in a text box a get a lot of ? and some random characters.  I understand that is because the text box is trying to read my bytes as characters.  I just did that to make sure my MSCOMM code was working and I was receiving data.  Additionally, I know the box is working because I used a port monitor setup for binary and was able to see all of the data. I pasted a capture from the monitor below. My problem is how do I collect the data, identify the headers and EOM, parse the bytes, and convert the data into a number that I can assign to a variable? I wrote a function to convert an eight digit binary number to a decimal but it needs 1s and 0s for it to work. I can't see 1s and 0s. I don't want to convert it to characters unless I have to. I rather just convert it to numbers.

00000110 01110100 00000000 00000000 00000000 11111110 11111010 11111010 10000110 01010111 11110011 00000100 11101100 00011110 10000001 10101010 11111111 00000001 11100000 00000001 11100000 00000001 11100000 00000001 11100000 00000110 01110100 00000110 01110100 00000110 01110100 00000110 01110100 00000000 00000000 00000000 11111110 11111010 11111010 10000110 01010111
Note: I captured this data using a port monitor set up to recieve binary.

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

You can get the Acsii value of each character with the Asc() function. This will yield 6,116,0,0,0,254 etc

This is fine if each byte is a number on its own, but your device may be sending some larger number in two or four byte sets. Also they may be high order first - big endian ot low order - little endian. Do you know the exact specification?
Avatar of cguarino
cguarino

ASKER

Most of the bytes are a single number.  However, there are some two byte numbers that transmit the MSB first.   What do I do about those?  Also, is this fast?  I don't want to slow the processing time down because this is for an engine gauge and needs to be real time.  As for your solution are you saying I can do something like:

rBuff=Mscomm1.input
sBuff=Asc(rBuff)

Asc with return the value of the leftmost character in a string, so that could work.

It might be easier to assemble the whole message and then to parse it, but if you are able to keep track in real time then you could do it as the message parts come in.

Here are two functions to convert character pairs to numerics. As you will understand the one to use depends on which method the device is using to send integers.


Function IntLittleE(strTwoChars As String) As Integer
    IntLittleE = Asc(Mid$(strTwoChars, 1, 1)) + Asc(Mid$(strTwoChars, 2, 1)) * 2 ^ 8
End Function
 
Function IntBigE(strTwoChars As String) As Integer
    IntBigE = Asc(Mid$(strTwoChars, 1, 1)) * 2 ^ 8 + Asc(Mid$(strTwoChars, 2, 1))
End Function

Open in new window

I don't have a string coming in.  It is just 1s and 0s that I can't even see.  If it was a string I could deal with it.  The port monitor I was using had no trouble turning the data into a string of 1s and 0s.  That is what I need to know how to do.
ASKER CERTIFIED SOLUTION
Avatar of cguarino
cguarino

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer