2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 CanFestival Copyright (C): Edouard TISSERANT and Francis DUPIN
5 CanFestival Win32 port Copyright (C) 2007 Leonid Tochinski, ChattenAssociates, Inc.
7 See COPYING file for copyrights details.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /**************************************************************************
25 CanFestival3 win32 port example
27 This sample demonstrates CanFestival usage with Win32
29 Program implements master node. It starts CANOpen slave node, modifies OD,
30 performs SDO reads and prints some slave node information.
34 win32test <node_id> [can driver dll filename [baud rate]]
36 where node_id is node_id in decimal format
38 If driver is not specified, CAN-uVCCM.dll will be used by default.
39 If baudrate is not specified, 125K will be used by default.
40 You should have CanFestival-3.dll can driver dll in the search path to run this sample.
42 Sample can work on other platdorms as well.
43 ***************************************************************************/
47 #include "win32test.h"
48 #include "canfestival.h"
51 #define sleep_proc(ms) Sleep(ms)
52 #define uptime_ms_proc() GetTickCount()
55 #define sleep_proc(ms)
56 #define uptime_ms_proc (1000*(time()%86400)) // TOD
59 UNS8 GetChangeStateResults(UNS8 node_id, UNS8 expected_state, unsigned long timeout_ms)
61 unsigned long start_time = 0;
64 win32test_Data.NMTable[node_id] = Unknown_state;
67 masterRequestNodeState(&win32test_Data, node_id);
69 start_time = uptime_ms_proc();
70 while(uptime_ms_proc() - start_time < timeout_ms)
72 if (getNodeState(&win32test_Data, node_id) == expected_state)
79 UNS8 ReadSDO(UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, void* data, UNS8* size)
82 UNS8 res = SDO_UPLOAD_IN_PROGRESS;
84 UNS8 err = readNetworkDict (&win32test_Data, nodeId, index, subIndex, dataType);
89 res = getReadResultNetworkDict (&win32test_Data, nodeId, data, size, &abortCode);
90 if (res != SDO_UPLOAD_IN_PROGRESS)
95 closeSDOtransfer(&win32test_Data, nodeId, SDO_CLIENT);
96 if (res == SDO_FINISHED)
101 int main(int argc, char *argv[])
104 s_BOARD MasterBoard = {"1", "125K"};
107 /* process command line arguments */
110 printf("USAGE: win32test <node_id> [can driver dll filename [baud rate]]\n");
114 node_id = atoi(argv[1]);
115 if (node_id < 2 || node_id > 127)
117 printf("ERROR: node_id shoule be >=2 and <= 127\n");
122 dll_file_name = argv[2];
124 dll_file_name = "can_uvccm_win32.dll";
127 MasterBoard.baudrate = argv[3];
130 if (!LoadCanDriver(dll_file_name))
132 printf("ERROR: could not load diver %s\n", dll_file_name);
136 if (canOpen(&MasterBoard,&win32test_Data))
138 /* Defining the node Id */
139 setNodeId(&win32test_Data, 0x01);
142 setState(&win32test_Data, Initialisation);
144 /****************************** START *******************************/
145 /* Put the master in operational mode */
146 setState(&win32test_Data, Operational);
148 /* Ask slave node to go in operational mode */
149 masterSendNMTstateChange (&win32test_Data, 0, NMT_Start_Node);
151 printf("\nStarting node %d (%xh) ...\n",(int)node_id,(int)node_id);
153 /* wait untill mode will switch to operational state*/
154 if (GetChangeStateResults(node_id, Operational, 3000) != 0xFF)
156 /* modify Client SDO 1 Parameter */
157 UNS32 COB_ID_Client_to_Server_Transmit_SDO = 0x600 + node_id;
158 UNS32 COB_ID_Server_to_Client_Receive_SDO = 0x580 + node_id;
159 UNS32 Node_ID_of_the_SDO_Server = node_id;
160 UNS8 ExpectedSize = sizeof (UNS32);
162 if (OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 1, &COB_ID_Client_to_Server_Transmit_SDO, &ExpectedSize, RW)
163 && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 2, &COB_ID_Server_to_Client_Receive_SDO, &ExpectedSize, RW)
164 && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 3, &Node_ID_of_the_SDO_Server, &ExpectedSize, RW))
167 char device_name[64]="";
176 printf("\nnode_id: %d (%xh) info\n",(int)node_id,(int)node_id);
177 printf("********************************************\n");
179 size = sizeof (dev_type);
180 res = ReadSDO(node_id, 0x1000, 0, uint32, &dev_type, &size);
181 printf("device type: %d\n",dev_type & 0xFFFF);
183 size = sizeof (device_name);
184 res = ReadSDO(node_id, 0x1008, 0, visible_string, device_name, &size);
185 printf("device name: %s\n",device_name);
187 size = sizeof (hw_ver);
188 res = ReadSDO(node_id, 0x1009, 0, visible_string, hw_ver, &size);
189 printf("HW version: %s\n",hw_ver);
191 size = sizeof (sw_ver);
192 res = ReadSDO(node_id, 0x100A, 0, visible_string, sw_ver, &size);
193 printf("SW version: %s\n",sw_ver);
195 size = sizeof (vendor_id);
196 res = ReadSDO(node_id, 0x1018, 1, uint32, &vendor_id, &size);
197 printf("vendor id: %d\n",vendor_id);
199 size = sizeof (prod_code);
200 res = ReadSDO(node_id, 0x1018, 2, uint32, &prod_code, &size);
201 printf("product code: %d\n",prod_code);
203 size = sizeof (ser_num);
204 res = ReadSDO(node_id, 0x1018, 4, uint32, &ser_num, &size);
205 printf("serial number: %d\n",ser_num);
207 printf("********************************************\n");
211 printf("ERROR: Object dictionary access failed\n");
216 printf("ERROR: node_id %d (%xh) is not responding\n",(int)node_id,(int)node_id);
219 masterSendNMTstateChange (&win32test_Data, 0x02, NMT_Stop_Node);
221 setState(&win32test_Data, Stopped);
223 canClose(&win32test_Data);