Fixed Modbus register access
This commit is contained in:
@@ -11,6 +11,15 @@ VAR
|
||||
_xEnableInverter : BOOL;
|
||||
_fbString : FB_String('String 1');
|
||||
_fbInverter : FB_PowerSupplySunspec;
|
||||
_stInverterData : ST_SUNSPEC_CURRENT_VALUES;
|
||||
|
||||
// Variable to detect charge status change
|
||||
_eLastChargeStatus : E_CHARGE_STATUS;
|
||||
// Variable to detect battery status change
|
||||
_eLastBatteryStatus : E_BATTERY_STATUS;
|
||||
|
||||
// Variable to detect any battery status change
|
||||
_xBatteryStatusChange : BOOL;
|
||||
|
||||
_iState : INT;
|
||||
|
||||
@@ -25,10 +34,52 @@ VAR
|
||||
|
||||
// Small delay for inverter shutdown
|
||||
_tonBeginShutdown : TON := (PT := T#10S);
|
||||
|
||||
// Not all strings in automatic mode
|
||||
_fbNoAutomaticModeAlarm : Fb_TcAlarm;
|
||||
|
||||
// First cycle tag
|
||||
_xFirstCycle : BOOL := TRUE;
|
||||
|
||||
// ADS reader for modbus server data
|
||||
_fbADSReader : ADSREAD;
|
||||
|
||||
// Timer for ADS read
|
||||
_timADSReadTimer : TON;
|
||||
|
||||
// MQTT client
|
||||
_fbMQTTClient : FB_IotMqttClient;
|
||||
|
||||
// connect to mqtt broker
|
||||
_xConnectToMQTTBrocker : BOOL;
|
||||
|
||||
// DEBUG
|
||||
_xRestart : BOOL;
|
||||
END_VAR
|
||||
]]></Declaration>
|
||||
<Implementation>
|
||||
<ST><![CDATA[// Dely release of errors during PLC startup phase
|
||||
<ST><![CDATA[// ===============================
|
||||
// DEBUG
|
||||
IF _xRestart AND (_iState = 1010) THEN
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower := 0;
|
||||
_xConfirmAlarms := TRUE;
|
||||
END_IF
|
||||
|
||||
IF _xRestart AND (_iState = 0) THEN
|
||||
_xRestart := FALSE;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower := 1000;
|
||||
END_IF
|
||||
|
||||
// DEBUG
|
||||
// ===============================
|
||||
|
||||
IF _xFirstCycle THEN
|
||||
_xFirstCycle := FALSE;
|
||||
_fbString.Name := 'String 1';
|
||||
_xConnectToMQTTBrocker := TRUE;
|
||||
END_IF
|
||||
|
||||
// Dely release of errors during PLC startup phase
|
||||
_tonStartupDelay(IN := TRUE);
|
||||
|
||||
// Call string 1
|
||||
@@ -40,7 +91,7 @@ _fbString(
|
||||
xReleaseLimitErrors:= _xReleaseLimitsErrors AND _tonStartupDelay.Q,
|
||||
xConfirmAlarms:= _xConfirmAlarms);
|
||||
|
||||
|
||||
// DEACTIVATED FOR DEBUG REASONS !!!
|
||||
// Call inverter
|
||||
//_fbInverter(
|
||||
// sInverterIPAddr:= GVL_CONFIG.sInverterIp,
|
||||
@@ -48,12 +99,32 @@ _fbString(
|
||||
// rPower:= _rPowerInverter,
|
||||
// xReset:= _xConfirmAlarms,
|
||||
// rMaxBattPower:= DINT_TO_REAL(GVL_CONFIG.diMaxStringDischargePower),
|
||||
// xCloseDCRelais=> ,
|
||||
// rActDCCurrent=> ,
|
||||
// rActDCVoltage=> ,
|
||||
// xError=> ,
|
||||
// xActive=> );
|
||||
|
||||
// stCurrentValues => _stInverterData);
|
||||
|
||||
|
||||
// ===============================
|
||||
// Read modbus request count
|
||||
// ===============================
|
||||
_timADSReadTimer(IN := NOT _fbADSReader.BUSY, PT := T#200MS);
|
||||
_fbADSReader(
|
||||
NETID:= '',
|
||||
PORT:= 10500,
|
||||
IDXGRP:= 16#2000,
|
||||
IDXOFFS:= 1,
|
||||
LEN:= 4,
|
||||
DESTADDR:= ADR(GVL_MODBUS.stModbusEMSComm.stModbusReg11.udiLifeMessage),
|
||||
READ:= _timADSReadTimer.Q,
|
||||
TMOUT:= T#1S,
|
||||
BUSY=> ,
|
||||
ERR=> ,
|
||||
ERRID=> );
|
||||
|
||||
// ===============================
|
||||
// MQTT cummunication
|
||||
// ===============================
|
||||
// Handle connection to broker
|
||||
_fbMQTTClient.Execute(_xConnectToMQTTBrocker);
|
||||
|
||||
|
||||
// ===============================
|
||||
// State machine
|
||||
@@ -61,18 +132,18 @@ _fbString(
|
||||
CASE _iState OF
|
||||
0: // Idle
|
||||
// Wait for power command
|
||||
IF ABS(GVL_MODBUS.stModbusEMSComm.diSetpointActivePower) > GVL_CONFIG.diMinimumAbsPowerForEnable THEN
|
||||
IF (ABS(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower) > GVL_CONFIG.diMinimumAbsPowerForEnable) AND _fbString.xSafetyIntlksOk AND (NOT _fbString.xError) THEN
|
||||
_iState := 5;
|
||||
END_IF
|
||||
|
||||
5: // Check if power command is within limits
|
||||
IF GVL_MODBUS.stModbusEMSComm.diSetpointActivePower < GVL_CONFIG.diMaxStringDischargePower
|
||||
AND GVL_MODBUS.stModbusEMSComm.diSetpointActivePower > GVL_CONFIG.diMaxStringChargingPower THEN
|
||||
IF GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower < GVL_CONFIG.diMaxStringDischargePower
|
||||
AND GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower > GVL_CONFIG.diMaxStringChargingPower THEN
|
||||
_xEnableString := TRUE;
|
||||
_iState := 10;
|
||||
ELSE
|
||||
// Set error bitmap flag
|
||||
GVL_MODBUS.stModbusEMSComm.lwErrorBitmap.0 := 1;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.lwErrorBitmap.0 := 1;
|
||||
|
||||
// Goto error state
|
||||
_iState := 1000;
|
||||
@@ -90,7 +161,9 @@ CASE _iState OF
|
||||
|
||||
25: // Wait for inverter to be ready
|
||||
IF _fbInverter.xActive AND (NOT _fbInverter.xError) THEN
|
||||
GVL_MODBUS.stModbusEMSComm.eBatteryStatus := E_BATTERY_STATUS.ACTIVE;
|
||||
// Set battery status for modbus
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.ACTIVE;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 1;
|
||||
_iState := 30;
|
||||
END_IF
|
||||
|
||||
@@ -101,22 +174,23 @@ CASE _iState OF
|
||||
|
||||
30: // String and inverter enabled
|
||||
// Set inverter power to modbus requested power
|
||||
_rPowerInverter := DINT_TO_REAL(GVL_MODBUS.stModbusEMSComm.diSetpointActivePower);
|
||||
_rPowerInverter := DINT_TO_REAL(GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower);
|
||||
|
||||
// Check if the battery should still be active
|
||||
IF (GVL_MODBUS.stModbusEMSComm.diSetpointActivePower = 0) THEN
|
||||
IF (GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower = 0) THEN
|
||||
_xNoPowerRequested := TRUE;
|
||||
ELSE
|
||||
_xNoPowerRequested := FALSE;
|
||||
END_IF
|
||||
|
||||
// Set battery status
|
||||
IF GVL_MODBUS.stModbusEMSComm.diSetpointActivePower > 0 THEN
|
||||
GVL_MODBUS.stModbusEMSComm.eChargeStatus := E_CHARGE_STATUS.DISCHARGING;
|
||||
ELSIF GVL_MODBUS.stModbusEMSComm.diSetpointActivePower < 0 THEN
|
||||
GVL_MODBUS.stModbusEMSComm.eChargeStatus := E_CHARGE_STATUS.CHARGING;
|
||||
IF GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower > 0 THEN
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.DISCHARGING;
|
||||
ELSIF GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower < 0 THEN
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.CHARGING;
|
||||
|
||||
ELSE
|
||||
GVL_MODBUS.stModbusEMSComm.eChargeStatus := E_CHARGE_STATUS.UNDEFINED;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.UNDEFINED;
|
||||
END_IF
|
||||
|
||||
// Add small delay before shutdown by EMS is detected
|
||||
@@ -131,11 +205,12 @@ CASE _iState OF
|
||||
|
||||
// Start string shutdown
|
||||
_xEnableString := FALSE;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
|
||||
_iState := 35;
|
||||
END_IF
|
||||
|
||||
// Shutdown triggered by battery fully charged
|
||||
IF GVL_MODBUS.stModbusEMSComm.eChargeStatus = E_CHARGE_STATUS.CHARGING AND (_fbString.rCurrentVoltage >= GVL_CONFIG.rStringFullyChargedVoltage) THEN
|
||||
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus = E_CHARGE_STATUS.CHARGING AND (_fbString.rCurrentVoltage >= GVL_CONFIG.rStringFullyChargedVoltage) THEN
|
||||
_tonBeginShutdown(In := FALSE);
|
||||
|
||||
// Set inverter to zero power
|
||||
@@ -145,13 +220,13 @@ CASE _iState OF
|
||||
_xEnableString := FALSE;
|
||||
|
||||
// Change battery status
|
||||
GVL_MODBUS.stModbusEMSComm.eChargeStatus := E_CHARGE_STATUS.FULL;
|
||||
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.FULL;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
|
||||
_iState := 35;
|
||||
END_IF
|
||||
|
||||
// Shutdown triggered by battery empty
|
||||
IF GVL_MODBUS.stModbusEMSComm.eChargeStatus = E_CHARGE_STATUS.DISCHARGING AND (_fbString.rCurrentVoltage <= GVL_CONFIG.rStringEmptyVoltage) THEN
|
||||
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus = E_CHARGE_STATUS.DISCHARGING AND (_fbString.rCurrentVoltage <= GVL_CONFIG.rStringEmptyVoltage) THEN
|
||||
_tonBeginShutdown(In := FALSE);
|
||||
|
||||
// Set inverter to zero power
|
||||
@@ -161,8 +236,8 @@ CASE _iState OF
|
||||
_xEnableString := FALSE;
|
||||
|
||||
// Change battery status
|
||||
GVL_MODBUS.stModbusEMSComm.eChargeStatus := E_CHARGE_STATUS.FULL;
|
||||
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus := E_CHARGE_STATUS.FULL;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg10.uiActiveParallelMembers := 0;
|
||||
_iState := 35;
|
||||
END_IF
|
||||
|
||||
@@ -205,6 +280,7 @@ CASE _iState OF
|
||||
|
||||
45: // Wait for shutdown of string to be done
|
||||
IF (NOT _fbString.xInShutdownDischargeMode) AND _fbString.xOff THEN
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.OFF;
|
||||
_iState := 0;
|
||||
END_IF
|
||||
|
||||
@@ -217,36 +293,130 @@ CASE _iState OF
|
||||
_xEnableString := FALSE;
|
||||
_xEnableInverter := FALSE;
|
||||
_rPowerInverter := 0.0;
|
||||
GVL_MODBUS.stModbusEMSComm.eBatteryStatus := E_BATTERY_STATUS.ERROR;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.ERROR;
|
||||
_iState := 1010;
|
||||
|
||||
1010: // Wait for reset from error state
|
||||
IF (GVL_MODBUS.stModbusEMSComm.diSetpointActivePower = 0) AND (NOT _fbString.xError) AND (NOT _fbInverter.xError) THEN
|
||||
IF (GVL_MODBUS.stModbusEMSComm.stModbusReg12.diSetpointActivePower = 0) AND (NOT _fbString.xError) AND (NOT _fbInverter.xError) THEN
|
||||
// Reset modbus error register
|
||||
GVL_MODBUS.stModbusEMSComm.lwErrorBitmap := 0;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.lwErrorBitmap := 0;
|
||||
|
||||
// Reset modbus error flag
|
||||
GVL_MODBUS.stModbusEMSComm.eBatteryStatus := E_BATTERY_STATUS.OFF;
|
||||
GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus := E_BATTERY_STATUS.OFF;
|
||||
|
||||
// Goto init state
|
||||
_iState := 0;
|
||||
END_IF
|
||||
|
||||
END_CASE
|
||||
|
||||
// Send MQTT battery status
|
||||
_xBatteryStatusChange := FALSE;
|
||||
|
||||
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus <> _eLastBatteryStatus THEN
|
||||
_xBatteryStatusChange := TRUE;
|
||||
END_IF
|
||||
_eLastBatteryStatus := GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus;
|
||||
|
||||
IF GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus <> _eLastChargeStatus THEN
|
||||
_xBatteryStatusChange := TRUE;
|
||||
END_IF
|
||||
_eLastChargeStatus := GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus;
|
||||
|
||||
IF _xBatteryStatusChange THEN
|
||||
SendBatteryStatus();
|
||||
END_IF
|
||||
|
||||
// Reset alarm confirmation
|
||||
IF _xConfirmAlarms THEN
|
||||
_xConfirmAlarms := FALSE;
|
||||
END_IF]]></ST>
|
||||
</Implementation>
|
||||
<Method Name="SendBatteryStatus" Id="{d5a8daa9-6942-4419-a57d-7958e56f7e71}">
|
||||
<Declaration><![CDATA[METHOD SendBatteryStatus
|
||||
VAR
|
||||
_sJSONString : T_MaxString;
|
||||
_fbFormatString : FB_FormatString;
|
||||
_sBatteryStatus : T_MaxString;
|
||||
_sChargeStatus : T_MaxString;
|
||||
_sBMSVersion : T_MaxString;
|
||||
END_VAR
|
||||
VAR CONSTANT
|
||||
_sTemplateString : STRING := '{"batteryStatus": "%s", "chargeStatus": "%s", "bmsVersion": "%s"}';
|
||||
END_VAR
|
||||
]]></Declaration>
|
||||
<Implementation>
|
||||
<ST><![CDATA[// Temp strings because string format needs write access to these variables
|
||||
_sBatteryStatus := TO_STRING(GVL_MODBUS.stModbusEMSComm.stModbusReg11.eBatteryStatus);
|
||||
_sChargeStatus := TO_STRING(GVL_MODBUS.stModbusEMSComm.stModbusReg11.eChargeStatus);
|
||||
_sBMSVersion := GVL_MODBUS.stModbusEMSComm.stModbusReg10.sBMSVersion;
|
||||
|
||||
// Build JSON string
|
||||
_fbFormatString(
|
||||
sFormat:= _sTemplateString,
|
||||
arg1:= F_STRING(_sBatteryStatus),
|
||||
arg2:= F_STRING(_sChargeStatus),
|
||||
arg3:= F_STRINg(_sBMSVersion),
|
||||
sOut=> _sJSONString);
|
||||
|
||||
// Send message if connected and no formatting error occured
|
||||
IF NOT _fbFormatString.bError AND _fbMQTTClient.bConnected THEN
|
||||
_fbMQTTClient.Publish('status', ADR(_sJSONString), INT_TO_UDINT(LEN(_sJSONString)), GVL_MQTT.eMQTTQoS, FALSE, FALSE);
|
||||
END_IF]]></ST>
|
||||
</Implementation>
|
||||
</Method>
|
||||
<Action Name="SetMQTTParameter" Id="{f5ef2661-7fa1-4c9b-aa13-fba0d7bff32a}">
|
||||
<Implementation>
|
||||
<ST><![CDATA[//_fbMQTTClient.sClientId:= '';
|
||||
_fbMQTTClient.sHostName:= GVL_MQTT.sMQTTBrokerAddr;
|
||||
_fbMQTTClient.nHostPort:= 1883;
|
||||
//_fbMQTTClient.sTopicPrefix := '';
|
||||
_fbMQTTClient.nKeepAlive:= GVL_MQTT.uiMQTTKeepAlive;
|
||||
// _fbMQTTClient.ipMessageQueue := ;]]></ST>
|
||||
</Implementation>
|
||||
</Action>
|
||||
<LineIds Name="MAIN">
|
||||
<LineId Id="126" Count="55" />
|
||||
<LineId Id="405" Count="0" />
|
||||
<LineId Id="395" Count="0" />
|
||||
<LineId Id="400" Count="0" />
|
||||
<LineId Id="402" Count="1" />
|
||||
<LineId Id="399" Count="0" />
|
||||
<LineId Id="407" Count="0" />
|
||||
<LineId Id="406" Count="0" />
|
||||
<LineId Id="408" Count="0" />
|
||||
<LineId Id="410" Count="0" />
|
||||
<LineId Id="409" Count="0" />
|
||||
<LineId Id="534" Count="0" />
|
||||
<LineId Id="398" Count="0" />
|
||||
<LineId Id="404" Count="0" />
|
||||
<LineId Id="475" Count="0" />
|
||||
<LineId Id="396" Count="0" />
|
||||
<LineId Id="476" Count="0" />
|
||||
<LineId Id="478" Count="0" />
|
||||
<LineId Id="624" Count="0" />
|
||||
<LineId Id="477" Count="0" />
|
||||
<LineId Id="397" Count="0" />
|
||||
<LineId Id="126" Count="19" />
|
||||
<LineId Id="147" Count="0" />
|
||||
<LineId Id="784" Count="3" />
|
||||
<LineId Id="598" Count="0" />
|
||||
<LineId Id="808" Count="0" />
|
||||
<LineId Id="793" Count="10" />
|
||||
<LineId Id="789" Count="0" />
|
||||
<LineId Id="599" Count="0" />
|
||||
<LineId Id="463" Count="0" />
|
||||
<LineId Id="601" Count="0" />
|
||||
<LineId Id="600" Count="0" />
|
||||
<LineId Id="626" Count="0" />
|
||||
<LineId Id="617" Count="0" />
|
||||
<LineId Id="724" Count="0" />
|
||||
<LineId Id="152" Count="29" />
|
||||
<LineId Id="218" Count="0" />
|
||||
<LineId Id="182" Count="0" />
|
||||
<LineId Id="304" Count="0" />
|
||||
<LineId Id="207" Count="2" />
|
||||
<LineId Id="628" Count="0" />
|
||||
<LineId Id="314" Count="0" />
|
||||
<LineId Id="782" Count="0" />
|
||||
<LineId Id="210" Count="2" />
|
||||
<LineId Id="305" Count="1" />
|
||||
<LineId Id="308" Count="0" />
|
||||
@@ -260,6 +430,7 @@ END_IF]]></ST>
|
||||
<LineId Id="236" Count="0" />
|
||||
<LineId Id="309" Count="3" />
|
||||
<LineId Id="315" Count="1" />
|
||||
<LineId Id="632" Count="0" />
|
||||
<LineId Id="360" Count="1" />
|
||||
<LineId Id="313" Count="0" />
|
||||
<LineId Id="317" Count="0" />
|
||||
@@ -274,6 +445,7 @@ END_IF]]></ST>
|
||||
<LineId Id="263" Count="0" />
|
||||
<LineId Id="265" Count="0" />
|
||||
<LineId Id="260" Count="0" />
|
||||
<LineId Id="783" Count="0" />
|
||||
<LineId Id="245" Count="0" />
|
||||
<LineId Id="244" Count="0" />
|
||||
<LineId Id="328" Count="0" />
|
||||
@@ -311,6 +483,7 @@ END_IF]]></ST>
|
||||
<LineId Id="286" Count="0" />
|
||||
<LineId Id="282" Count="1" />
|
||||
<LineId Id="300" Count="0" />
|
||||
<LineId Id="666" Count="0" />
|
||||
<LineId Id="302" Count="1" />
|
||||
<LineId Id="301" Count="0" />
|
||||
<LineId Id="297" Count="2" />
|
||||
@@ -321,8 +494,38 @@ END_IF]]></ST>
|
||||
<LineId Id="252" Count="1" />
|
||||
<LineId Id="255" Count="0" />
|
||||
<LineId Id="254" Count="0" />
|
||||
<LineId Id="193" Count="9" />
|
||||
<LineId Id="193" Count="2" />
|
||||
<LineId Id="197" Count="0" />
|
||||
<LineId Id="647" Count="0" />
|
||||
<LineId Id="646" Count="0" />
|
||||
<LineId Id="654" Count="0" />
|
||||
<LineId Id="659" Count="0" />
|
||||
<LineId Id="648" Count="2" />
|
||||
<LineId Id="664" Count="0" />
|
||||
<LineId Id="656" Count="0" />
|
||||
<LineId Id="655" Count="0" />
|
||||
<LineId Id="657" Count="1" />
|
||||
<LineId Id="665" Count="0" />
|
||||
<LineId Id="661" Count="0" />
|
||||
<LineId Id="660" Count="0" />
|
||||
<LineId Id="662" Count="1" />
|
||||
<LineId Id="199" Count="3" />
|
||||
<LineId Id="25" Count="0" />
|
||||
</LineIds>
|
||||
<LineIds Name="MAIN.SendBatteryStatus">
|
||||
<LineId Id="27" Count="0" />
|
||||
<LineId Id="29" Count="1" />
|
||||
<LineId Id="28" Count="0" />
|
||||
<LineId Id="32" Count="0" />
|
||||
<LineId Id="31" Count="0" />
|
||||
<LineId Id="14" Count="4" />
|
||||
<LineId Id="5" Count="0" />
|
||||
<LineId Id="23" Count="0" />
|
||||
<LineId Id="19" Count="3" />
|
||||
</LineIds>
|
||||
<LineIds Name="MAIN.SetMQTTParameter">
|
||||
<LineId Id="2" Count="4" />
|
||||
<LineId Id="1" Count="0" />
|
||||
</LineIds>
|
||||
</POU>
|
||||
</TcPlcObject>
|
||||
Reference in New Issue
Block a user