int NodeInit(int NodeID, int NodeType) { if(NodeType) CANOpenShellOD_Data = &CANOpenShellMasterOD_Data; else CANOpenShellOD_Data = &CANOpenShellSlaveOD_Data; /* Load can library */ LoadCanDriver(LibraryPath); /* Define callback functions */ CANOpenShellOD_Data->initialisation = CANOpenShellOD_initialisation; CANOpenShellOD_Data->preOperational = CANOpenShellOD_preOperational; CANOpenShellOD_Data->operational = CANOpenShellOD_operational; CANOpenShellOD_Data->stopped = CANOpenShellOD_stopped; CANOpenShellOD_Data->post_sync = CANOpenShellOD_post_sync; CANOpenShellOD_Data->post_TPDO = CANOpenShellOD_post_TPDO; CANOpenShellOD_Data->post_SlaveBootup=CANOpenShellOD_post_SlaveBootup; /* Open the Peak CANOpen device */ if(!canOpen(&Board,CANOpenShellOD_Data)) return INIT_ERR; /* Defining the node Id */ setNodeId(CANOpenShellOD_Data, NodeID); /* Start Timer thread */ StartTimerLoop(&Init); return 0; }
bool QCan::init() { if (!LoadCanDriver("/usr/local/lib/libcanfestival_can_vscom.so")) return false; TimerInit(); StartTimerLoop(Init); s_BOARD Board = {(char*)"/dev/ttyUSB0", (char*)"125K"}; mPort = canOpen(&Board, mData); if(!mPort) return false; setState(mData, Initialisation); masterSendNMTstateChange(mData, 0, NMT_Reset_Node); return true; }
//**************************************************************************** //*************************** MAIN ***************************************** //**************************************************************************** int main_can (s_BOARD SlaveBoard, char *LibraryPath) { #if !defined(WIN32) && !defined(__CYGWIN__) //TimerInit(); #endif TimerInit(); printf ("Bus name: %s Freq: %s Driver: %s\n", SlaveBoard.busname, SlaveBoard.baudrate, LibraryPath); #ifndef NOT_USE_DYNAMIC_LOADING if (LoadCanDriver (LibraryPath) == NULL) *textLog << wxT ("Unable to load library\n"); #endif // Open CAN devices ObjDict_Data.heartbeatError = Call_heartbeatError; ObjDict_Data.initialisation = Call_initialisation; ObjDict_Data.preOperational = Call_preOperational; ObjDict_Data.operational = Call_operational; ObjDict_Data.stopped = Call_stopped; ObjDict_Data.post_sync = Call_post_sync; ObjDict_Data.post_TPDO = Call_post_TPDO; ObjDict_Data.storeODSubIndex = Call_storeODSubIndex; if (!canOpen (&SlaveBoard, &ObjDict_Data)) { printf ("Cannot open Slave Board (%s,%s)\n", SlaveBoard.busname, SlaveBoard.baudrate); return (1); } StartTimerLoop (&InitNodes); return 0; }
int main(int argc,char **argv) { int c; extern char *optarg; char* LibraryPath="libcanfestival_can_virtual.so"; char *snodeid; while ((c = getopt(argc, argv, "-m:s:M:S:l:i:")) != EOF) { switch(c) { case 'm' : if (optarg[0] == 0) { help(); exit(1); } MasterBoard.busname = optarg; break; case 'M' : if (optarg[0] == 0) { help(); exit(1); } MasterBoard.baudrate = optarg; break; case 'l' : if (optarg[0] == 0) { help(); exit(1); } LibraryPath = optarg; break; case 'i' : if (optarg[0] == 0) { help(); exit(1); } snodeid = optarg; sscanf(snodeid,"%x",&slavenodeid); break; default: help(); exit(1); } } #if !defined(WIN32) || defined(__CYGWIN__) /* install signal handler for manual break */ signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); TimerInit(); #endif #ifndef NOT_USE_DYNAMIC_LOADING LoadCanDriver(LibraryPath); #endif TestMaster_Data.heartbeatError = TestMaster_heartbeatError; TestMaster_Data.initialisation = TestMaster_initialisation; TestMaster_Data.preOperational = TestMaster_preOperational; TestMaster_Data.operational = TestMaster_operational; TestMaster_Data.stopped = TestMaster_stopped; TestMaster_Data.post_sync = TestMaster_post_sync; TestMaster_Data.post_TPDO = TestMaster_post_TPDO; if(!canOpen(&MasterBoard,&TestMaster_Data)){ eprintf("Cannot open Master Board\n"); goto fail_master; } // Start timer thread StartTimerLoop(&InitNodes); // wait Ctrl-C pause(); eprintf("Finishing.\n"); // Reset the slave node for next use (will stop emitting heartbeat) masterSendNMTstateChange (&TestMaster_Data, slavenodeid, NMT_Reset_Node); // Stop master setState(&TestMaster_Data, Stopped); // Stop timer thread StopTimerLoop(&Exit); fail_master: if(MasterBoard.baudrate) canClose(&TestMaster_Data); TimerCleanup(); return 0; }
int main() { LIB_HANDLE driver; /* handles for additional CO-PCICAN functions */ int (*get_fd_of_port)( CAN_PORT port ); int (*co_pcican_enter_run_mode)( const int fd ); int (*co_pcican_enter_config_mode)( const int fd ); int (*co_pcican_select_channel)( const unsigned char channel, const unsigned int direction ); int (*co_pcican_configure_selected_channel)( const int fd, s_BOARD *board, const unsigned int direction ); s_BOARD bd; CO_Data myData; CAN_PORT port; Message myMessage; const char busname[] = "/dev/co_pcican-0"; const char baudrate[] = "1M"; int fd; memset( &myData, 0x00, sizeof(CO_Data) ); myData.CurrentCommunicationState.csSYNC = 1; myData.post_sync = mySyncHandler; bd.busname = (char*)busname; bd.baudrate = (char*)baudrate; printf( "This example sends three SYNCs from port#%u to port#%u with a CO-PCICAN card\n", CHTX, CHRX ); driver = LoadCanDriver( "/usr/local/lib/libcanfestival_can_copcican_linux.so" ); if( driver == NULL ) { /* something strange happenend */ return 0; } /* dynamic load of additional library functions */ get_fd_of_port = dlsym( driver, "get_fd_of_port" ); co_pcican_enter_run_mode = dlsym( driver, "co_pcican_enter_run_mode" ); co_pcican_enter_config_mode = dlsym( driver, "co_pcican_enter_config_mode" ); co_pcican_select_channel = dlsym( driver, "co_pcican_select_channel" ); co_pcican_configure_selected_channel = dlsym( driver, "co_pcican_configure_selected_channel" ); /* direction: 0=RX, 1=TX */ co_pcican_select_channel( CHRX, 0 ); co_pcican_select_channel( CHTX, 1 ); port = canOpen( &bd, &myData ); if( port == NULL ) { UnLoadCanDriver( driver ); /* something strange happenend */ return 0; } /* get file descriptor to control the opened device */ fd = get_fd_of_port( port ); co_pcican_enter_config_mode( fd ); co_pcican_configure_selected_channel( fd, &bd, 0 ); co_pcican_configure_selected_channel( fd, &bd, 1 ); co_pcican_enter_run_mode( fd ); memset( &myMessage, 0x00, sizeof(Message) ); myMessage.cob_id = 0x80; /* SYNC message */ myMessage.len = 1; myMessage.data[0] = 0xA5; /* SEND HERE */ canSend( port, &myMessage ); myMessage.data[0] = 0x5A; canSend( port, &myMessage ); myMessage.data[0] = 0xA5; canSend( port, &myMessage ); /* mySyncHandler() is called by the receive thread and shows a received SYNC message on console */ usleep( 1*1000*1000 ); /* 1s */ canClose( &myData ); UnLoadCanDriver( driver ); return 0; }
int main(int argc, char *argv[]) { UNS8 node_id = 0; s_BOARD MasterBoard = {"1", "125K"}; char* dll_file_name; /* process command line arguments */ if (argc < 2) { printf("USAGE: win32test <node_id> [can driver dll filename [baud rate]]\n"); return 1; } node_id = atoi(argv[1]); if (node_id < 2 || node_id > 127) { printf("ERROR: node_id shoule be >=2 and <= 127\n"); return 1; } if (argc > 2) dll_file_name = argv[2]; else dll_file_name = "can_uvccm_win32.dll"; if (argc > 3) MasterBoard.baudrate = argv[3]; // load can driver if (!LoadCanDriver(dll_file_name)) { printf("ERROR: could not load diver %s\n", dll_file_name); return 1; } if (canOpen(&MasterBoard,&win32test_Data)) { /* Defining the node Id */ setNodeId(&win32test_Data, 0x01); /* init */ setState(&win32test_Data, Initialisation); /****************************** START *******************************/ /* Put the master in operational mode */ setState(&win32test_Data, Operational); /* Ask slave node to go in operational mode */ masterSendNMTstateChange (&win32test_Data, 0, NMT_Start_Node); printf("\nStarting node %d (%xh) ...\n",(int)node_id,(int)node_id); /* wait untill mode will switch to operational state*/ if (GetChangeStateResults(node_id, Operational, 3000) != 0xFF) { /* modify Client SDO 1 Parameter */ UNS32 COB_ID_Client_to_Server_Transmit_SDO = 0x600 + node_id; UNS32 COB_ID_Server_to_Client_Receive_SDO = 0x580 + node_id; UNS32 Node_ID_of_the_SDO_Server = node_id; UNS8 ExpectedSize = sizeof (UNS32); if (OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 1, &COB_ID_Client_to_Server_Transmit_SDO, &ExpectedSize, RW) && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 2, &COB_ID_Server_to_Client_Receive_SDO, &ExpectedSize, RW) && OD_SUCCESSFUL == writeLocalDict(&win32test_Data, 0x1280, 3, &Node_ID_of_the_SDO_Server, &ExpectedSize, RW)) { UNS32 dev_type = 0; char device_name[64]=""; char hw_ver[64]=""; char sw_ver[64]=""; UNS32 vendor_id = 0; UNS32 prod_code = 0; UNS32 ser_num = 0; UNS8 size; UNS8 res; printf("\nnode_id: %d (%xh) info\n",(int)node_id,(int)node_id); printf("********************************************\n"); size = sizeof (dev_type); res = ReadSDO(node_id, 0x1000, 0, uint32, &dev_type, &size); printf("device type: %d\n",dev_type & 0xFFFF); size = sizeof (device_name); res = ReadSDO(node_id, 0x1008, 0, visible_string, device_name, &size); printf("device name: %s\n",device_name); size = sizeof (hw_ver); res = ReadSDO(node_id, 0x1009, 0, visible_string, hw_ver, &size); printf("HW version: %s\n",hw_ver); size = sizeof (sw_ver); res = ReadSDO(node_id, 0x100A, 0, visible_string, sw_ver, &size); printf("SW version: %s\n",sw_ver); size = sizeof (vendor_id); res = ReadSDO(node_id, 0x1018, 1, uint32, &vendor_id, &size); printf("vendor id: %d\n",vendor_id); size = sizeof (prod_code); res = ReadSDO(node_id, 0x1018, 2, uint32, &prod_code, &size); printf("product code: %d\n",prod_code); size = sizeof (ser_num); res = ReadSDO(node_id, 0x1018, 4, uint32, &ser_num, &size); printf("serial number: %d\n",ser_num); printf("********************************************\n"); } else { printf("ERROR: Object dictionary access failed\n"); } } else { printf("ERROR: node_id %d (%xh) is not responding\n",(int)node_id,(int)node_id); } masterSendNMTstateChange (&win32test_Data, 0x02, NMT_Stop_Node); setState(&win32test_Data, Stopped); canClose(&win32test_Data); } return 0; }
int main(int argc,char **argv) { struct sigaction act; uint8_t nodeid = 2; // register handler on SIGINT signal act.sa_handler=sortie; sigemptyset(&act.sa_mask); act.sa_flags=0; sigaction(SIGINT,&act,0); // Check that we have the right command line parameters if(argc != 3){ display_usage(argv[0]); exit(1); } // get command line parameters nodeid = strtoul(argv[2], NULL, 10); SlaveBoard0.busname = argv[1]; printf("Starting on %s with node id = %u\n", SlaveBoard0.busname, nodeid); // register the callbacks we use RegisterSetODentryCallBack(&slavedic_Data, 0x2001, 0, callback_on_position); slavedic_Data.initialisation=state_change; slavedic_Data.preOperational=state_change; slavedic_Data.operational=state_change; slavedic_Data.stopped=state_change; // Init Canfestival if (LoadCanDriver("./libcanfestival_can_socket.so") == NULL){ printf("Unable to load driver library\n"); printf("please put file libcanfestival_can_socket.so in the current directory\n"); exit(1); } if(!canOpen(&SlaveBoard0,&slavedic_Data)){ printf("Cannot open can interface %s\n",SlaveBoard0.busname); exit(1); } TimerInit(); setNodeId(&slavedic_Data, nodeid); setState(&slavedic_Data, Initialisation); printf("Canfestival initialisation done\n"); Run = 1; while(Run) { sleep(1); EnterMutex(); counter += nodeid; LeaveMutex(); } // Stop timer thread StopTimerLoop(&Exit); // Close CAN devices (and can threads) canClose(&slavedic_Data); return 0; }