simple_test.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include <sys/time.h>
00015 #include <unistd.h>
00016
00017 #include "ethercattype.h"
00018 #include "nicdrv.h"
00019 #include "ethercatbase.h"
00020 #include "ethercatmain.h"
00021 #include "ethercatcoe.h"
00022 #include "ethercatfoe.h"
00023 #include "ethercatconfig.h"
00024 #include "ethercatprint.h"
00025
00026 char IOmap[4096];
00027
00028 void simpletest(char *ifname)
00029 {
00030 int i, j, oloop, iloop, wkc, wkc_count, slave;
00031 boolean needlf;
00032
00033 needlf = FALSE;
00034 printf("Starting simple test\n");
00035
00036
00037 if (ec_init(ifname))
00038 {
00039 printf("ec_init on %s succeeded.\n",ifname);
00040
00041
00042
00043 if ( ec_config_init(FALSE) > 0 )
00044 {
00045 printf("%d slaves found and configured.\n",ec_slavecount);
00046
00047 ec_config_map(&IOmap);
00048
00049 printf("Slaves mapped, state to SAFE_OP.\n");
00050
00051 ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE * 4);
00052
00053 oloop = ec_slave[0].Obytes;
00054 if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
00055 if (oloop > 8) oloop = 8;
00056 iloop = ec_slave[0].Ibytes;
00057 if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
00058 if (iloop > 8) iloop = 8;
00059
00060 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]);
00061
00062 printf("Request operational state for all slaves\n");
00063 printf("Calculated workcounter %d\n",ec_group[0].expectedWKC);
00064 ec_slave[0].state = EC_STATE_OPERATIONAL;
00065
00066 ec_send_processdata();
00067 ec_receive_processdata(EC_TIMEOUTRET);
00068
00069 ec_writestate(0);
00070
00071 ec_statecheck(0, EC_STATE_OPERATIONAL, EC_TIMEOUTSTATE * 4);
00072 if (ec_slave[0].state == EC_STATE_OPERATIONAL )
00073 {
00074 printf("Operational state reached for all slaves.\n");
00075 wkc_count = 0;
00076
00077 for(i = 1; i <= 10000; i++)
00078 {
00079 ec_send_processdata();
00080 wkc = ec_receive_processdata(EC_TIMEOUTRET);
00081
00082 if( (wkc < ec_group[0].expectedWKC) || ec_group[0].docheckstate)
00083 {
00084 if (needlf)
00085 {
00086 needlf = FALSE;
00087 printf("\n");
00088 }
00089
00090 ec_group[0].docheckstate = FALSE;
00091 ec_readstate();
00092 for (slave = 1; slave <= ec_slavecount; slave++)
00093 {
00094 if (ec_slave[slave].state != EC_STATE_OPERATIONAL)
00095 {
00096 ec_group[0].docheckstate = TRUE;
00097 if (ec_slave[slave].state == (EC_STATE_SAFE_OP + EC_STATE_ERROR))
00098 {
00099 printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", slave);
00100 ec_slave[slave].state = (EC_STATE_SAFE_OP + EC_STATE_ACK);
00101 ec_writestate(slave);
00102 }
00103 else if(ec_slave[slave].state == EC_STATE_SAFE_OP)
00104 {
00105 printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", slave);
00106 ec_slave[slave].state = EC_STATE_OPERATIONAL;
00107 ec_writestate(slave);
00108 }
00109 else if(ec_slave[slave].state > 0)
00110 {
00111 if (ec_reconfig_slave(slave))
00112 {
00113 ec_slave[slave].islost = FALSE;
00114 printf("MESSAGE : slave %d reconfigured\n",slave);
00115 }
00116 }
00117 else if(!ec_slave[slave].islost)
00118 {
00119 ec_slave[slave].islost = TRUE;
00120 printf("ERROR : slave %d lost\n",slave);
00121 }
00122 }
00123 if (ec_slave[slave].islost)
00124 {
00125 if(!ec_slave[slave].state)
00126 {
00127 if (ec_recover_slave(slave))
00128 {
00129 ec_slave[slave].islost = FALSE;
00130 printf("MESSAGE : slave %d recovered\n",slave);
00131 }
00132 }
00133 else
00134 {
00135 ec_slave[slave].islost = FALSE;
00136 printf("MESSAGE : slave %d found\n",slave);
00137 }
00138 }
00139 }
00140 if(!ec_group[0].docheckstate)
00141 printf("OK : all slaves resumed OPERATIONAL.\n");
00142 }
00143 else
00144 {
00145 printf("Processdata cycle %4d, WKC %d , O:", i, wkc);
00146
00147 for(j = 0 ; j < oloop; j++)
00148 {
00149 printf(" %2.2x", *(ec_slave[0].outputs + j));
00150 }
00151
00152 printf(" I:");
00153 for(j = 0 ; j < iloop; j++)
00154 {
00155 printf(" %2.2x", *(ec_slave[0].inputs + j));
00156 }
00157 printf("\r");
00158 needlf = TRUE;
00159 }
00160 usleep(10000);
00161
00162 }
00163 }
00164 else
00165 {
00166 printf("Not all slaves reached operational state.\n");
00167 ec_readstate();
00168 for(i = 1; i<=ec_slavecount ; i++)
00169 {
00170 if(ec_slave[i].state != EC_STATE_OPERATIONAL)
00171 {
00172 printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
00173 i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
00174 }
00175 }
00176 }
00177 printf("\nRequest safe operational state for all slaves\n");
00178 ec_slave[0].state = EC_STATE_INIT;
00179
00180 ec_writestate(0);
00181 }
00182 else
00183 {
00184 printf("No slaves found!\n");
00185 }
00186 printf("End simple test, close socket\n");
00187
00188 ec_close();
00189 }
00190 else
00191 {
00192 printf("No socket connection on %s\nExcecute as root\n",ifname);
00193 }
00194 }
00195
00196 int main(int argc, char *argv[])
00197 {
00198 printf("SOEM (Simple Open EtherCAT Master)\nSimple test\n");
00199
00200 if (argc > 1)
00201 {
00202
00203 simpletest(argv[1]);
00204 }
00205 else
00206 {
00207 printf("Usage: simple_test ifname1\nifname = eth0 for example\n");
00208 }
00209
00210 printf("End program\n");
00211 return (0);
00212 }