get slave count

This commit is contained in:
Chinky 2024-01-14 23:08:53 +08:00
parent 5fbafd07ec
commit 5d2b04e7b3
8 changed files with 282 additions and 420 deletions

View File

@ -108,10 +108,10 @@ install(FILES
DESTINATION ${SOEM_INCLUDE_INSTALL_DIR})
if(BUILD_TESTS)
add_subdirectory(test/simple_ng)
add_subdirectory(test/linux/slaveinfo)
add_subdirectory(test/linux/eepromtool)
add_subdirectory(test/linux/simple_test)
# add_subdirectory(test/simple_ng)
add_subdirectory(test/linux/ecslaveinfo)
add_subdirectory(test/linux/eceeprom)
# add_subdirectory(test/linux/simple_test)
add_subdirectory(test/linux/tj30_test)
add_subdirectory(test/linux/red_test)
# add_subdirectory(test/linux/red_test)
endif()

View File

@ -0,0 +1,5 @@
set(SOURCES eceeprom.c)
add_executable(eceeprom ${SOURCES})
target_link_libraries(eceeprom soem)
install(TARGETS eceeprom DESTINATION bin)

View File

@ -25,18 +25,21 @@
#define MINBUF 128
#define CRCBUF 14
#define MODE_NONE 0
#define MODE_READBIN 1
#define MODE_READINTEL 2
#define MODE_WRITEBIN 3
#define MODE_WRITEINTEL 4
#define MODE_WRITEALIAS 5
#define MODE_WRITESN 6
#define MODE_INFO 7
#define MODE_NONE 0
#define MODE_READBIN 1
#define MODE_READINTEL 2
#define MODE_WRITEBIN 3
#define MODE_WRITEINTEL 4
#define MODE_WRITEALIAS 5
#define MODE_WRITESN 6
#define MODE_INFO 7
#define MODE_SLAVECOUNTTIP 8
#define MODE_SLAVECOUNT 9
#define MAXSLENGTH 256
uint8 ebuf[MAXBUF];
char IOmap[4096];
uint8 ob;
uint16 ow;
int os;
@ -164,11 +167,14 @@ int output_bin(char *fname, int length)
fp = fopen(fname, "wb");
if (fp == NULL)
{
printf("Can not write to file %s\n", fname);
return 0;
}
for (cc = 0; cc < length; cc++)
fputc(ebuf[cc], fp);
fclose(fp);
printf("EEPROM data save to file %s in binary format, size %d bytes\n", fname, length);
return 1;
}
@ -180,7 +186,10 @@ int output_intelhex(char *fname, int length)
fp = fopen(fname, "w");
if (fp == NULL)
{
printf("Can not write to file %s\n", fname);
return 0;
}
while (cc < length)
{
ll = length - cc;
@ -198,7 +207,7 @@ int output_intelhex(char *fname, int length)
}
fprintf(fp, ":00000001FF\n");
fclose(fp);
printf("EEPROM data save to file %s in Intel Hex format, size %d bytes\n", fname, length);
return 1;
}
@ -347,176 +356,267 @@ int eeprom_writesn(int slave, uint32_t sn)
return ret;
}
int eepromtool(char *ifname, int slave, int mode, char *fname)
int get_slavecount(const char *ifname)
{
int w, rc = 0, estart, esize;
uint16 *wbuf;
/* initialise SOEM, bind socket to ifname */
if (ec_init(ifname) <= 0)
{
return -1;
}
int w = 0x0000;
wkc = ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w, EC_TIMEOUTSAFE); /* detect number of slaves */
if (wkc > 0)
{
ec_slavecount = wkc;
return wkc;
}
return 0;
}
static int show_info(int slave)
{
tstart = osal_current_time();
eeprom_read(slave, 0x0000, MINBUF); // read first 128 bytes
uint16 crc = SIIcrc(&ebuf[0]);
uint16 *wbuf = (uint16 *)&ebuf[0];
printf("Slave %d data\n", slave);
printf(" PDI Control : 0x%4.4X(%u)\n", *(wbuf + 0x00), *(wbuf + 0x00));
printf(" PDI Config : 0x%4.4X(%u)\n", *(wbuf + 0x01), *(wbuf + 0x01));
printf(" Config Alias : 0x%4.4X(%u)\n", *(wbuf + 0x04), *(wbuf + 0x04));
printf(" Checksum : 0x%2.2X (%s)\n", *(wbuf + 0x07),
(*(wbuf + 0x07) == crc) ? "Match" : "NOT Match");
printf(" calculated : 0x%2.2X\n", crc);
printf(" Vendor ID : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x08), *(uint32 *)(wbuf + 0x08));
printf(" Product Code : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0A), *(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0C), *(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0E), *(uint32 *)(wbuf + 0x0E));
printf(" Mailbox Protocol : 0x%4.4X\n", *(wbuf + 0x1C));
int esize = (*(wbuf + 0x3E) + 1) * 128;
if (esize > MAXBUF)
esize = MAXBUF;
printf(" Size : 0x%4.4X = %d bytes\n", *(wbuf + 0x3E), esize);
printf(" Version : 0x%4.4X(%u)\n", *(wbuf + 0x3F), *(wbuf + 0x3F));
return esize;
}
int eepromtool(const char *ifname, int slave, int mode, char *fname)
{
int rc = 0, estart, esize;
/* initialise SOEM, bind socket to ifname */
if (ec_init(ifname))
rc = get_slavecount(ifname);
if (rc == -1)
{
printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
printf("No socket connection on %s\nExcecute as root\n", ifname);
return -1;
}
if (mode == MODE_SLAVECOUNT)
{
printf("%d\n", ec_slavecount);
goto __exit;
}
else
{
printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
printf("ec_init on %s succeeded.\n", ifname);
}
if (rc == 0)
{
printf("No slaves found!\n");
goto __exit;
}
printf("%d slaves found.\n", ec_slavecount);
if (mode == MODE_SLAVECOUNTTIP)
{
goto __exit;
}
if ((slave > ec_slavecount) || (slave <= 0))
{
printf("Slave number outside range.\n");
goto __exit;
}
w = 0x0000;
wkc = ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w,
EC_TIMEOUTSAFE); /* detect number of slaves */
if (wkc > 0)
switch (mode)
{
case MODE_INFO:
show_info(slave);
break;
case MODE_READBIN:
case MODE_READINTEL:
esize = show_info(slave);
if (esize > MINBUF)
eeprom_read(slave, MINBUF, esize - MINBUF); // read reminder
tend = osal_current_time();
osal_time_diff(&tstart, &tend, &tdif);
if (mode == MODE_READINTEL)
output_intelhex(fname, esize);
if (mode == MODE_READBIN)
output_bin(fname, esize);
printf("\nTotal EEPROM read time: %ldms\n", (tdif.usec + (tdif.sec * 1000000L)) / 1000);
break;
case MODE_WRITEBIN:
case MODE_WRITEINTEL:
estart = 0;
if (mode == MODE_WRITEINTEL)
rc = input_intelhex(fname, &estart, &esize);
if (mode == MODE_WRITEBIN)
rc = input_bin(fname, &esize);
if (rc > 0)
{
ec_slavecount = wkc;
uint16 *wbuf = (uint16 *)&ebuf[0];
printf("Slave %d\n", slave);
printf(" Vendor ID : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x08),
*(uint32 *)(wbuf + 0x08));
printf(" Product Code : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0A),
*(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0C),
*(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0E),
*(uint32 *)(wbuf + 0x0E));
printf("%d slaves found.\n", ec_slavecount);
if ((ec_slavecount >= slave) && (slave > 0))
printf("Busy");
fflush(stdout);
tstart = osal_current_time();
eeprom_write(slave, estart, esize);
tend = osal_current_time();
osal_time_diff(&tstart, &tend, &tdif);
printf("\nTotal EEPROM write time :%ldms\n",
(tdif.usec + (tdif.sec * 1000000L)) / 1000);
}
else
{
printf("Error reading file, abort.\n");
}
break;
case MODE_WRITEALIAS:
if (eeprom_read(slave, 0x0000, CRCBUF)) // read first 14 bytes
{
uint16 *wbuf = (uint16 *)&ebuf[0];
*(wbuf + 0x04) = alias;
rc = eeprom_writealias(slave, alias, SIIcrc(&ebuf[0]));
if (rc)
{
if ((mode == MODE_INFO) || (mode == MODE_READBIN) || (mode == MODE_READINTEL))
{
tstart = osal_current_time();
eeprom_read(slave, 0x0000, MINBUF); // read first 128 bytes
uint16 crc = SIIcrc(&ebuf[0]);
wbuf = (uint16 *)&ebuf[0];
printf("Slave %d data\n", slave);
printf(" PDI Control : 0x%4.4X(%u)\n", *(wbuf + 0x00), *(wbuf + 0x00));
printf(" PDI Config : 0x%4.4X(%u)\n", *(wbuf + 0x01), *(wbuf + 0x01));
printf(" Config Alias : 0x%4.4X(%u)\n", *(wbuf + 0x04), *(wbuf + 0x04));
printf(" Checksum : 0x%2.2X (%s)\n", *(wbuf + 0x07),
(*(wbuf + 0x07) == crc) ? "Match" : "NOT Match");
printf(" calculated : 0x%2.2X\n", crc);
printf(" Vendor ID : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x08),
*(uint32 *)(wbuf + 0x08));
printf(" Product Code : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0A),
*(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0C),
*(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0E),
*(uint32 *)(wbuf + 0x0E));
printf(" Mailbox Protocol : 0x%4.4X\n", *(wbuf + 0x1C));
esize = (*(wbuf + 0x3E) + 1) * 128;
if (esize > MAXBUF)
esize = MAXBUF;
printf(" Size : 0x%4.4X = %d bytes\n", *(wbuf + 0x3E), esize);
printf(" Version : 0x%4.4X(%u)\n", *(wbuf + 0x3F), *(wbuf + 0x3F));
}
if ((mode == MODE_READBIN) || (mode == MODE_READINTEL))
{
if (esize > MINBUF)
eeprom_read(slave, MINBUF, esize - MINBUF); // read reminder
tend = osal_current_time();
osal_time_diff(&tstart, &tend, &tdif);
if (mode == MODE_READINTEL)
output_intelhex(fname, esize);
if (mode == MODE_READBIN)
output_bin(fname, esize);
printf("\nTotal EEPROM read time: %ldms\n",
(tdif.usec + (tdif.sec * 1000000L)) / 1000);
}
if ((mode == MODE_WRITEBIN) || (mode == MODE_WRITEINTEL))
{
estart = 0;
if (mode == MODE_WRITEINTEL)
rc = input_intelhex(fname, &estart, &esize);
if (mode == MODE_WRITEBIN)
rc = input_bin(fname, &esize);
if (rc > 0)
{
wbuf = (uint16 *)&ebuf[0];
printf("Slave %d\n", slave);
printf(" Vendor ID : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x08),
*(uint32 *)(wbuf + 0x08));
printf(" Product Code : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0A),
*(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0C),
*(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0E),
*(uint32 *)(wbuf + 0x0E));
printf("Busy");
fflush(stdout);
tstart = osal_current_time();
eeprom_write(slave, estart, esize);
tend = osal_current_time();
osal_time_diff(&tstart, &tend, &tdif);
printf("\nTotal EEPROM write time :%ldms\n",
(tdif.usec + (tdif.sec * 1000000L)) / 1000);
}
else
printf("Error reading file, abort.\n");
}
if (mode == MODE_WRITEALIAS)
{
if (eeprom_read(slave, 0x0000, CRCBUF)) // read first 14 bytes
{
wbuf = (uint16 *)&ebuf[0];
*(wbuf + 0x04) = alias;
rc = eeprom_writealias(slave, alias, SIIcrc(&ebuf[0]));
if (rc)
{
printf("Alias %d (HEX: %4.4X) written successfully to slave %d\n",
alias, alias, slave);
}
else
{
printf("Alias not written\n");
}
}
else
{
printf("Could not read slave EEPROM");
}
}
if (mode == MODE_WRITESN)
{
rc = eeprom_writesn(slave, serial_no);
if (rc)
{
printf("SerialNo %u (HEX: %8.8X) written successfully to slave %d\n",
serial_no, serial_no, slave);
}
else
{
printf("SerialNo not written\n");
}
}
printf("Alias %d (HEX: %4.4X) written successfully to slave %d\n", alias, alias,
slave);
}
else
{
printf("Slave number outside range.\n");
printf("Alias not written\n");
}
}
else
{
printf("No slaves found!\n");
printf("Could not read slave EEPROM");
}
break;
case MODE_WRITESN:
rc = eeprom_writesn(slave, serial_no);
if (rc)
{
printf("SerialNo %u (HEX: %8.8X) written successfully to slave %d\n", serial_no,
serial_no, slave);
}
else
{
printf("SerialNo not written\n");
}
break;
default:
break;
}
__exit:
if (mode != MODE_SLAVECOUNT)
printf("End, close socket\n");
/* stop SOEM, close socket */
ec_close();
}
else
{
printf("No socket connection on %s\nExcecute as root\n", ifname);
}
/* stop SOEM, close socket */
ec_close();
return rc;
}
void show_help(void)
{
ec_adaptert *adapter = NULL;
printf("Usage: eceeprom ifname [slaveID] OPTION fname|alias|serial_no\n");
printf("ifname = name of the network interface, e.g., eth0\n");
printf("slaveID = slave number in EtherCAT order (1..n)\n");
printf("Options:\n");
printf("(no option) Display slaves count\n");
printf(" -c Display slaves count (Only number)\n");
printf(" -i Display EEPROM information\n");
printf(" -wa Write slave alias in decimal format\n");
printf(" -wah Write slave alias in hexadecimal format\n");
printf(" -wsn Write serial number in decimal format\n");
printf(" -wsnh Write serial number in hexadecimal format\n");
printf(" -r Read EEPROM and output in binary format\n");
printf(" -ri Read EEPROM and output in Intel Hex format\n");
printf(" -w Write EEPROM using binary input format\n");
printf(" -wi Write EEPROM using Intel Hex input format\n");
printf("Exaple:\n");
printf(" eceeprom eth0 -- Display slaves count\n");
printf(" eceeprom eth0 -c -- Display slaves count (Only number)\n");
printf(" eceeprom eth0 2 -wa 12345 -- Write slave 2 alias '12345'\n");
printf("\nAvailable adapters:\n");
adapter = ec_find_adapters();
while (adapter != NULL)
{
printf(" - %s (%s)\n", adapter->name, adapter->desc);
adapter = adapter->next;
}
ec_free_adapters(adapter);
}
int main(int argc, char *argv[])
{
int rc = 1;
printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
mode = MODE_NONE;
if (argc > 3)
int rc = 0;
mode = MODE_NONE;
char *arg3 = NULL;
char *arg4 = NULL;
int slave = 0;
switch (argc)
{
slave = atoi(argv[2]);
char *arg3 = argv[3];
char *arg4 = (argc > 4) ? argv[4] : NULL;
case 2:
mode = MODE_SLAVECOUNTTIP;
break;
case 3:
if (strcmp(argv[2], "-c") == 0)
mode = MODE_SLAVECOUNT;
else
{
mode = MODE_INFO;
slave = atoi(argv[2]);
}
break;
case 4:
arg3 = argv[3];
if (strcmp(arg3, "-i") == 0)
mode = MODE_INFO;
else if (strcmp(arg3, "-r") == 0)
{
mode = MODE_INFO;
slave = atoi(argv[2]);
}
else
{
goto __help;
}
break;
case 5:
slave = atoi(argv[2]);
arg4 = argv[4];
arg3 = argv[3];
if (strcmp(arg3, "-r") == 0)
mode = MODE_READBIN;
else if (strcmp(arg3, "-ri") == 0)
mode = MODE_READINTEL;
@ -534,39 +634,17 @@ int main(int argc, char *argv[])
mode = MODE_WRITESN;
serial_no = strtoul(arg4, NULL, (strcmp(arg3, "-wsn") == 0) ? 10 : 16);
}
/* start tool */
rc = !eepromtool(argv[1], slave, mode, arg4);
else
goto __help;
break;
default:
goto __help;
}
else
{
ec_adaptert *adapter = NULL;
printf("Usage: eepromtool ifname slave OPTION fname|alias|serial_no\n");
printf("ifname = name of the network interface, e.g., eth0\n");
printf("slave = slave number in EtherCAT order (1..n)\n");
printf("Options:\n");
printf(" -i Display EEPROM information\n");
printf(" -wa Write slave alias in decimal format\n");
printf(" -wah Write slave alias in hexadecimal format\n");
printf(" -wsn Write serial number in decimal format\n");
printf(" -wsnh Write serial number in hexadecimal format\n");
printf(" -r Read EEPROM and output in binary format\n");
printf(" -ri Read EEPROM and output in Intel Hex format\n");
printf(" -w Write EEPROM using binary input format\n");
printf(" -wi Write EEPROM using Intel Hex input format\n");
printf("\nAvailable adapters:\n");
adapter = ec_find_adapters();
while (adapter != NULL)
{
printf(" - %s (%s)\n", adapter->name, adapter->desc);
adapter = adapter->next;
}
ec_free_adapters(adapter);
}
printf("End program\n");
/* start tool */
rc = !eepromtool(argv[1], slave, mode, arg4);
return rc;
__help:
show_help();
return -1;
}

View File

@ -0,0 +1,5 @@
set(SOURCES ecslaveinfo.c)
add_executable(ecslaveinfo ${SOURCES})
target_link_libraries(ecslaveinfo soem)
install(TARGETS ecslaveinfo DESTINATION bin)

View File

@ -711,7 +711,7 @@ int main(int argc, char *argv[])
}
else
{
printf("Usage: slaveinfo ifname [options]\nifname = eth0 for example\nOptions :\n -sdo : print SDO info\n -map : print mapping\n");
printf("Usage: ecslaveinfo ifname [options]\nifname = eth0 for example\nOptions :\n -sdo : print SDO info\n -map : print mapping\n");
printf ("Available adapters\n");
adapter = ec_find_adapters ();

View File

@ -1,5 +0,0 @@
set(SOURCES eepromtool.c)
add_executable(eepromtool ${SOURCES})
target_link_libraries(eepromtool soem)
install(TARGETS eepromtool DESTINATION bin)

View File

@ -1,5 +0,0 @@
set(SOURCES slaveinfo.c)
add_executable(slaveinfo ${SOURCES})
target_link_libraries(slaveinfo soem)
install(TARGETS slaveinfo DESTINATION bin)

View File

@ -1,216 +0,0 @@
/** \file
* \brief Example code for Simple Open EtherCAT master
*
* Usage : tj30_test [ifname1]
* ifname is NIC interface, f.e. eth0
*
* This is a minimal test.
*
* (c)Arthur Ketels 2010 - 2011
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "ethercat.h"
#define EC_TIMEOUTMON 500
char IOmap[4096]; // 个人理解 这里就是给提供了一块内存空间
OSAL_THREAD_HANDLE thread1; // demo 里面的一个 检测线程
OSAL_THREAD_HANDLE thread_freash;// 笔者自己加的 每5ms 就去刷新一下pdo 数据
int expectedWKC;
volatile int wkc;
boolean inOP;
uint8 currentgroup = 0;
// 状态监测线程主要是监测ethcat 的异常状态?
// 这个线程笔者并没去深究,大概的用途就是去 定时的检测当前从站的在线状态,及时发现从站掉线
OSAL_THREAD_FUNC ecatcheck( void *ptr )
{
int slave;
(void)ptr; /* Not used */
while(1)
{
if( inOP && ((wkc < expectedWKC) || ec_group[currentgroup].docheckstate))
{
/* one ore more slaves are not responding */
ec_group[currentgroup].docheckstate = FALSE;
ec_readstate();// 读取从站状态
for (slave = 1; slave <= ec_slavecount; slave++) // 遍历
{
if ((ec_slave[slave].group == currentgroup) && (ec_slave[slave].state != EC_STATE_OPERATIONAL))
{
ec_group[currentgroup].docheckstate = TRUE;
if (ec_slave[slave].state == (EC_STATE_SAFE_OP + EC_STATE_ERROR))
{
printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", slave);
ec_slave[slave].state = (EC_STATE_SAFE_OP + EC_STATE_ACK);
ec_writestate(slave); // 将指定的状态写到指定的从站之中
}
else if(ec_slave[slave].state == EC_STATE_SAFE_OP)
{
printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", slave);
ec_slave[slave].state = EC_STATE_OPERATIONAL;
ec_writestate(slave);
}
else if(ec_slave[slave].state > EC_STATE_NONE)
{
if (ec_reconfig_slave(slave, EC_TIMEOUTMON))
{
ec_slave[slave].islost = FALSE;
printf("MESSAGE : slave %d reconfigured\n",slave);
}
}
else if(!ec_slave[slave].islost)
{
/* re-check state */
ec_statecheck(slave, EC_STATE_OPERATIONAL, EC_TIMEOUTRET);
if (ec_slave[slave].state == EC_STATE_NONE)
{
ec_slave[slave].islost = TRUE;
printf("ERROR : slave %d lost\n",slave);
}
}
}
if (ec_slave[slave].islost) // 检查从站是否 离线?
{
if(ec_slave[slave].state == EC_STATE_NONE)
{
if (ec_recover_slave(slave, EC_TIMEOUTMON)) // 恢复和从站之间的连接
{
ec_slave[slave].islost = FALSE;
printf("MESSAGE : slave %d recovered\n",slave);
}
}
else
{
ec_slave[slave].islost = FALSE;
printf("MESSAGE : slave %d found\n",slave);
}
}
}
if(!ec_group[currentgroup].docheckstate)
printf("OK : all slaves resumed OPERATIONAL.\n");
}
osal_usleep(10000);
}
}
// PDO 刷新线程,定时的去 收 发 PDO 数据。
OSAL_THREAD_FUNC ecatfreash( void *ptr )
{
(void)ptr;
int i, oloop, iloop, chk; /* Not used */
inOP = FALSE;
char *ifname = "eth1";
if (ec_init(ifname)) // 初始化网卡1 基于野火的 i.mx6ull 这个是右面的那个网卡
{
printf("ec_init on %s succeeded.\n",ifname);
/* find and auto-config slaves */
if ( ec_config_init(FALSE) > 0 ) // 检查是否初始化完成了
{
printf("%d slaves found and configured.\n",ec_slavecount);
ec_config_map(&IOmap); //根据 FMMU 的地址 完成数据的映射。
ec_configdc(); // 配置时钟信息
printf("Slaves mapped, state to SAFE_OP.\n");
/* wait for all slaves to reach SAFE_OP state */
ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE * 4); // 检查当前的状态
oloop = ec_slave[0].Obytes;
if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
if (oloop > 8) oloop = 8;
iloop = ec_slave[0].Ibytes;
if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
if (iloop > 8) iloop = 8;
printf("segments : %d : %d %d %d %d\n",ec_group[0].nsegments ,ec_group[0].IOsegment[0],ec_group[0].IOsegment[1],ec_group[0].IOsegment[2],ec_group[0].IOsegment[3]);
printf("Request operational state for all slaves\n");
expectedWKC = (ec_group[0].outputsWKC * 2) + ec_group[0].inputsWKC;
printf("Calculated workcounter %d\n", expectedWKC);
ec_slave[0].state = EC_STATE_OPERATIONAL;
/* send one valid process data to make outputs in slaves happy*/
ec_send_processdata();
ec_receive_processdata(EC_TIMEOUTRET);
/* request OP state for all slaves */
ec_writestate(0);
chk = 200;
/* wait for all slaves to reach OP state */
do
{
ec_send_processdata();
ec_receive_processdata(EC_TIMEOUTRET);
ec_statecheck(0, EC_STATE_OPERATIONAL, 50000);
}
while (chk-- && (ec_slave[0].state != EC_STATE_OPERATIONAL)); //等待第一个从站进入到可以操作的状态 或者200个周期没进入 判定为超时
if (ec_slave[0].state == EC_STATE_OPERATIONAL )
{
printf("Operational state reached for all slaves.\n");
inOP = TRUE;
/* cyclic loop */
//for(i = 1; i <= 10000; i++)
while(1) // 笔者的改动之处 线程在这里进行无限的循环每隔5ms 刷新一下pdo
{
ec_send_processdata();
wkc = ec_receive_processdata(EC_TIMEOUTRET); //
osal_usleep(5000);
}
inOP = FALSE;
}
else
{
printf("Not all slaves reached operational state.\n");
ec_readstate();
for(i = 1; i<=ec_slavecount ; i++)
{
if(ec_slave[i].state != EC_STATE_OPERATIONAL)
{
printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
}
}
}
printf("\nRequest init state for all slaves\n");
ec_slave[0].state = EC_STATE_INIT;
/* request INIT state for all slaves */
ec_writestate(0);
}
else
{
printf("No slaves found!\n");
}
}
}
int main(int argc, char *argv[])
{
printf("SOEM (Simple Open EtherCAT Master)\nSimple test\n");
if (argc > 1)
{
/* create thread to handle slave error handling in OP */
// pthread_create( &thread1, NULL, (void *) &ecatcheck, (void*) &ctime);
osal_thread_create(&thread1, 128000, &ecatcheck, NULL);
/* start cyclic part */
// simpletest(argv[1]);
osal_thread_create(&thread_freash, 128000, &ecatfreash, NULL);
// 这里需要等待一段时间,让刷新现成初始化好
while( ec_slave[0].outputs == 0); // 这个等待是必须的, 因为创建的子线程 ecatfreash 需要一段时间才能完成初始化工作
while(1)
{
ec_slave[0].outputs[0] = ec_slave[0].inputs[0]; // 将第一个从站的 输入 等于输出
// 显示的效果上面就是 我按下按键之后 从站上的灯就会亮起
// 测试使用的从站模块是只有8个LED 和 8个 按键的模块。
osal_usleep(5000);
}
}else
{
printf("Usage: simple_test ifname1\nifname = eth0 for example\n");
}
ec_close();
printf("End program %s\n",argv[0]);
return (0);
}