Digital IO access
This sample explains the access to a TwinCAT system via Modbus.
The default mapping of the TwinCAT Modbus TCP mapps the digital output (coils) to the physical outputs of the PLC.
PROGRAM MAIN
VAR
Q00 AT%QX0.0 : BOOL;
Q01 AT%QX0.1 : BOOL;
Q02 AT%QX0.2 : BOOL;
Q03 AT%QX0.3 : BOOL;
Q04 AT%QX0.4 : BOOL;
Q05 AT%QX0.5 : BOOL;
Q06 AT%QX0.6 : BOOL;
Q07 AT%QX0.7 : BOOL;
fbWriteCoils : FB_MBWriteCoils;
bWrite : BOOL;
nValue : INT;
END_VAR
IF NOT bWrite THEN
nValue := nValue + 1;
bWrite := TRUE;
fbWriteCoils.nQuantity := 8;
fbWriteCoils.cbLength := SIZEOF(nValue);
fbWriteCoils.pSrcAddr := ADR(nValue);
fbWriteCoils.tTimeout := T#5s;
fbWriteCoils(bExecute:=TRUE);
ELSE
IF NOT fbWriteCoils.bBUSY THEN
bWrite :=FALSE;
END_IF
fbWriteCoils(bExecute:=FALSE);
END_IF
The counter nValue will be written to physical outputs of the plc (Q00-Q07) by a rising edge of bWrite.
The bit ordering is explained in this table:
Bit | 8 MSB | 7 | 6 | 5 | 4 | 3 | 2 | 1 LSB |
---|---|---|---|---|---|---|---|---|
Output | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
MSB = Most significant bit
LSB = Least significant bit