2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
6 See COPYING file for copyrights details.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #if defined(WIN32) && !defined(__CYGWIN__)
33 //#include <sys/time.h>
40 #include <can_driver.h>
41 #include <timers_driver.h>
45 #include "TestMasterSlave.h"
47 #define MyCase(fc) case fc: eprintf(#fc);break;
48 void print_message(Message *m)
51 switch(m->cob_id.w >> 7)
68 eprintf(" rtr:%d", m->rtr);
69 eprintf(" len:%d", m->len);
70 for (i = 0 ; i < m->len ; i++)
71 eprintf(" %02x", m->data[i]);
75 UNS32 OnMasterMap1Update(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
77 eprintf("OnSlaveMap1Update:%d\n", SlaveMap1);
81 CAN_PORT SlaveCanHandle;
82 CAN_PORT MasterCanHandle;
84 s_BOARD SlaveBoard = {"0", "500K"};
85 s_BOARD MasterBoard = {"1", "500K"};
87 #if !defined(WIN32) || defined(__CYGWIN__)
88 void catch_signal(int sig)
90 signal(SIGTERM, catch_signal);
91 signal(SIGINT, catch_signal);
92 eprintf("Got Signal %d\n",sig);
98 printf("**************************************************************\n");
99 printf("* TestMasterSlave *\n");
101 printf("* A simple example for PC. It does implement 2 CanOpen *\n");
102 printf("* nodes in the same process. A master and a slave. Both *\n");
103 printf("* communicate together, exchanging periodically NMT, SYNC, *\n");
104 printf("* SDO and PDO. *\n");
106 printf("* Usage: *\n");
107 printf("* ./TestMasterSlave [OPTIONS] *\n");
109 printf("* OPTIONS: *\n");
110 printf("* -l : Can library [\"libcanfestival_can_virtual.so\"] *\n");
112 printf("* Slave: *\n");
113 printf("* -s : bus name [\"0\"] *\n");
114 printf("* -S : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
116 printf("* Master: *\n");
117 printf("* -m : bus name [\"1\"] *\n");
118 printf("* -M : 1M,500K,250K,125K,100K,50K,20K,10K,none(disable) *\n");
120 printf("**************************************************************\n");
123 /*************************** INIT *****************************************/
124 void InitNodes(CO_Data* d, UNS32 id)
126 /****************************** INITIALISATION SLAVE *******************************/
127 if(SlaveBoard.baudrate) {
128 /* Defining the node Id */
129 setNodeId(&TestSlave_Data, 0x02);
131 setState(&TestSlave_Data, Initialisation);
134 /****************************** INITIALISATION MASTER *******************************/
135 if(MasterBoard.baudrate){
136 RegisterSetODentryCallBack(&TestMaster_Data, 0x2000, 0, &OnMasterMap1Update);
138 /* Defining the node Id */
139 setNodeId(&TestMaster_Data, 0x01);
142 setState(&TestMaster_Data, Initialisation);
144 /****************************** START *******************************/
145 /* Put the master in operational mode */
146 setState(&TestMaster_Data, Operational);
148 /* Ask slave node to go in operational mode */
149 masterSendNMTstateChange (&TestMaster_Data, 0x02, NMT_Start_Node);
153 /****************************************************************************/
154 /*************************** MAIN *****************************************/
155 /****************************************************************************/
156 int main(int argc,char **argv)
161 char* LibraryPath="libcanfestival_can_virtual.so";
163 while ((c = getopt(argc, argv, "-m:s:M:S:l:")) != EOF)
173 SlaveBoard.busname = optarg;
181 MasterBoard.busname = optarg;
189 SlaveBoard.baudrate = optarg;
197 MasterBoard.baudrate = optarg;
205 LibraryPath = optarg;
213 #if !defined(WIN32) || defined(__CYGWIN__)
214 /* install signal handler for manual break */
215 signal(SIGTERM, catch_signal);
216 signal(SIGINT, catch_signal);
219 #ifndef NOT_USE_DYNAMIC_LOADING
220 LoadCanDriver(LibraryPath);
223 if(SlaveBoard.baudrate){
224 SlaveCanHandle = canOpen(&SlaveBoard,&TestSlave_Data);
225 if(SlaveCanHandle == NULL){
226 eprintf("Cannot open Slave Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
231 if(MasterBoard.baudrate){
232 MasterCanHandle = canOpen(&MasterBoard,&TestMaster_Data);
233 if(MasterCanHandle == NULL){
234 eprintf("Cannot open Master Board (%s,%s)\n",SlaveBoard.busname, SlaveBoard.baudrate);
239 // Start timer thread
240 StartTimerLoop(&InitNodes);
244 eprintf("Finishing.\n");
249 // Close CAN devices (and can threads)
250 if(SlaveBoard.baudrate) canClose(SlaveCanHandle);
252 if(MasterBoard.baudrate) canClose(MasterCanHandle);