TwinCAT3 实时核中ADS实现C++ server、clinet数据传输

一、基本概念

ADS :Automation Device Specification,ADS设备间进行通信的协议规范。协议定义了ADS device之间如何寻址对方、ADS device之间可以执行哪些操作、执行这些操作需要哪些参数,以及操作完成后如何返回结果等。从编程角度看,协议涵盖了AMS netID,port number(二者用于寻址ADS device)以及AdsReadWriteInd、AdsReadWriteRes、AdsReadWriteReq、AdsReadWriteCon、AdsReadReq……等一系列函数(用于在ADS device之间传输数据)。

ADS device:具备ADS接口的软件模块。 下图是从倍福官网拷贝的,从图中可以看出,所有的ADS device连接在ADS Router Engine上,那么通过ADS通讯时,是如何识别通讯的ADS device双方呢?这就涉及到后面提到的AMS了。

AMS :Automation Message Specification,指定了ADS数据的交互格式。从编程的角度,需要关心Ams NetId、Ams Prot,二者用于寻址通讯双方的 ADS device。

AMS NetId:默认的AMS NetId是本机IP地址后面加上.1.1,比如192.168.56.1.1.1。但是,AMS NetId与IP地址是没有任何关系的,可以通过下面步骤改成其他值。

AMS Port:通过AMS NetId只能找到对应的PC或控制器,PC或控制器中可能运行很多ADS device,所以还需要AMS Port识别具体的ADS device。AMS Port与linux、Windows中的端口一样,用于识别应用程序,每个应用程序分配有唯一的端口号,port的范围见参考资料4。

Route :通讯的server、client双方必须配置路由信息,路由包含路由名称、AmsNetId、对方的ip地址、路由类型。 TwinCAT 3中添加路由操作如下:

TwinCAT device:装有TwinCAT runtime的PC或控制器。

实时核:TwinCAT可以设置共享实时核、独占实时核,用于对运行时间要求严格的任务。

二、Server与Client通讯模型

与非实时ADS通讯不同,实时核中通信双方不能阻塞,需要采用异步的方式,TwinCAT提供的实时核通信接口见参考资料3。ADS通讯模型是Client-Server模式,客户端发起请求,服务端进行响应。

图中的Req()可以是AdsReadWriteReq、AdsReadReq、AdsWriteReq、AdsReadStateReq等等。
这些函数都有对应的Ind()、Res()、Con(),比如AdsReadWriteReq--AdsReadWriteInd--AdsReadWriteRes--AdsReadWriteCon等等,见参考资料3。

以Client读写Server端的变量为例,流程如下:

1)Client端申请一个AMS port,其他的ADS device(比如Server)可通过该port识别到该Client。

并且获取服务端的AMS NetId和AMS Port。

// State transition from PREOP to SAFEOP

HRESULT CClient::SetObjStatePS(PTComInitDataHdr pInitData)
{
    /*
     * Client端的端口范围是32768–65535,
     * m_AmsPort 的值不要与其它ADS device Client用的端口重复。
     */
	WORD m_AmsPort = 33275;
    AmsAddr m_Addr;

	HRESULT hr = S_OK;
	IMPLEMENT_ITCOMOBJECT_EVALUATE_INITDATA(pInitData);

	hr = SUCCEEDED(hr) ? InitAmsPort(m_spSrv, m_AmsPort) : hr;

	m_Addr.netId = AmsGetNetId(); //服务端AMS NetId
	m_Addr.port = 25100; //服务端AMS Port

	// cleanup on failure
	if (FAILED(hr)) {
		ShutdownAmsPort();
	}
	m_Trace.Log(tlVerbose, FLEAVEA "hr=0x%08x", hr);
	return hr;
}

2)Server端申请一个AMS Port,用于识别本ADS device。

// State transition from PREOP to SAFEOP

HRESULT CModule1::SetObjStatePS(PTComInitDataHdr pInitData)
{
    /*
     * Server端的端口范围是25000–25999,
     * m_AmsPort 的值不要与其它ADS device Server用的端口重复。
     */
	WORD m_AmsPort = 25100;
	m_Trace.Log(tlVerbose, FENTERA);
	HRESULT hr = S_OK;
	IMPLEMENT_ITCOMOBJECT_EVALUATE_INITDATA(pInitData);

	hr = SUCCEEDED(hr) ? InitAmsPort(m_spSrv, m_AmsPort) : hr;

	// cleanup on failure
	if (FAILED(hr)) {
		ShutdownAmsPort();
	}
	m_Trace.Log(tlVerbose, FLEAVEA "hr=0x%08x", hr);
	return hr;
}

3)与Client端需获取Server端的AMS NetId、Port不同,Server端不需要获取Clinet的这些信息。因为TwinCAT采用的是请求-响应的方式,请求发送至Server端时,是含有Client端的AMS NetId、Port这些信息的。

4)Client通过AdsReadWriteReq发送一个读写Server端的请求。

int nErr;
ULONG test_data;
ULONG InvokeId = 0x00000001;
ULONG IndexGroup = 0x08;
ULONG IndexOffset = 0x09;
ULONG cbReadLength = sizeof(test_data);
ULONG cbWriteLength = 0;

/*
 * m_Addr,服务端的AMS NetId、AMS Port
 * InvokeId是一个ULONG数值,用于代表Client端的这个请求。
 * 因为实时核ADS通讯不能阻塞,是异步的,所以Client端收到Server端的响应时,
 * 需要通过invoke id响应对应的是哪个请求。
 *
 * IndexGroup、IndexOffset用于识别具体的命令。比如服务端识别出0x08、0x09后,
 * 就将数值拷贝到test_data中。
 * cbReadLength,读数据的长度,本例中是都一个ULONG类型的数值,读到test_data
 * 变量中。
 * cbWriteLength,写数据的长度,本例中只读不写,该参数可忽略。
nErr = AdsReadWriteReq(m_Addr, InvokeId, IndexGroup, IndexOffset, cbReadLength, cbWriteLength, & test_data);

5)Server端收到Client的请求,自动触发AdsReadWriteInd函数执行。

AdsReadWriteInd通过解析参数indexGroup、indexOffset,执行对应的处理操作,填充Client端请求的数据。还有重要的一点,需要执行AdsReadWriteRes(rAddr, invokeId, ADSERR_NOERR, cbReadLength, pData)把响应发送给Client端。

enum Module1IndexGroups : ULONG
{
	Module1IndexGroup1 = 0x00000001,
	Module1IndexGroup2 = 0x00000002,
	IG_OVERWRITE = 0x00000003,
	ServerIndexGroup8 = 0x00000008
};

enum Module1IndexOffsets : ULONG
{
	Module1IndexOffset1 = 0x00000001,
	Module1IndexOffset2 = 0x00000002,
	ServerIndexGroup9 = 0x00000009
};

void CModule1::AdsReadWriteInd
(
	AmsAddr& rAddr,
	ULONG invokeId,
	ULONG indexGroup,
	ULONG indexOffset,
	ULONG cbReadLength,
	ULONG cbWriteLength,
	PVOID pData
)
{
	m_Trace.Log(tlVerbose, FENTERA "oid=0x%08x, invokeId=%d, indexGroup=0x%08x, indexOffset=0x%08x, cbReadLength=%d, cbWriteLength=%d, pData=0x%p",
		m_objId.value, invokeId, indexGroup, indexOffset, cbReadLength, cbWriteLength, pData);

	switch(indexGroup)
	{
	case ServerIndexGroup8:
		switch (indexOffset)
		{
		case ServerIndexGroup9:
			// TODO: add custom code here     // override counter with value provided by ADS-client
			unsigned long* pCounter = (unsigned long*)pData;
			//m_Counter = *pCounter;
			*pCounter = 8234;
			AdsReadWriteRes(rAddr, invokeId, ADSERR_NOERR, cbReadLength, pData);
			break;
		}
		break;
	default:
		__super::AdsReadWriteInd(rAddr, invokeId, indexGroup, indexOffset, cbReadLength, cbWriteLength, pData); 
		break;
	}
	m_Trace.Log(tlVerbose, FLEAVEA);
}

7)Client收到Server端的响应,自动触发AdsReadWriteCon函数执行。

函数的参数@cbLength,服务端响应数据的长度。
                  @pData,响应数据的起始地址。

void  CClient::AdsReadWriteCon(AmsAddr& rAddr, ULONG invokeId, ULONG nResult, ULONG cbLength, PVOID pData)
{
	if (nResult == S_OK && invokeId == ClientIndexGroup1) {
		m_bCount_client = *(int*)pData;
		m_Trace.Log(tlAlways, FNAMEA "AdsReadWrite for ads-variable getHdl got invokeid=0x%08x and nresult=0x%08x", invokeId, nResult);
	} else {
		m_Trace.Log(tlAlways, FNAMEA "AdsReadWrite for ads-variable getHdl failed nresult=0x%08x - retrying", nResult);
	}
}

三、参考资料

1,ADS、AMS、Router官网说明

 手册左侧:TwinCAT 3 --> Technologies --> ADS --> AmsNAT --> Introduction 

ADS (Automation Device Specification) is the TwinCAT communication protocol that specifies the interaction between two ADS devices. For example, it defines what operations can be executed on another ADS device, what parameters are necessary for that and what return value is sent after execution.

AMS (Automation Message Specification) specifies the exchange of the ADS data. A major component of the communication protocol is the AmsNetId. This is specified in the AMS/ADS package for the source and target device. An ADS device can be explicitly addressed using the AmsNetId.

route between two devices must be setup in TwinCAT so that they can communicate. This route is configured on both sides and typically contains the route name, the AmsNetId and the address of the communication partner as well as the type of connection. The configuration of new routes and an overview of existing routes in a TwinCAT system are shown in the following figure.

2, ADS device官网说明

手册左侧:TwinCAT 3 --> Technologies --> ADS --> ADS Basics --> ADS device concept 

The TwinCAT system architecture allows the individual modules of the software (e.g. TwinCAT PLC, User HMI, ...) to be treated as independent devices: For every task there is a software module ("Server" or "Client"). The servers in the system are the executing working "devices" in the form of software, whose operating behaviour is exactly like that of a hardware device. For this reason we can speak of "virtual" devices implemented in the software. The "clients" are programs which request the services of the "servers", e.g. a visualisation, or even a "programming device" in the form of a program. It is thus possible for TwinCAT to grow, since there can always be new servers and clients for tasks such as camshaft controllers, oscilloscopes, PID controllers etc. 

3,ADS实时核函数官网说明

手册左侧:TwinCAT 3 --> TE1000 XAE --> C/C++ --> Programming Reference  --> ADS Communication

4,AMS Port官网说明

手册左侧:TwinCAT 3 --> Technologies --> ADS --> ADS Basics --> ADS device indentification

The unique identification of ADS devices is implemented with the aid of two identifiers:

  • PortNr
  • NetId
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值