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
'
DataTable(Ascii_VN,SonTrigger,-1)
TableFile("CRD:Ascii_VN_",64,-2,N,0,Min,0,0)
Sample(12,VN(),Float)
EndTable
'
' 20 Hz table for VecNav binary data
'
DataTable (Bin_VN,SonTrigger,-1)
TableFile("CRD:Bin_VN_",64,-2,N,0,Min,0,0)
Sample(1,VN2ascii,String)
Sample(1,yawb,IEEE4)
Sample(1,pitchb,IEEE4)
Sample(1,rollb,IEEE4)
EndTable
BeginProg
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
SerialFlush(ComC1)
'
' 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
SplitStr(VN(),VNString,"",12,0)
' flush the binary com port then read in the 18 byte string or until a carriage return is read
SerialFlush(COMU3)
SerialIn(VN2binary,ComU3,0,CHR(13),BinSize)
VN2ascii=""
' convert binary data to hex (to check)
For K=1 To BinSize
ascStr(K)=ASCII(VN2binary(1,1,K))
VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x") & " "
Next K
' decode each set of 4 bytes to decimal
For K=5 To 9
ascStr(K)=ASCII(VN2binary(1,1,K))
VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x")
Next K
yawb=HexToDec(VN2ascii)
VN2ascii=""
'For K=9 To 12
ascStr(K)=ASCII(VN2binary(1,1,K))
VN2ascii=VN2ascii & FormatLong(ASCII(VN2binary(1,1,K)),"%02x")
Next K
pitchb=HexToDec(VN2ascii)
' 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
NextScan
Next J
EndProgram