Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.

Log in or register to post/reply in the forum.

reading binary data from serial port

pfjessen Feb 9, 2021 11:28 AM

I'm trying to read binary data from a serial port and have run into a brick wall. The data is always an 18 byte record with the  first four bytes the same (basically a header). In hex it would be something like:

FA 01 08 00 5E 27 04 43 AD CF 1E C1 0C 35 25 41 0C 50 where bytes 5-9 are yaw, bytes 10-13, are roll, and bytes 14-17 are pitch from an IMU.

To test whether I'm reading the binary data correctly I'm also saving the ascii pitch, roll, and yaw data from another serial port. When I convert the binary data to a hex string to output to a data table the first 4 bytes are fine, but after that is where I run into problems. If I take the actual numbers for say yaw and convert it to hex it will match up with two of the four bytes (after some byte swapping), but not the other two. 

Here's some of the code Im using. Any suggestions would be much appreciated!

Public VN(12) As Float   'serial port 1 IMU data from the VN-100/200
Const BinSize=18          'Size of binary string from VN-200. Adjust as necessary
Const Dbin=BinSize * 2
'misc. constants, etc
Public VNString As String * 125    ' Raw VectorNav string ($VNYMR)
' Array for VN100 channels
Public VN2binary As String * BinSize 'VectorNav binary string for serial port 2
Public VN2ascii As String * Dbin
Public VN_NByteRet
Public VN2_NByteRet
Public yawb As Float
Public rollb As Float
Public pitchb As Float' yaw, pitch, roll from binary
Public r1 As Float
Dim J, K

'20 Hz table For VecNav ascii data
' 20 Hz table for VecNav binary data
DataTable (Bin_VN,SonTrigger,-1)

SerialOpen(ComC1,115200,0,0,500,0) 'for IMU ascii data
SerialOpen(COMU3,115200,19,0,500) ' This format (19) is for TTL binary
' Main loop for program (just do single loop)
For J=1 To 1 Step 1
' main scan is a 50 msec scan for 20 Hz data, it continues till exitscan is called

Scan(50,msec,bufsize,N) ' scan for 20 Hz data
SerialInRecord(ComC1,VNString,36,0,&H0D0A,VN_NByteRet,01) ' this is the ascii data

' flush the binary com port then read in the 18 byte string or until a carriage return is read

' convert binary data to hex (to check)
For K=1 To BinSize
VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x") & " "
Next K
' decode each set of 4 bytes to decimal
For K=5 To 9
VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x")
Next K

'For K=9 To 12
 VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x")
 Next K

' VN2ascii=""
' For K=13 To 16
'' ascStr(K)=ASCII(VN2binary(1,1,K))
' VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x")
' Next K
' rollb=HexToDec(VN2ascii)

CallTable Ascii_VN
CallTable Bin_VN

Next J


Log in or register to post/reply in the forum.