TwinCAT-COE资源库使用

本文深入探讨了FB_EcCoESdoReadEx与FB_EcCoESdoWriteEx功能块的使用,详细解析了C++调用PLC功能块的方法,包括TcQueryInterface的实现细节与cycleUpdate()函数的应用。此外,还介绍了PLC资源库的综合调试过程,涵盖了从网络配置到状态获取的一系列操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. FB_EcCoESdoReadEx功能块

功能块位置

在这里插入图片描述

实验结果

在这里插入图片描述

2. FB_EcCoESdoWriteEx

功能块位置

在这里插入图片描述

实验结果

在这里插入图片描述

3. C++调用PLC功能块

相关代码(仅供参考)

PLC:TcQueryInterface
变量定义区:

{attribute 'c++_compatible'}
{attribute 'minimal_input_size' := '4'}
{attribute 'pack_mode' := '4'}
//{error '添加方法实现或删除属性以使用基本实现'}
METHOD TcQueryInterface : HRESULT
VAR_INPUT
	iid	: REFERENCE TO IID;
	pipItf	: POINTER TO PVOID;
	ipCalc: I_Calculation;
END_VAR

程序区:

IF GuidsEqual(pGuidA:= ADR(iid),
		      pGuidB:= ADR(TC_GLOBAL_IID_LIST.IID_I_Calculation)) THEN
	ipCalc := THIS^;
	pipItf^ := ITCUNKNOWN_TO_PVOID(ipCalc);
	TcAddRef();
	TcQueryInterface := S_OK;
ELSE
	TcQueryInterface := SUPER^.TcQueryInterface(iid,pipItf);
END_IF

C++: cycleUpdate()

	if ((m_spCalculation == NULL) && m_Inputs.oidProvider != 0) {
		m_spCalculation.SetOID(m_Inputs.oidProvider);
		m_spSrv->TcQuerySmartObjectInterface(m_spCalculation);
	}
	if (m_spCalculation != NULL) {
		m_spCalculation->Addition(m_Inputs.nln1,m_Inputs.nln2,m_Outputs.m_resAdd);
		m_spCalculation->Subtraction(m_Inputs.nln1, m_Inputs.nln2, m_Outputs.m_resSub);
	}

4. PLC资源库综合调试

变量声明区
PROGRAM MAIN
VAR
	netId:T_AmsNetId;
	SlaveAddrArray : ARRAY[1..2] OF UINT;
	getAllSlaveAddrExe,getAllSlaveStateExe,getMasterStateExe,getSlaveStateExe,reqMasterStateExe,setMasterStateExe,getAllSlaveAbnormalStateExe,getAllSyncUnitSalveAddrExe : BOOL;
	timeOut:TIME;
	slavStateArray: ARRAY[1..2] OF ST_EcSlaveState;
	slaveAbnormalStateArray : ARRAY [1..2] OF UDINT;
	allSyncUnitSlaveAddrArray : ARRAY [1..2] OF UINT;
	
	
	
	getAllSlaveAddr: FB_EcGetAllSlaveAddr;
	getAllSlaveStates: FB_EcGetAllSlaveStates;
	getMasterState: FB_EcGetMasterState;
	getSlaveState: FB_EcGetSlaveState;
	reqMasterState: FB_EcReqMasterState;
	setMasterState: FB_EcSetMasterState;
	
	
	getAllSlaveAbnormalState: FB_EcGetAllSlaveAbnormalStateChanges;
	
	getAllSyncUnitSalveAddr: FB_EcGetAllSyncUnitSlaveAddr;
	
	objID: OTCID;
END_VAR

程序区
netId := '192.168.5.221.3.1';
timeOut := T#5S;
objID := 16#03040020;
getAllSlaveAddr(
	sNetId:= netId, 
	pAddrBuf:= ADR(SlaveAddrArray), 
	cbBufLen:= SIZEOF(SlaveAddrArray), 
	bExecute:= getAllSlaveAddrExe, 
	tTimeout:= timeOut, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	nSlaves=> );
	
	
getAllSlaveStates(
	sNetId:= netId, 
	pStateBuf:= ADR(slavStateArray), 
	cbBufLen:= SIZEOF(slavStateArray), 
	bExecute:= getAllSlaveStateExe, 
	tTimeout:= timeOut, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	nSlaves=> );
	
	
	
getMasterState(
	sNetId:= netId, 
	bExecute:= getMasterStateExe, 
	tTimeout:= timeOut, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	state=> );
	
	
getSlaveState(
	sNetId:= netId, 
	nSlaveAddr:= SlaveAddrArray[1], 
	bExecute:= getSlaveStateExe, 
	tTimeout:= timeOut, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	state=> );	
	
	
reqMasterState(
	sNetId:= netId, 
	bExecute:= reqMasterStateExe, 
	tTimeout:= timeOut, 
	state:= EC_DEVICE_STATE_SAFEOP, 
	bBusy=> , 
	bError=> , 
	nErrId=> );
	
	
	
setMasterState(
	sNetId:= netId, 
	bExecute:= setMasterStateExe, 
	tTimeout:= timeOut, 
	reqState:= EC_DEVICE_STATE_OP, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	currState=> );
	
	
getAllSlaveAbnormalState(
	sNetId:=netId , 
	pAddrBuf:= ADR(slaveAbnormalStateArray), 
	cbBufLen:= SIZEOF(slaveAbnormalStateArray), 
	bExecute:= getAllSlaveAbnormalStateExe, 
	tTimeout:= timeOut, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	nSlaves=> );
	
	
getAllSyncUnitSalveAddr(
	sNetId:= netId, 
	nObjectId:= objID, 
	pAddrBuf:= ADR(allSyncUnitSlaveAddrArray), 
	cbBufLen:= SIZEOF(allSyncUnitSlaveAddrArray), 
	bExecute:= getAllSyncUnitSalveAddrExe, 
	tTimeout:= timeOut, 
	bBusy=> , 
	bError=> , 
	nErrId=> , 
	nSlaves=> );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值