PROGRAM ReadDownholeSensor
ModbusCycleTimer(NOT ModbusCycleTimer.Q, ModbusCycleTime);
DH_SENSOR_MODBUS0(ModbusCycleTimer.Q AND Downhole_Tool_Enabled, FALSE, cfg_DHSensor_LocalCfg, cfg_DHSensor_TargetCfg0, ModbusData0);

DH_SENSOR_MODBUS1000(DH_SENSOR_MODBUS0.Q AND Downhole_Tool_Enabled, FALSE, cfg_DHSensor_LocalCfg, cfg_DHSensor_TargetCfg1000, ModbusData1000);

sts_ModbusError := DH_SENSOR_MODBUS0.Error OR DH_SENSOR_MODBUS1000.Error;

IF Downhole_Tool_Enabled THEN
	(*Modbus Mapping*)
	DH_IntakeTemperature 				:= ANY_TO_REAL(ModbusData0[2]) / 10.0;
	DH_IntakePressure 					:= ANY_TO_REAL(ModbusData0[3]) - 14.0;
	DH_NumChannels						:= ModbusData0[10];
	DH_ToolVoltage						:= ModbusData0[11];
	DH_MaxIntakeTemperature_Startup		:= ANY_TO_REAL(ModbusData0[40]) / 10.0;
	DH_MaxIntakePressure_Startup		:= ModbusData0[41];
	DH_DownholeStatus_INT				:= ModbusData0[99];
	DH_Fluid_Level 						:= DH_IntakePressure * (1.0 / 0.433) * (1.0 / cfg_SpecificGravity) + cfg_DHSensorDistToIntake;

	CASE ModbusData0[99] OF
		0: DH_DownholeStatus := 'OK';
		1: DH_DownholeStatus := 'Connecting';
		2: DH_DownholeStatus := 'Open Circuit';
		3: DH_DownholeStatus := 'Shorted';
		4: DH_DownholeStatus := 'Cannot Decode';
	ELSE
		DH_DownholeStatus := 'Error';
	END_CASE;

	DH_ToolType							:= ModbusData1000[5];
	DH_PSIRating						:= ModbusData1000[7] * 1000;
	DH_MaxIntakeTemperature_Forever		:= ANY_TO_REAL(ModbusData1000[8]) / 10.0;
	DH_MaxIntakePressure_Forever		:= ModbusData1000[9];
ELSE
	DH_DownholeStatus := 'Disabled';
END_IF;
END_PROGRAM