'CR1000X Series Datalogger
'Example datalogger program for Apogee Instruments SP-722-SS Albedometers
'date: May 20, 2021
'program author: John Huber

'For more technical information regarding the SP-722-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 CR1000X, some adjustments to commands will be necessary

'Explanation of Constants and Variables Used in Datalogger Program
'BattV = datalogger battery voltage
'PanelT = datalogger panel temperature
'Output1 = Data retrieved from holding registers 0-1 and 2-3
'Output2 = Data retrieved from holding registers 8-9
'SWi = incoming global shortwave radiation in W m-2 (watts per square meter)
'SWo = outgoing global shortwave radiation in W m-2 (watts per square meter)
'Albedo = Albedo calculated from SWi and SWo

'Errorcode = Modbus error code value (Check CRBasic help menu for a list of error codes)

'Declare Public Variables
Public PTemp, Batt_volt
Public Output1(2), Output2(1)
Public Errorcode

Alias Output1(1) = SWi
Alias Output1(2) = SWo
Alias Output2(1) = Albedo

'Define Data Tables.
DataTable (Solar,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,SWi,IEEE4,False)
	Average(1,SWo,IEEE4,False)
	Average(1,Albedo,IEEE4,False)
EndTable

'Main Program
BeginProg
	Scan (2,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 SWi measurement
		SemaphoreGet(3)
		  ModbusMaster(Errorcode,ComC5,19200,1,3,Output1(),1,2,2,100,2)
		SemaphoreRelease(3)
		SemaphoreGet(3)
		  ModbusMaster(Errorcode,ComC5,19200,1,3,Output2(),9,1,2,100,2)
		SemaphoreRelease(3)
		CallTable Solar
	NextScan
EndProg



