I have a program to read out the data from a sensor using RS232. According to the manufacturer the 4-byte hex output from the sensor should be converted to a two's complement, converted to a decimal and then divided by 70 to get the correct flow.
Unfortunately I cannot find any command oin CRBASIC that handles convertion of hex or dec to binary which would be the easiest way to find the two's complement. I have tried subtracting each byte from F and adding 1 but this is not giving the correct result.
Does anyone have any idea how I can do this? Below is my example code.
'CR1000X Series Datalogger
'Sensirion ASF1430 MFC for O3 flow
Public O3flow(1) As String *36
Public O3flow_in1 As Long
Public O3flow_in2 As String *36
Units O3flow = sccm
'MAIN PROGRAMM
BeginProg
'Configure RS232 Sensirion MFCs for flows
'SerialOpen(ComC5,19200,19,0,200,0)
Scan (500,mSec,100,0)
' Measure System parameters
'Read Sensirion flow meter:
'SerialInBlock (ComC5,O3flow_in1,36)
'O3flow_in2=Hex(O3flow_in1)
'SplitStr (O3flow(),O3flow_in2,"7F7F",1,4)
'Use O3flow=87AE to test
O3flow()= "87AE"
'Get two's complement:
O3flow()=&HF -O3flow(1,1,1) & &HF- O3flow(1,1,2)& &HF- O3flow(1,1,3)& &HF- O3flow(1,1,4) + &H1
O3flow()=HexToDec(O3flow())/70 '70 is the calibration factor.
NextScan
EndProg
Sounds like a 16 bit signed integer.
Public FlowString As String * 4 = "87AE"
Public tempInteger As Long
Public Flow As Float
'Main Program
BeginProg
Scan (1,Sec,0,0)
tempInteger = HexToDec(FlowString)
If tempInteger AND &h8000 Then 'Check sign bit 'Alternative check: If tempInteger > 32767 Then
tempInteger = tempInteger - 65536 'A shortcut for flipping bits correctly
EndIf
Flow = tempInteger / 70
NextScan
EndProg