Am attempting to interface a Vaisala PTB330 over RS232 to a CR6 datalogger via the user terminals.
For clarity, the PTB330 is wired to the CR6 as such
PTB330 - User Port RS232 TX to CR6 C2
PTB330 - User Port RS232 RX to CR6 C1
PTB330 - User Port RS232 GND to CR6 GND
_________________________
The PTB330 Serial settings are:
9600b/s
8N1
Poll Adress:0
________________________
I've confirmed the unit is outputting barometric pressure as an ASCII string in a 6 digit format with 2 decimal places via connecting directly with Putty ie.
1014.15
1014.18
I am having issues with converting this data to a floating point number to be logged within the unit as currently the output to the CR6 registers as NAN(Not a number).
At first I wanted to try and read it as a string, but I'd prefer the data stored directly as a floating point into a table for ease of use.
I've tried a few different approaches with trying to store the string inside a variable then convert it to a float before outputting it in the table. However I cannot seem to get it to work.
'CR6 Series
'Created by Short Cut (4.7)
'Declare Variables and Units
Public BattV
Public PTemp_C
Public AirTC
Public RH
Public WS_ms
Public WindDir
Public RawTempV
Public RawHumidV
Public RawBuffer As String * 50
Public CleanPressure As String * 20
Units BattV = Volts
Units PTemp_C = Deg C
Units AirTC = Deg C
Units RH = %
Units WS_ms = meters/second
Units WindDir = degrees
'Define Data Tables
DataTable(Hourly, True, -1)
DataInterval(5, 60, Sec, 10)
MQTTPublishTable(1,0,10,Min,2,0,0,0)
Minimum(1, BattV, FP2, False, True)
Maximum(1, BattV, FP2, False, True)
Minimum(1, PTemp_C, FP2, False, True)
Maximum(1, PTemp_C, FP2, False, True)
Minimum(1, AirTC, FP2, False, True)
Maximum(1, AirTC, FP2, False, True)
Minimum(1, RH, FP2, False, True)
Maximum(1, RH, FP2, False, True)
Maximum(1, WS_ms, FP2, False, True)
Minimum(1, WS_ms, FP2, False, True)
Sample (1, WindDir, FP2)
Sample (1, CleanPressure, String)
EndTable
DataTable(Seconds, True, -1)
DataInterval(5, 60, Sec, 10)
MQTTPublishTable(1,0,10,Min,2,0,0,0)
Minimum(1, BattV, FP2, False, False)
Sample (1, AirTC, FP2)
Sample (1, RH, FP2)
Sample (1, CleanPressure, String)
EndTable
'Main Program
BeginProg
SerialOpen(COMC1, 9600, 0, 0, 25)
'Main Scan
Scan(5, Sec, 1, 0)
Battery(BattV)
PanelTemp(PTemp_C, 60)
'Poll PTB330 & Read ASCII
SerialOutBlock(COMC1, "SEND", 4)
Delay(0, 500, mSec)
SerialInRecord(COMC1, RawBuffer, &H20, 0, &H0D, RawBuffer, 1)
If RawBuffer <> "" Then
' The idea here is to trim and convert to float, then format to "XXXX.XX"
Sprintf(CleanPressure, "%06.2f", Float( Trim(RawBuffer) ))
EndIf
'MP100A Temp & RH Sensor
VoltSE(AirTC, 1, mV1000, U1, False, 0, 60, 0.1, 0)
VoltSE(RawTempV, 1, mV1000, U1, False, 0, 60, 1, 0)
VoltSE(RH, 1, mV1000, U2, False, 0, 60, 0.1, 0)
VoltSE(RawHumidV, 1, mV1000, U2, False, 0, 60, 1, 0)
If (RH > 100) AND (RH < 108) Then RH = 100
'05103 Wind Sensor
PulseCount(WS_ms, 1, U6, 5, 1, 0.098, 0)
BrHalf(WindDir, 1, mV5000, U4, U3, 1, 2500, True, 20000, 60, 355, 0)
If (WindDir >= 355) OR (WindDir < 0) Then WindDir = 0
CallTable Hourly
CallTable Seconds
NextScan
EndProg
The
Sprintf(CleanPressure, "%06.2f", Float( Trim(RawBuffer) ))
doesn't feel like the right way to do it.
Any assistance would be greatly appreciated.
Do a SerialFlush() before the SerialOutBlock() to empty the serial buffer of anything left over.
If the output is fixed width, you can change SerialInRecord() by setting BeginWord parameter to 0 and number of expected bytes to 7. Otherwise, use SerialIn() instead of SerialInRecord(). Destination variable should be a string.
SplitStr with option 0 should pull the floating point number out of the string.
Thanks JDavis for your assistance.
Updated CRBasic based on your advice.
'CR6 Series
'Declare Variables and Units
Public BattV
Public PTemp_C
Public AirTC
Public RH
Public WS_ms
Public WindDir
Public RawTempV
Public RawHumidV
Public NBytesReturned
Public FP_Pressure As String *20
Units BattV = Volts
Units PTemp_C = Deg C
Units AirTC = Deg C
Units RH = %
Units WS_ms = meters/second
Units WindDir = degrees
Units FP_Pressure = hPa
'Define Data Tables
DataTable(Hourly, True, -1)
DataInterval(5, 60, Sec, 10)
MQTTPublishTable(1,0,10,Min,2,0,0,0)
Minimum(1, BattV, FP2, False, True)
Maximum(1, BattV, FP2, False, True)
Minimum(1, PTemp_C, FP2, False, True)
Maximum(1, PTemp_C, FP2, False, True)
Minimum(1, AirTC, FP2, False, True)
Maximum(1, AirTC, FP2, False, True)
Minimum(1, RH, FP2, False, True)
Maximum(1, RH, FP2, False, True)
Maximum(1, WS_ms, FP2, False, True)
Minimum(1, WS_ms, FP2, False, True)
Sample (1, WindDir, FP2)
Sample (1, FP_Pressure, FP2, False,)
EndTable
DataTable(Seconds, True, -1)
DataInterval(5, 60, Sec, 10)
MQTTPublishTable(1,0,10,Min,2,0,0,0)
Minimum(1, BattV, FP2, False, False)
Sample (1, AirTC, FP2
Sample (1, RH, FP2)
Sample (1, FP_Pressure, FP2)
EndTable
'Main Program
BeginProg
'RS232 Setup
SerialOpen(COMC1, 9600, 0, 0, 30)
'Main Scan
Scan(5, Sec, 1, 0)
Battery(BattV)
PanelTemp(PTemp_C, 60)
'Poll PTB330 & Read ASCII
SerialFlush(ComC1)
SerialInRecord (ComC1,FP_Pressure,0,8,&H0A,NBytesReturned,01)
Trim(FP_Pressure)
'MP100A Temp & RH Sensor
VoltSe(AirTC, 1, mV1000, U1, False, 0, 60, 0.1, 0)
VoltSe(RawTempV, 1, mV1000, U1, False, 0, 60, 1, 0)
VoltSe(RH, 1, mV1000, U2, False, 0, 60, 0.1, 0)
VoltSe(RawHumidV, 1, mV1000, U2, False, 0, 60, 1, 0)
If (RH > 100) AND (RH < 108) Then RH = 100
'05103 Wind Sensor
PulseCount(WS_ms, 1, U6, 5, 1, 0.098, 0)
BrHalf(WindDir, 1, mV5000, U4, U3, 1, 2500, True, 20000, 60, 355, 0)
If (WindDir >= 355) OR (WindDir < 0) Then WindDir = 0
CallTable Hourly
CallTable Seconds
NextScan
EndProg
I used the terminal emulator and have read the incoming from the port with the PTB330 setup to SEND a reading at 5Hz, the incoming from the unit with ASCII turned off in the terminal emulator is as follows.
21:07:46.607 R 31 30 32 30 2E 34 33 0D 0A 1020.43.. 21:07:47.607 R 31 30 32 30 2E 34 33 0D 0A 1020.43.. 21:07:48.606 R 31 30 32 30 2E 34 33 0D 0A 1020.43..
With ASCII enabled -
23:22:29.080 R 1016.23 23:22:34.086 R 1016.23 23:22:39.084 R 1016.23
I can see the string numbers in the Monitor Data tab now (hooray!) within PC400 however, when viewing the data log file in PC400, the data shows up as "1016.23". How do I have this entered into my tables as a FP number with 2 decimals instead of what seems to be a number string.
I tried SplitStr however was receiving and error related to ResultVar(). I am not sure if I am using it correctly.
Thanks.
This post is under review.