629
|
1 |
/*
|
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack.
|
|
3 |
|
|
4 |
Copyright (C): Cosateq GmbH & Co.KG
|
|
5 |
http://www.cosateq.com/
|
|
6 |
http://www.scale-rt.com/
|
|
7 |
|
|
8 |
See COPYING file for copyrights details.
|
|
9 |
|
|
10 |
This library is free software; you can redistribute it and/or
|
|
11 |
modify it under the terms of the GNU Lesser General Public
|
|
12 |
License as published by the Free Software Foundation; either
|
|
13 |
version 2.1 of the License, or (at your option) any later version.
|
|
14 |
|
|
15 |
This library is distributed in the hope that it will be useful,
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 |
Lesser General Public License for more details.
|
|
19 |
|
|
20 |
You should have received a copy of the GNU Lesser General Public
|
|
21 |
License along with this library; if not, write to the Free Software
|
|
22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 |
*/
|
|
24 |
|
|
25 |
/*
|
|
26 |
example for application with CO-PCICAN card.
|
|
27 |
*/
|
|
28 |
|
|
29 |
#include <stdio.h>
|
|
30 |
#include "canfestival.h"
|
|
31 |
|
|
32 |
/* only for usleep() */
|
|
33 |
#include <linux/delay.h>
|
|
34 |
|
|
35 |
#define CHTX 2 /* channel number of the CO-PCICAN */
|
|
36 |
#define CHRX 0 /* channel number of the CO-PCICAN */
|
|
37 |
|
|
38 |
void mySyncHandler( CO_Data* d )
|
|
39 |
{
|
|
40 |
printf( " got a SYNC message...\n" );
|
|
41 |
}
|
|
42 |
|
|
43 |
int main()
|
|
44 |
{
|
|
45 |
LIB_HANDLE driver;
|
|
46 |
|
|
47 |
/* handles for additional CO-PCICAN functions */
|
|
48 |
int (*get_fd_of_port)( CAN_PORT port );
|
|
49 |
int (*co_pcican_enter_run_mode)( const int fd );
|
|
50 |
int (*co_pcican_enter_config_mode)( const int fd );
|
|
51 |
int (*co_pcican_select_channel)( const unsigned char channel, const unsigned int direction );
|
|
52 |
int (*co_pcican_configure_selected_channel)( const int fd, s_BOARD *board, const unsigned int direction );
|
|
53 |
|
|
54 |
s_BOARD bd;
|
|
55 |
CO_Data myData;
|
|
56 |
CAN_PORT port;
|
|
57 |
Message myMessage;
|
|
58 |
const char busname[] = "/dev/co_pcican-0";
|
|
59 |
const char baudrate[] = "1M";
|
|
60 |
int fd;
|
|
61 |
|
|
62 |
memset( &myData, 0x00, sizeof(CO_Data) );
|
|
63 |
myData.CurrentCommunicationState.csSYNC = 1;
|
|
64 |
myData.post_sync = mySyncHandler;
|
|
65 |
bd.busname = (char*)busname;
|
|
66 |
bd.baudrate = (char*)baudrate;
|
|
67 |
|
|
68 |
printf( "This example sends three SYNCs from port#%u to port#%u with a CO-PCICAN card\n", CHTX, CHRX );
|
|
69 |
|
|
70 |
driver = LoadCanDriver( "/usr/local/lib/libcanfestival_can_copcican_linux.so" );
|
|
71 |
|
|
72 |
if( driver == NULL )
|
|
73 |
{
|
|
74 |
/* something strange happenend */
|
|
75 |
return 0;
|
|
76 |
}
|
|
77 |
|
|
78 |
/* dynamic load of additional library functions */
|
|
79 |
get_fd_of_port = dlsym( driver, "get_fd_of_port" );
|
|
80 |
co_pcican_enter_run_mode = dlsym( driver, "co_pcican_enter_run_mode" );
|
|
81 |
co_pcican_enter_config_mode = dlsym( driver, "co_pcican_enter_config_mode" );
|
|
82 |
co_pcican_select_channel = dlsym( driver, "co_pcican_select_channel" );
|
|
83 |
co_pcican_configure_selected_channel = dlsym( driver, "co_pcican_configure_selected_channel" );
|
|
84 |
|
|
85 |
/* direction: 0=RX, 1=TX */
|
|
86 |
co_pcican_select_channel( CHRX, 0 );
|
|
87 |
co_pcican_select_channel( CHTX, 1 );
|
|
88 |
|
|
89 |
port = canOpen( &bd, &myData );
|
|
90 |
|
|
91 |
if( port == NULL )
|
|
92 |
{
|
|
93 |
UnLoadCanDriver( driver );
|
|
94 |
|
|
95 |
/* something strange happenend */
|
|
96 |
return 0;
|
|
97 |
}
|
|
98 |
|
|
99 |
/* get file descriptor to control the opened device */
|
|
100 |
fd = get_fd_of_port( port );
|
|
101 |
|
|
102 |
co_pcican_enter_config_mode( fd );
|
|
103 |
co_pcican_configure_selected_channel( fd, &bd, 0 );
|
|
104 |
co_pcican_configure_selected_channel( fd, &bd, 1 );
|
|
105 |
co_pcican_enter_run_mode( fd );
|
|
106 |
|
|
107 |
memset( &myMessage, 0x00, sizeof(Message) );
|
|
108 |
myMessage.cob_id = 0x80; /* SYNC message */
|
|
109 |
myMessage.len = 1;
|
|
110 |
myMessage.data[0] = 0xA5;
|
|
111 |
|
|
112 |
/* SEND HERE */
|
|
113 |
canSend( port, &myMessage );
|
|
114 |
|
|
115 |
myMessage.data[0] = 0x5A;
|
|
116 |
canSend( port, &myMessage );
|
|
117 |
|
|
118 |
myMessage.data[0] = 0xA5;
|
|
119 |
canSend( port, &myMessage );
|
|
120 |
|
|
121 |
/* mySyncHandler() is called by the receive thread and shows a received SYNC message on console */
|
|
122 |
usleep( 1*1000*1000 ); /* 1s */
|
|
123 |
|
|
124 |
canClose( &myData );
|
|
125 |
|
|
126 |
UnLoadCanDriver( driver );
|
|
127 |
|
|
128 |
return 0;
|
|
129 |
}
|
|
130 |
|