TwinCAT3中通过PLC修改Coe参数的例程

发布时间 2023-08-04 10:25:11作者: 一杯清酒邀明月

CoE 接口的驱动器,要在 PLC 程序中修改驱动器参数,可以使用 CoeSDO 通讯的方式 。

CoeSDO 通 讯 的 功 能 块 包 括 FB_EcCoeSdoRead , FB_EcCoeSdoWrite ,FB_EcCoeSdoReadEx , FB_EcCoeSdoWriteEx 等 , 属 于 库 文 件 TcEtherCAT.Lib 。

以FB_EcCoeSdoWriteEx 为例。首先导入Tc2_EtherCAT.Lib库文件。

以修改8010:01最大电流为800mA为例。

Coe子模块:

 1 FUNCTION_BLOCK CoeSet
 2 VAR_INPUT
 3     bExecute   : BOOL; (* rising edge starts writing to the CoE Object *) 
 4 END_VAR
 5 
 6 VAR_OUTPUT
 7     bError     : BOOL;    
 8     nErrId     : UDINT;
 9 END_VAR
10 
11 VAR
12     fbSdoWrite             : FB_EcCoESdoWrite;    
13     sNetId                 : T_AmsNetId := '169.254.208.225.3.1'; (* NetId of EtherCAT Master *)
14     
15     nSlaveAddr_1         : UINT := 1002; (* Port Number of EtherCAT Slave *)    
16     nMaxCurrent             : WORD := 16#8010; (* CoE Object Index *) 
17     nMaxCurrentSubIndex     : BYTE := 1; (* Subindex of CoE Object *)    
18     valueMaxCurrent_1         : UINT := 800; (* variable to be written to the CoE Object *)   //注意数据类型,在Coe中具体查看
19 END_VAR
 1 fbSdoWrite(
 2     sNetId     := sNetId,
 3     nSlaveAddr := nSlaveAddr_1,
 4     nIndex     := nMaxCurrent,
 5     nSubIndex  := nMaxCurrentSubIndex,
 6     pSrcBuf    := ADR(valueMaxCurrent_1),
 7     cbBufLen   := SIZEOF(valueMaxCurrent_1),
 8     bExecute   := bExecute
 9 );
10 
11 
12 IF NOT fbSdoWrite.bBusy THEN
13     bExecute := FALSE;
14     IF NOT bError THEN 
15         (* write successful *)
16         bError := FALSE;
17         nErrId := 0;
18     ELSE 
19         (* write failed *)
20         bError := fbSdoWrite.bError;
21         nErrId := fbSdoWrite.nErrId;
22     END_IF
23 
24     fbSdoWrite(bExecute := FALSE);
25 END_IF

主程序:

1 PROGRAM MAIN
2 VAR
3     coe:CoeSet;
4     bExcuteCOE  : BOOL;
5     bResError    : BOOL;    
6     nResErrId   : UDINT;
7 END_VAR
coe(bExecute:=bExcuteCOE,bError=>bResError,nErrId=>nResErrId);

sNetId: EtherCAT 主站卡的 NetID,字符串,如下图。

 nSlaveAddr: 要写参数的 EtherCAT 节点地址。如下图中的 EtherCAT Addr:1002。

nSubIndex 若没有时,置为空。

TwinCAT接入第三方伺服驱动器同样适用(松下验证过)。