'CR1000X Series Datalogger
'Example datalogger program for Apogee Instruments SN-522-SS Modbus Net Radiometer sensors
'date: June 9, 2020
'Revised: Dec 29, 2020
'program author: John Huber

'For more technical information regarding the SN-522-SS operation as well as default settings and register address lists, please consult the sensor's manual, which can be found online:
'https://www.apogeeinstruments.com/product-manuals/

'Wiring:
'White -> C6 Comm port (RS485 +)
'Blue -> C5 Comm port (RS485-)
'Black -> G port (Ground)
'Green -> G port (Communication mode selector wire.  Powered = RS232, Grounded = RS485)
'Red -> 12V port (Input power)
'Clear -> Shield ground

'If using a datalogger model other than the CR1000, some adjustments to commands will be necessary

'Explanation of Constants and Variables Used in Datalogger Program
'BattV = datalogger battery voltage
'PanelT = datalogger panel temeprature
'Net(4) = array to hold output from the ModbusMaster function
'    Location 1 = incoming SW, location 2 = outgoing SW,
'    Location 3 = incoming LW, location 4 = outgoing LW
'Incoming_SW = downwelling shortwave radiation in Watts per square meter
'Outgoing_SW = upwelling (reflected) shortwave radiation in Watts per square meter
'Incoming_LW = downwelling longwave radiation in Watts per square meter
'Outgoing_LW = upwelling (emitted) longwave radiation in Watts per square meter
'Errorcode = see CRBasic Editor help for the ModbusMaster function for a list of error codes and associated causes (0 = normal operation)

'Declare Public Variables
Public PTemp, Batt_volt
Public Errorcode
Public Net(4)

'Rename variables from Net(4) array
Alias Net(1) = Incoming_SW
Alias Net(2) = Outgoing_SW
Alias Net(3) = Incoming_LW
Alias Net(4) = Outgoing_LW

'Define Data Tables.
DataTable (Net_Rad,1,-1) 'Set table size to # of records, or -1 to autoallocate.
	DataInterval (0,1,Min,10)
	Minimum (1,Batt_volt,FP2,False,False)
	Sample (1,PTemp,FP2)
	Average(1,Incoming_SW,IEEE4,False)
	Average(1,Outgoing_SW,IEEE4,False)
	Average(1,Incoming_LW,IEEE4,False)
	Average(1,Outgoing_LW,IEEE4,False)
EndTable

'Main Program
BeginProg
	Scan (1,Sec,0,0)
		PanelTemp (PTemp,60)
		Battery (Batt_volt)
		'Open modbus serial port in RS232 mode
		SemaphoreGet(3)
		  SerialOpen(ComC5,19200,2,0,256,3)
		  SerialFlush(ComC5)
		SemaphoreRelease(3)
		'Obtain PPF measurement
		SemaphoreGet(3)
		  ModbusMaster(Errorcode,ComC5,19200,1,3,Net(),1,4,2,100,2)
		SemaphoreRelease(3)
		CallTable Net_Rad
	NextScan
EndProg


