]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - examples/win32test/main.c
*** empty log message ***
[CanFestival-3.git] / examples / win32test / main.c
1 /*
2 This file is part of CanFestival, a library implementing CanOpen Stack.
3
4 CanFestival Copyright (C): Edouard TISSERANT and Francis DUPIN
5 CanFestival Win32 port Copyright (C) 2007 Leonid Tochinski, ChattenAssociates, Inc.
6
7 See COPYING file for copyrights details.
8
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.
13
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.
18
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
22 */
23
24 /**************************************************************************
25 CanFestival3 win32 port example
26
27 This sample demonstrates CanFestival usage with Win32
28
29 Program implements master node. It starts CANOpen slave node, modifies OD, 
30 performs SDO reads and prints some slave node information.
31
32 Usage:
33
34    win32test <node_id> [can driver dll filename [baud rate]]
35
36  where node_id is node_id in decimal format
37
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.
41
42 Sample can work on other platdorms as well.
43 ***************************************************************************/
44
45 #include <stdio.h>
46 #include <stdlib.h> 
47 #include "win32test.h"
48 #include "canfestival.h"
49
50 #ifdef WIN32
51 #define sleep_proc(ms) Sleep(ms)
52 #define uptime_ms_proc() GetTickCount()
53 #else
54 #include <time.h> 
55 #define sleep_proc(ms)
56 #define uptime_ms_proc (1000*(time()%86400))  // TOD
57 #endif
58
59 UNS8 GetChangeStateResults(UNS8 node_id, UNS8 expected_state, unsigned long timeout_ms)
60    {
61    unsigned long start_time = 0;
62    
63    // reset nodes state
64    win32test_Data.NMTable[node_id] = Unknown_state;
65
66    // request node state
67    masterRequestNodeState(&win32test_Data, node_id);
68    
69    start_time = uptime_ms_proc();
70    while(uptime_ms_proc() - start_time < timeout_ms)
71       {
72       if (getNodeState(&win32test_Data, node_id) == expected_state)
73          return 0;
74       sleep_proc(1);   
75       }
76    return 0xFF;
77    }
78
79 UNS8 ReadSDO(UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, void* data, UNS8* size)
80    {
81    UNS32 abortCode = 0;
82    UNS8 res = SDO_UPLOAD_IN_PROGRESS;
83    // Read SDO
84    UNS8 err = readNetworkDict (&win32test_Data, nodeId, index, subIndex, dataType);
85    if (err)
86       return 0xFF;
87    for(;;)
88       {
89       res = getReadResultNetworkDict (&win32test_Data, nodeId, data, size, &abortCode);
90       if (res != SDO_UPLOAD_IN_PROGRESS)
91          break;   
92       sleep_proc(1);
93       continue;
94       }
95    closeSDOtransfer(&win32test_Data, nodeId, SDO_CLIENT);
96    if (res == SDO_FINISHED)
97       return 0;
98    return 0xFF;   
99    }
100
101 int main(int argc, char *argv[])
102   {
103    UNS8 node_id = 0;
104    s_BOARD MasterBoard = {"1", "125K"};
105    char* dll_file_name;
106
107    /* process command line arguments */
108    if (argc < 2)
109       {
110       printf("USAGE: win32test <node_id> [can driver dll filename [baud rate]]\n");
111       return 1;
112       }
113
114    node_id = atoi(argv[1]);
115    if (node_id < 2 || node_id > 127)
116       {
117       printf("ERROR: node_id shoule be >=2 and <= 127\n");
118       return 1;
119       }
120
121    if (argc > 2)
122       dll_file_name = argv[2];
123    else
124       dll_file_name = "can_uvccm_win32.dll";
125
126    if (argc > 3)
127       MasterBoard.baudrate = argv[3];
128
129    // load can driver
130    if (!LoadCanDriver(dll_file_name))
131       {
132       printf("ERROR: could not load diver %s\n", dll_file_name);
133       return 1;
134       }
135    
136    if (canOpen(&MasterBoard,&win32test_Data))
137       {
138       /* Defining the node Id */
139       setNodeId(&win32test_Data, 0x01);
140
141       /* init */
142       setState(&win32test_Data, Initialisation);
143
144       /****************************** START *******************************/
145       /* Put the master in operational mode */
146       setState(&win32test_Data, Operational);
147
148       /* Ask slave node to go in operational mode */
149       masterSendNMTstateChange (&win32test_Data, 0, NMT_Start_Node);
150
151       printf("\nStarting node %d (%xh) ...\n",(int)node_id,(int)node_id);
152       
153       /* wait untill mode will switch to operational state*/
154       if (GetChangeStateResults(node_id, Operational, 3000) != 0xFF)
155          {
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);
161
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))
165             {
166             UNS32 dev_type = 0;
167             char device_name[64]="";
168             char hw_ver[64]="";
169             char sw_ver[64]="";   
170             UNS32 vendor_id = 0;            
171             UNS32 prod_code = 0;
172             UNS32 ser_num = 0;
173             UNS8 size;
174             UNS8 res;
175
176             printf("\nnode_id: %d (%xh) info\n",(int)node_id,(int)node_id);
177             printf("********************************************\n");
178
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);
182            
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);
186
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);
190
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);            
194             
195             size = sizeof (vendor_id);
196             res = ReadSDO(node_id, 0x1018, 1, uint32, &vendor_id, &size);
197             printf("vendor id: %d\n",vendor_id);
198
199             size = sizeof (prod_code);
200             res = ReadSDO(node_id, 0x1018, 2, uint32, &prod_code, &size);
201             printf("product code: %d\n",prod_code);
202
203             size = sizeof (ser_num);
204             res = ReadSDO(node_id, 0x1018, 4, uint32, &ser_num, &size);
205             printf("serial number: %d\n",ser_num);
206             
207             printf("********************************************\n");
208             } 
209          else
210             {
211             printf("ERROR: Object dictionary access failed\n");
212             }
213          }
214       else
215          {
216          printf("ERROR: node_id %d (%xh) is not responding\n",(int)node_id,(int)node_id);
217          }
218          
219       masterSendNMTstateChange (&win32test_Data, 0x02, NMT_Stop_Node);
220
221       setState(&win32test_Data, Stopped);
222       
223       canClose(&win32test_Data);
224       }
225    return 0;
226   }
227   
228