edouard@629: /* edouard@629: This file is part of CanFestival, a library implementing CanOpen Stack. edouard@629: edouard@629: Copyright (C): Cosateq GmbH & Co.KG edouard@629: http://www.cosateq.com/ edouard@629: http://www.scale-rt.com/ edouard@629: edouard@629: See COPYING file for copyrights details. edouard@629: edouard@629: This library is free software; you can redistribute it and/or edouard@629: modify it under the terms of the GNU Lesser General Public edouard@629: License as published by the Free Software Foundation; either edouard@629: version 2.1 of the License, or (at your option) any later version. edouard@629: edouard@629: This library is distributed in the hope that it will be useful, edouard@629: but WITHOUT ANY WARRANTY; without even the implied warranty of edouard@629: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU edouard@629: Lesser General Public License for more details. edouard@629: edouard@629: You should have received a copy of the GNU Lesser General Public edouard@629: License along with this library; if not, write to the Free Software edouard@629: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA edouard@629: */ edouard@629: edouard@629: /* edouard@629: example for application with CO-PCICAN card. edouard@629: */ edouard@629: edouard@629: #include edouard@629: #include "canfestival.h" edouard@629: edouard@629: /* only for usleep() */ edouard@629: #include edouard@629: edouard@629: #define CHTX 2 /* channel number of the CO-PCICAN */ edouard@629: #define CHRX 0 /* channel number of the CO-PCICAN */ edouard@629: edouard@629: void mySyncHandler( CO_Data* d ) edouard@629: { edouard@629: printf( " got a SYNC message...\n" ); edouard@629: } edouard@629: edouard@629: int main() edouard@629: { edouard@629: LIB_HANDLE driver; edouard@629: edouard@629: /* handles for additional CO-PCICAN functions */ edouard@629: int (*get_fd_of_port)( CAN_PORT port ); edouard@629: int (*co_pcican_enter_run_mode)( const int fd ); edouard@629: int (*co_pcican_enter_config_mode)( const int fd ); edouard@629: int (*co_pcican_select_channel)( const unsigned char channel, const unsigned int direction ); edouard@629: int (*co_pcican_configure_selected_channel)( const int fd, s_BOARD *board, const unsigned int direction ); edouard@629: edouard@629: s_BOARD bd; edouard@629: CO_Data myData; edouard@629: CAN_PORT port; edouard@629: Message myMessage; edouard@629: const char busname[] = "/dev/co_pcican-0"; edouard@629: const char baudrate[] = "1M"; edouard@629: int fd; edouard@629: edouard@629: memset( &myData, 0x00, sizeof(CO_Data) ); edouard@629: myData.CurrentCommunicationState.csSYNC = 1; edouard@629: myData.post_sync = mySyncHandler; edouard@629: bd.busname = (char*)busname; edouard@629: bd.baudrate = (char*)baudrate; edouard@629: edouard@629: printf( "This example sends three SYNCs from port#%u to port#%u with a CO-PCICAN card\n", CHTX, CHRX ); edouard@629: edouard@629: driver = LoadCanDriver( "/usr/local/lib/libcanfestival_can_copcican_linux.so" ); edouard@629: edouard@629: if( driver == NULL ) edouard@629: { edouard@629: /* something strange happenend */ edouard@629: return 0; edouard@629: } edouard@629: edouard@629: /* dynamic load of additional library functions */ edouard@629: get_fd_of_port = dlsym( driver, "get_fd_of_port" ); edouard@629: co_pcican_enter_run_mode = dlsym( driver, "co_pcican_enter_run_mode" ); edouard@629: co_pcican_enter_config_mode = dlsym( driver, "co_pcican_enter_config_mode" ); edouard@629: co_pcican_select_channel = dlsym( driver, "co_pcican_select_channel" ); edouard@629: co_pcican_configure_selected_channel = dlsym( driver, "co_pcican_configure_selected_channel" ); edouard@629: edouard@629: /* direction: 0=RX, 1=TX */ edouard@629: co_pcican_select_channel( CHRX, 0 ); edouard@629: co_pcican_select_channel( CHTX, 1 ); edouard@629: edouard@629: port = canOpen( &bd, &myData ); edouard@629: edouard@629: if( port == NULL ) edouard@629: { edouard@629: UnLoadCanDriver( driver ); edouard@629: edouard@629: /* something strange happenend */ edouard@629: return 0; edouard@629: } edouard@629: edouard@629: /* get file descriptor to control the opened device */ edouard@629: fd = get_fd_of_port( port ); edouard@629: edouard@629: co_pcican_enter_config_mode( fd ); edouard@629: co_pcican_configure_selected_channel( fd, &bd, 0 ); edouard@629: co_pcican_configure_selected_channel( fd, &bd, 1 ); edouard@629: co_pcican_enter_run_mode( fd ); edouard@629: edouard@629: memset( &myMessage, 0x00, sizeof(Message) ); edouard@629: myMessage.cob_id = 0x80; /* SYNC message */ edouard@629: myMessage.len = 1; edouard@629: myMessage.data[0] = 0xA5; edouard@629: edouard@629: /* SEND HERE */ edouard@629: canSend( port, &myMessage ); edouard@629: edouard@629: myMessage.data[0] = 0x5A; edouard@629: canSend( port, &myMessage ); edouard@629: edouard@629: myMessage.data[0] = 0xA5; edouard@629: canSend( port, &myMessage ); edouard@629: edouard@629: /* mySyncHandler() is called by the receive thread and shows a received SYNC message on console */ edouard@629: usleep( 1*1000*1000 ); /* 1s */ edouard@629: edouard@629: canClose( &myData ); edouard@629: edouard@629: UnLoadCanDriver( driver ); edouard@629: edouard@629: return 0; edouard@629: } edouard@629: