临沂哪家做网站最好,邹城网站定制,怎样在百度上发帖子,网站排行榜前十名一、基本概念
ADS #xff1a;Automation Device Specification#xff0c;ADS设备间进行通信的协议规范。协议定义了ADS device之间如何寻址对方、ADS device之间可以执行哪些操作、执行这些操作需要哪些参数#xff0c;以及操作完成后如何返回结果等。从编程角度看#…一、基本概念
ADS Automation Device SpecificationADS设备间进行通信的协议规范。协议定义了ADS device之间如何寻址对方、ADS device之间可以执行哪些操作、执行这些操作需要哪些参数以及操作完成后如何返回结果等。从编程角度看协议涵盖了AMS netIDport 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端的变量为例流程如下
1Client端申请一个AMS port其他的ADS device比如Server可通过该port识别到该Client。
并且获取服务端的AMS NetId和AMS Port。
// State transition from PREOP to SAFEOPHRESULT 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 NetIdm_Addr.port 25100; //服务端AMS Port// cleanup on failureif (FAILED(hr)) {ShutdownAmsPort();}m_Trace.Log(tlVerbose, FLEAVEA hr0x%08x, hr);return hr;
}
2Server端申请一个AMS Port用于识别本ADS device。
// State transition from PREOP to SAFEOPHRESULT 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 failureif (FAILED(hr)) {ShutdownAmsPort();}m_Trace.Log(tlVerbose, FLEAVEA hr0x%08x, hr);return hr;
}
3与Client端需获取Server端的AMS NetId、Port不同Server端不需要获取Clinet的这些信息。因为TwinCAT采用的是请求-响应的方式请求发送至Server端时是含有Client端的AMS NetId、Port这些信息的。
4Client通过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);
5Server端收到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 oid0x%08x, invokeId%d, indexGroup0x%08x, indexOffset0x%08x, cbReadLength%d, cbWriteLength%d, pData0x%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-clientunsigned 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);
}
7Client收到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 invokeid0x%08x and nresult0x%08x, invokeId, nResult);} else {m_Trace.Log(tlAlways, FNAMEA AdsReadWrite for ads-variable getHdl failed nresult0x%08x - retrying, nResult);}
} 三、参考资料
1ADS、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.
A 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.
3ADS实时核函数官网说明
手册左侧TwinCAT 3 -- TE1000 XAE -- C/C -- Programming Reference -- ADS Communication
AdsReadDeviceInfoAdsReadAdsWriteAdsReadWriteAdsReadStateAdsWriteControlAdsAddDeviceNotificationAdsDelDeviceNotificationAdsDeviceNotification
4AMS Port官网说明
手册左侧TwinCAT 3 -- Technologies -- ADS -- ADS Basics -- ADS device indentification
The unique identification of ADS devices is implemented with the aid of two identifiers:
PortNrNetId