main.c

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

Generated on Mon Jul 2 19:10:16 2007 for CanFestival by  doxygen 1.5.1