The PLC library Tc3_IoLink is a library that can be used for communication with IoLink devices, and it supports the following profiles:
- Common Profile
- Smart Sensor Profile
Within this library, there are function blocks available that allow for the reading and writing of parameters.
System requirements
- WinXP, WES, Win7, WES7, WEC7 IPC or CX, (x86, x64, ARM)
- Min. TwinCAT Version:3.1.4024.25
- Min. TwinCAT Level:TC1200 TC3 PLC
Reference Link
Basic
Before using Tc3_IOLINK, let me guide you on how to read data from an IO-LINK device using the Beckhoff TwinCAT’s AoE screen. Please click on EL6224.
Open the AoE-Online Tab.
You can select a Port number from the Port Drop-Down List in the IO-Link Profile.
This time, we sent a Read command to Port 4, Index 16, SubIndex 0.The string “Contrinex AG” is returned in the Read-String.
In fact, you can check the Index, SubIndex, Data Type and Default value of each Index by checking the IO-LINK Device Manual of each company.
How to check the AMS Net-ID?
Open EL6224 and from >AoE-Onlne ,there is a field called NetId.
This number is the AMS Net-ID.
Add library
Add Tc3_IoLink library by Reference>Add Library.
Search for Tc3_IoLink and add Library with Ok.
DUT
Here are the DUTs used in this article.
E_IolPort
Specifies the port number.
Port1 | 1 |
Port2 | 2 |
Port3 | 3 |
Port4 | 4 |
Port5 | 5 |
Port6 | 6 |
Port7 | 7 |
Port8 | 9 |
E_IolPortError
This is the error content of IO-Link communication.
NoError | 0 |
WatchdogDetected | 1 |
InternalError | 2 |
InvalidDeviceID | 3 |
InvalidVendorID | 4 |
InvalidIOLinkVersion | 5 |
InvalidFrameCapability | 6 |
InvalidCycletime | 7 |
InvalidPdInLength | 8 |
InvalidPdOutLenght | 9 |
NoDeviceDetected | 10 |
E_IolPortState
This is the communication information of IO Link Port.
Disabled | 0 |
stDigIn | 1 |
stDigOut | 2 |
CommunicationOP | 3 |
CommunicationComstop | 4 |
Function
Here are the Functions used in this article.
F_IolGetChannelStateTxt
This function converts the IO-LINK Port status into text format.
VAR_INPUT
Variables | Type | Description |
nIolChState | USINT | IO-LINK channel status value |
nPort | E_IolPort | Port number of the IO-LINK whose status you want to retrieve |
VAR_OUTPUT
Variables | Type | Description |
F_IolGetChannelStateTxt | String(255) | Convert channel status into text information. |
F_IolGetPortError
This function returns the error status of the IO-Link Port.
VAR_INPUT
Variables | Type | Description |
nIolChState | USINT | IO-LINK channel status value |
VAR_OUTPUT
Variables | Type | Description |
F_IolGetPortError | E_IolPortError | Indicates IO Link Port error information |
F_IolGetPortState
This function returns the status of the IO-Link Port.
VAR_INPUT
Variables | Type | Description |
nIolChState | USINT | IO-LINK channel status value |
VAR_OUPUT
Variables | Type | Description |
E_IoLGetPortState | E_IolPortState | Indicates Port status |
Function Block
Here is the Function Block used in this article.
FB_IolRead
This Function Block reads the parameters of the IO-link device.
VAR_INPUT
Variable | Type | Description |
bExecute | BOOL | Run at startup |
sNetid | T-AmsNetid | IO Link Master’s AMS NET-ID |
nlolPort | E_lolPort | IO-Link Port number you wish to access |
nIndex | WORD | Index of the IO-Link device you want to access |
nSubindex | BYTE | SubIndex of the IO-Link device you want to access |
pDSTBuf | PVOID | Memory Offset where data read from the IoLink device is stored. |
cbBufLen | UDINT | Total Size of the storage location for data read from the IoLink device |
tTimeout | TIME | Maximum execution time |
VAR_OUTPUT
Variable | Type | Description |
bBusy | BOOL | True=Executing |
bDone | BOOL | True=Execution succeeded |
bError | BOOL | True=Error exists |
hResult | HRESULT | Return value of Function Block |
nADSError | E_AdsErr | ADS Error Code |
nIolError | E_IolError | IO-Link error code |
FB_IolWrite
This Function Block writes the parameters of the IO-link device.
VAR_INPUT
Variable | Type | Description |
bExecute | BOOL | Run at startup |
sNetid | T-AmsNetid | IO Link Master’s AMS NET-ID |
nlolPort | E_lolPort | IO-Link Port number you wish to access |
nIndex | WORD | Index of the IO-Link device you want to access |
nSubindex | BYTE | SubIndex of the IO-Link device you want to access |
pSRCBuf | PVOID | Memory Offset of the destination for data to be written to the IoLink device. |
cbBufLen | UDINT | Total Size of the destination for data to be written to the IoLink device |
tTimeout | TIME | Maximum execution time |
VAR_OUTPUT
Variable | Type | Description |
bBusy | BOOL | True=Executing |
bDone | BOOL | True=Execution succeeded |
bError | BOOL | True=Error exists |
hResult | HRESULT | Return value of Function Block |
nADSError | E_AdsErr | ADS Error Code |
nIolError | E_IolError | IO-Link error code |
Implementation1-F_IolGetChannelStateTxt
F_IolGetChannelStateTxt Function is used to take the current Port state.
Program
The StateCh4 variable should actually be linked to the State variable of the EL6224.
VAR StateCh4 AT %I*:USINT; nPort :E_IolPort:=E_IolPort.Port4; sChannelStateTxt :STRING; END_VAR //F_IolGetChannelStateTxt sChannelStateTxt:=F_IolGetChannelStateTxt( nIolChState:=StateCh4 ,nPort:=nPort ); |
Result
When the IOLINK device is communicating normally, the text “Port4 is state CommunicationOP ” will return.
However, when I unplug the cable that is connected to the IO LINK device, the text changes to “Port 4 is state Disabled and has error NoDevice Detected”.
Implementation2-F_IolGetPortError
F_IolGetPortError Function is used to take the error status of the current Port.
Program
The StateCh4 variable should actually be linked to the State variable of the EL6224.
VAR StateCh4 AT %I*:USINT; sPortError :E_IolPortError END_VAR //F_IolGetPortError sPortError:=F_IolGetPortError( nIolChState:=StateCh4 ); |
Result
NoErrorOP’ returns when the IO LINK device is communicating normally.
However, when I unplug the cable connected to the IO LINK device, it changes to “NoDevice”.
Implementation3-F_IolGetPortState
F_IolGetPortState Function is used to take the current Port state.
Program
The StateCh4 variable should actually be linked to the State variable of the EL6224.
VAR StateCh4 AT %I*:USINT; sPortState :STRING; END_VAR //F_IolGetPortState sPortState:=F_IolGetPortState( nIolChState:=StateCh4 ); |
Result
When the IO LINK device is communicating normally, “CommunicationOP” will return.
However, when I disconnect the cable connected to the IO LINK device, it changes to “Disabled”.
Implementation4-FB_IolRead
We will use FB_IolRead Function Block to read the status of Index 16#10 and SubIndex 16#0 of the IO LINK device currently connected to the Port.
Program
VAR FB_IolRead :FB_IolRead; DSBuffer :STRING(255); nPort :E_IolPort:=E_IolPort.Port4; FB_IolRead_Execute :BOOL; END_VAR //FB_IolRead FB_IolRead.bExecute:=FB_IolRead_Execute; FB_IolRead.sNetId:=’169.254.141.157.2.6′; FB_IolRead.nIolPort:=nPort; FB_IolRead.pDSTBuf:=ADR(DSBuffer); FB_IolRead.cbBufLen:=SIZEOF(DSBuffer); FB_IolRead.nIndex:=16#10; FB_IolRead.nSubindex:=0; FB_IolRead(); |
Result
Done!Contrinex returned.
Implementation5-FB_IolWrite
we will use FB_IolWrite Function Block to change the current value of Index 16#40 and SubIndex 16#3 of the IO LINK devices currently connected to the Port.
Program
VAR FB_IolWrite:FB_IolWrite; FB_IolWrite_Execute:BOOL; SensorMode :USINT; nPort :E_IolPort:=E_IolPort.Port4 END_VAR SensorMode:=16#18; SrcBuff:=’Beckhoff’; FB_IolWrite.bExecute:=FB_IolWrite_Execute; FB_IolWrite.sNetId:=’169.254.141.157.2.6′; FB_IolWrite.nIolPort:=nPort; FB_IolWrite.pSRCBuf:=ADR(SensorMode); FB_IolWrite.cbBufLen:=sizeof(SensorMode); FB_IolWrite.nIndex:=16#40; FB_IolWrite.nSubindex:=3; FB_IolWrite(); |
Result
Done!Function Block was successfully executed without error.
Open EL6224 and check it from the AoE-Online screen.
Done! I was able to confirm the 18 I had just written.