Sample: 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);
ELSIF NOT fbWriteCoils.bBUSY THEN
bWrite := FALSE;
END_IF
fbWriteCoils(bExecute:=FALSE);
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
Requirements
Development environment |
Target system type |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_ModbusSrv |