nico@215: nico@215:
nico@215:00001 /************************************************************************** nico@215: 00002 CanFestival3 win32 port example nico@215: 00003 nico@215: 00004 This sample demonstrates CanFestival usage with Win32 nico@215: 00005 nico@215: 00006 Program implements master node. It starts CANOpen slave node, modifies OD, nico@215: 00007 performs SDO reads and prints some slave node information. nico@215: 00008 nico@215: 00009 Usage: nico@215: 00010 nico@215: 00011 win32test <node_id> nico@215: 00012 nico@215: 00013 where node_id is node ID in decimal format nico@215: 00014 nico@215: 00015 You should have CanFestival-3.dll CAN-uVCCM.dll in the search path to run this sample. nico@215: 00016 Code will work with non-UNICODE CanFestival-3.dll CAN-uVCCM.dll libraries. nico@215: 00017 nico@215: 00018 Sample can work on other platdorms as well. nico@215: 00019 nico@215: 00020 Copyright (C) 2007 Leonid Tochinski (ltochinski AT chattenassociates DOT com) nico@215: 00021 ***************************************************************************/ nico@215: 00022 nico@215: 00023 #include <stdio.h> nico@215: 00024 #include <stdlib.h> nico@215: 00025 #include "win32test.h" nico@215: 00026 #include "canfestival.h" nico@215: 00027 nico@215: 00028 #ifdef WIN32 nico@215: 00029 #define sleep_proc(ms) Sleep(ms) nico@215: 00030 #define uptime_ms_proc() GetTickCount() nico@215: 00031 #else nico@215: 00032 #include <time.h> etisserant@240: 00033 #define sleep_proc(ms) etisserant@240: 00034 #define uptime_ms_proc (1000*(time()%86400)) // TOD nico@215: 00035 #endif nico@215: 00036 etisserant@240: 00037 UNS8 GetChangeStateResults(UNS8 node_id, UNS8 expected_state, unsigned long timeout_ms) nico@215: 00038 { nico@215: 00039 unsigned long start_time = 0; nico@215: 00040 nico@215: 00041 // reset nodes state etisserant@240: 00042 win32test_Data.NMTable[node_id] = Unknown_state; nico@215: 00043 nico@215: 00044 // request node state etisserant@240: 00045 masterRequestNodeState(&win32test_Data, node_id); nico@215: 00046 etisserant@240: 00047 start_time = uptime_ms_proc(); etisserant@240: 00048 while(uptime_ms_proc() - start_time < timeout_ms) nico@215: 00049 { etisserant@240: 00050 if (getNodeState(&win32test_Data, node_id) == expected_state) nico@215: 00051 return 0; etisserant@240: 00052 sleep_proc(1); nico@215: 00053 } nico@215: 00054 return 0xFF; nico@215: 00055 } nico@215: 00056 etisserant@240: 00057 UNS8 ReadSDO(UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, void* data, UNS8* size) nico@215: 00058 { etisserant@240: 00059 UNS32 abortCode = 0; etisserant@240: 00060 UNS8 res = SDO_UPLOAD_IN_PROGRESS; nico@215: 00061 // Read SDO etisserant@240: 00062 UNS8 err = readNetworkDict (&win32test_Data, nodeId, index, subIndex, dataType); nico@215: 00063 if (err) nico@215: 00064 return 0xFF; nico@215: 00065 for(;;) nico@215: 00066 { etisserant@240: 00067 res = getReadResultNetworkDict (&win32test_Data, nodeId, data, size, &abortCode); etisserant@240: 00068 if (res != SDO_UPLOAD_IN_PROGRESS) nico@215: 00069 break; etisserant@240: 00070 sleep_proc(1); nico@215: 00071 continue; nico@215: 00072 } etisserant@240: 00073 closeSDOtransfer(&win32test_Data, nodeId, SDO_CLIENT); etisserant@240: 00074 if (res == SDO_FINISHED) nico@215: 00075 return 0; nico@215: 00076 return 0xFF; nico@215: 00077 } nico@215: 00078 etisserant@240: 00079 int main(int argc, char *argv[]) nico@215: 00080 { etisserant@240: 00081 UNS8 node_id = 0; etisserant@240: 00082 s_BOARD MasterBoard = {"1", "125K"}; nico@215: 00083 char* dll_file_name; nico@215: 00084 nico@215: 00085 /* process command line arguments */ nico@215: 00086 if (argc < 2) nico@215: 00087 { nico@215: 00088 printf("USAGE: win32test node_id [dll_file_name]\n"); nico@215: 00089 return 1; nico@215: 00090 } nico@215: 00091 nico@215: 00092 node_id = atoi(argv[1]); nico@215: 00093 if (node_id < 2 || node_id > 127) nico@215: 00094 { nico@215: 00095 printf("ERROR: node_id shoule be >=2 and <= 127\n"); nico@215: 00096 return 1; nico@215: 00097 } nico@215: 00098 nico@215: 00099 if (argc > 2) nico@215: 00100 dll_file_name = argv[2]; nico@215: 00101 else nico@215: 00102 dll_file_name = "can_uvccm_win32.dll"; nico@215: 00103 nico@215: 00104 // load can driver etisserant@240: 00105 if (!LoadCanDriver(dll_file_name)) nico@215: 00106 { nico@215: 00107 printf("ERROR: could not load diver %s\n", dll_file_name); nico@215: 00108 return 1; nico@215: 00109 } nico@215: 00110 etisserant@240: 00111 if (canOpen(&MasterBoard,&win32test_Data)) nico@215: 00112 { nico@215: 00113 /* Defining the node Id */ etisserant@240: 00114 setNodeId(&win32test_Data, 0x01); nico@215: 00115 nico@215: 00116 /* init */ etisserant@240: 00117 setState(&win32test_Data, Initialisation); nico@215: 00118 nico@215: 00119 /****************************** START *******************************/ nico@215: 00120 /* Put the master in operational mode */ etisserant@240: 00121 setState(&win32test_Data, Operational); nico@215: 00122 nico@215: 00123 /* Ask slave node to go in operational mode */ etisserant@240: 00124 masterSendNMTstateChange (&win32test_Data, 0, NMT_Start_Node); nico@215: 00125 nico@215: 00126 printf("\nStarting node %d (%xh) ...\n",(int)node_id,(int)node_id); nico@215: 00127 nico@215: 00128 /* wait untill mode will switch to operational state*/ etisserant@240: 00129 if (GetChangeStateResults(node_id, Operational, 3000) != 0xFF) nico@215: 00130 { nico@215: 00131 /* modify Client SDO 1 Parameter */ etisserant@240: 00132 UNS32 COB_ID_Client_to_Server_Transmit_SDO = 0x600 + node_id; etisserant@240: 00133 UNS32 COB_ID_Server_to_Client_Receive_SDO = 0x580 + node_id; etisserant@240: 00134 UNS32 Node_ID_of_the_SDO_Server = node_id; etisserant@240: 00135 UNS8 ExpectedSize = sizeof (UNS32); nico@215: 00136 etisserant@240: 00137 if (OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 1, &COB_ID_Client_to_Server_Transmit_SDO, &ExpectedSize, RW) etisserant@240: 00138 && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 2, &COB_ID_Server_to_Client_Receive_SDO, &ExpectedSize, RW) etisserant@240: 00139 && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 3, &Node_ID_of_the_SDO_Server, &ExpectedSize, RW)) nico@215: 00140 { etisserant@240: 00141 UNS32 dev_type = 0; nico@215: 00142 char device_name[64]=""; nico@215: 00143 char hw_ver[64]=""; nico@215: 00144 char sw_ver[64]=""; etisserant@240: 00145 UNS32 vendor_id = 0; etisserant@240: 00146 UNS32 prod_code = 0; etisserant@240: 00147 UNS32 ser_num = 0; etisserant@240: 00148 UNS8 size; etisserant@240: 00149 UNS8 res; nico@215: 00150 nico@215: 00151 printf("\nnode_id: %d (%xh) info\n",(int)node_id,(int)node_id); nico@215: 00152 printf("********************************************\n"); nico@215: 00153 nico@215: 00154 size = sizeof (dev_type); etisserant@240: 00155 res = ReadSDO(node_id, 0x1000, 0, uint32, &dev_type, &size); nico@215: 00156 printf("device type: %d\n",dev_type & 0xFFFF); nico@215: 00157 nico@215: 00158 size = sizeof (device_name); etisserant@240: 00159 res = ReadSDO(node_id, 0x1008, 0, visible_string, device_name, &size); nico@215: 00160 printf("device name: %s\n",device_name); nico@215: 00161 nico@215: 00162 size = sizeof (hw_ver); etisserant@240: 00163 res = ReadSDO(node_id, 0x1009, 0, visible_string, hw_ver, &size); nico@215: 00164 printf("HW version: %s\n",hw_ver); nico@215: 00165 nico@215: 00166 size = sizeof (sw_ver); etisserant@240: 00167 res = ReadSDO(node_id, 0x100A, 0, visible_string, sw_ver, &size); nico@215: 00168 printf("SW version: %s\n",sw_ver); nico@215: 00169 nico@215: 00170 size = sizeof (vendor_id); etisserant@240: 00171 res = ReadSDO(node_id, 0x1018, 1, uint32, &vendor_id, &size); nico@215: 00172 printf("vendor id: %d\n",vendor_id); nico@215: 00173 nico@215: 00174 size = sizeof (prod_code); etisserant@240: 00175 res = ReadSDO(node_id, 0x1018, 2, uint32, &prod_code, &size); nico@215: 00176 printf("product code: %d\n",prod_code); nico@215: 00177 nico@215: 00178 size = sizeof (ser_num); etisserant@240: 00179 res = ReadSDO(node_id, 0x1018, 4, uint32, &ser_num, &size); nico@215: 00180 printf("serial number: %d\n",ser_num); nico@215: 00181 nico@215: 00182 printf("********************************************\n"); nico@215: 00183 } nico@215: 00184 else nico@215: 00185 { nico@215: 00186 printf("ERROR: Object dictionary access failed\n"); nico@215: 00187 } nico@215: 00188 } nico@215: 00189 else nico@215: 00190 { nico@215: 00191 printf("ERROR: node_id %d (%xh) is not responding\n",(int)node_id,(int)node_id); nico@215: 00192 } nico@215: 00193 etisserant@240: 00194 masterSendNMTstateChange (&win32test_Data, 0x02, NMT_Stop_Node); nico@215: 00195 etisserant@240: 00196 setState(&win32test_Data, Stopped); nico@215: 00197 etisserant@240: 00198 canClose(&win32test_Data); nico@215: 00199 } nico@215: 00200 return 0; nico@215: 00201 } nico@215: 00202 nico@215: 00203 etisserant@240: