EtherCAT主站SOEM函数详解----SDO读写函数
本篇ethercatcoe文件相关的函数,COE的全称是CanOpen over EtherCAT, 采用CanOpen的相关协议开发了EtherCAT的运用层,核心是对象字典,想了解PDO相关知识,可以读CanOpen通信----PDO与SDO。对于报文结构体相关知识,可以参照EtherCAT数据帧及SOEM报文数据结构介绍,对下文的理解有一定帮组。
ethercatcoe.h头文件
为了比较全面的了解COE, 本文将先给出ethercatcoe.h的头文件,如下所示,其中核心的是读写函数。
/*
* Licensed under the GNU General Public License version 2 with exceptions. See
* LICENSE file in the project root for full license information
*/
/** \file
* \brief
* Headerfile for ethercatcoe.c
*/
#ifndef _ethercatcoe_
#define _ethercatcoe_
#ifdef __cplusplus
extern "C"
{
#endif
/** max entries in Object Description list */
#define EC_MAXODLIST 1024
/** max entries in Object Entry list */
#define EC_MAXOELIST 256
/* Storage for object description list */
typedef struct
{
/** slave number */
uint16 Slave;
/** number of entries in list */
uint16 Entries;
/** array of indexes */
uint16 Index[EC_MAXODLIST];
/** array of datatypes, see EtherCAT specification */
uint16 DataType[EC_MAXODLIST];
/** array of object codes, see EtherCAT specification */
uint8 ObjectCode[EC_MAXODLIST];
/** number of subindexes for each index */
uint8 MaxSub[EC_MAXODLIST];
/** textual description of each index */
char Name[EC_MAXODLIST][EC_MAXNAME+1];
} ec_ODlistt;
/* storage for object list entry information */
typedef struct
{
/** number of entries in list */
uint16 Entries;
/** array of value infos, see EtherCAT specification */
uint8 ValueInfo[EC_MAXOELIST];
/** array of value infos, see EtherCAT specification */
uint16 DataType[EC_MAXOELIST];
/** array of bit lengths, see EtherCAT specification */
uint16 BitLength[EC_MAXOELIST];
/** array of object access bits, see EtherCAT specification */
uint16 ObjAccess[EC_MAXOELIST];
/** textual description of each index */
char Name[EC_MAXOELIST][EC_MAXNAME+1];
} ec_OElistt;
#ifdef EC_VER1
void ec_SDOerror(uint16 Slave, uint16 Index, uint8 SubIdx, int32 AbortCode);
int ec_SDOread(uint16 slave, uint16 index, uint8 subindex,
boolean CA, int *psize, void *p, int timeout);
int ec_SDOwrite(uint16 Slave, uint16 Index, uint8 SubIndex,
boolean CA, int psize, void *p, int Timeout);
int ec_RxPDO(uint16 Slave, uint16 RxPDOnumber , int psize, void *p);
int ec_TxPDO(uint16 slave, uint16 TxPDOnumber , int *psize, void *p, int timeout);
int ec_readPDOmap(uint16 Slave, int *Osize, int *Isize);
int ec_readPDOmapCA(uint16 Slave, int Thread_n, int *Osize, int *Isize);
int ec_readODlist(uint16 Slave, ec_ODlistt *pODlist);
int ec_readODdescription(uint16 Item, ec_ODlistt *pODlist);
int ec_readOEsingle(uint16 Item, uint8 SubI, ec_ODlistt *pODlist, ec_OElistt *pOElist);
int ec_readOE(uint16 Item, ec_ODlistt *pODlist, ec_OElistt *pOElist);
#endif
void ecx_SDOerror(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIdx, int32 AbortCode);
int ecx_SDOread(ecx_contextt *context, uint16 slave, uint16 index, uint8 subindex,
boolean CA, int *psize, void *p, int timeout);
int ecx_SDOwrite(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIndex,
boolean CA, int psize, void *p, int Timeout);
int ecx_RxPDO(ecx_contextt *context, uint16 Slave, uint16 RxPDOnumber , int psize, void *p);
int ecx_TxPDO(ecx_contextt *context, uint16 slave, uint16 TxPDOnumber , int *psize, void *p, int timeout);
int ecx_readPDOmap(ecx_contextt *context, uint16 Slave, int *Osize, int *Isize);
int ecx_readPDOmapCA(ecx_contextt *context, uint16 Slave, int Thread_n, int *Osize, int *Isize);
int ecx_readODlist(ecx_contextt *context, uint16 Slave, ec_ODlistt *pODlist);
int ecx_readODdescription(ecx_contextt *context, uint16 Item, ec_ODlistt *pODlist);
int ecx_readOEsingle(ecx_contextt *context, uint16 Item, uint8 SubI, ec_ODlistt *pODlist, ec_OElistt *pOElist);
int ecx_readOE(ecx_contextt *context, uint16 Item, ec_ODlistt *pODlist, ec_OElistt *pOElist);
#ifdef __cplusplus
}
#endif
#endif
SDO读写函数
SDO服务数据,也称为邮箱数据,通常用在驱动器配置阶段,其数据传输是非周期性的。
SDO读取函数
int ecx_SDOread(ecx_contextt *context, uint16 slave, uint16 index, uint8 subindex,
boolean CA, int *psize, void *p, int timeout);
/** CoE SDO read, blocking. Single subindex or Complete Access.
*
* Only a "normal" upload request is issued. If the requested parameter is <= 4bytes
* then a "expedited" response is returned, otherwise a "normal" response. If a "normal"
* response is larger than the mailbox size then the response is segmented. The function
* will combine all segments and copy them to the parameter buffer.
*
* @param[in] context = context struct
* @param[in] slave = Slave number
* @param[in] index = Index to read
* @param[in] subindex = Subindex to read, must be 0 or 1 if CA is used.
* @param[in] CA = FALSE = single subindex. TRUE = Complete Access, all subindexes read.
* @param[in,out] psize = Size in bytes of parameter buffer, returns bytes read from SDO.
* @param[out] p = Pointer to parameter buffer
* @param[in] timeout = Timeout in us, standard is EC_TIMEOUTRXM
* @return Workcounter from last slave response
*/
参数解释:
- ecx_contextt *context:报文结构体指针,所有信息都是通过报文传递的,报文中包含了一系列配置信息;
- uint16 slave :从站编号,有顺序寻址、逻辑寻址等,采用顺序寻址在驱动器配置过程自动扫描分配,与连接顺序有关;
- uint16 index:对象字典索引,CanOpen相关协议
- uint8 subindex:对象字典子索引,CanOpen相关协议
- boolean CA:完全访问(Complete Access),会访问所有子索引,一般填写False,仅访问给定子索引。
- int *psize: 读取参数字节数
- void *p:读取参数指针,通过该指针可获取想要的信息
- int timeout:读取最大超时,单位us(微秒),一般给EC_TIMEOUTRXM,其值为700000
- @return Workcounter:返回计数wck,用于对比是否正确处理报文。
SDO写函数
int ecx_SDOwrite(ecx_contextt *context, uint16 Slave, uint16 Index, uint8 SubIndex,
boolean CA, int psize, void *p, int Timeout);
/** CoE SDO write, blocking. Single subindex or Complete Access.
*
* A "normal" download request is issued, unless we have
* small data, then a "expedited" transfer is used. If the parameter is larger than
* the mailbox size then the download is segmented. The function will split the
* parameter data in segments and send them to the slave one by one.
*
* @param[in] context = context struct
* @param[in] Slave = Slave number
* @param[in] Index = Index to write
* @param[in] SubIndex = Subindex to write, must be 0 or 1 if CA is used.
* @param[in] CA = FALSE = single subindex. TRUE = Complete Access, all subindexes written.
* @param[in] psize = Size in bytes of parameter buffer.
* @param[out] p = Pointer to parameter buffer
* @param[in] Timeout = Timeout in us, standard is EC_TIMEOUTRXM
* @return Workcounter from last slave response
参数解释:
- ecx_contextt *context:报文结构体指针,所有信息都是通过报文传递的,报文中包含了一系列配置信息;
- uint16 slave :从站编号,有顺序寻址、逻辑寻址等,采用顺序寻址在驱动器配置过程自动扫描分配,与连接顺序有关;
- uint16 index:对象字典索引,CanOpen相关协议
- uint8 subindex:对象字典子索引,CanOpen相关协议
- boolean CA:完全访问(Complete Access),会访问所有子索引,一般填写False,仅访问给定子索引。
- int psize: 需要写入参数字节数
- void *p:写入参数指针,通过该指针可获修改对应从站数据
- int timeout:写入最大超时,单位us(微秒),一般给EC_TIMEOUTRXM,其值为700000
- @return Workcounter:返回计数wck,用于对比是否正确处理报文。