2 This file is part of CanFestival, a library implementing CanOpen Stack.
6 See COPYING file for copyrights details.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * @author VScom (http://www.vscom.de)
28 * This file implements interface between CanFestival and the VSCAN API
29 * (supported VScom products SER-CAN, USB-CAN, NET-CAN).
31 * To build this interface following files will needed:
33 * va_can_api.lib (for Windows builds)
42 #include "vs_can_api.h" // for CAN_HANDLE
44 #include "can_driver.h"
46 /*********functions which permit to communicate with the board****************/
47 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
51 DWORD dwRead; /* number of read frames */
54 if (VSCAN_Read((VSCAN_HANDLE)fd0, Msg, 1, &dwRead) != VSCAN_ERR_OK)
56 printf("canReceive_driver (VScom): error receiving frame)\n");
60 /* identifier of the CAN frame */
61 m->cob_id = Msg[0].Id;
64 if (Msg[0].Flags == VSCAN_FLAGS_STANDARD)
69 /* width of the data bytes */
72 /* copy data bytes from the CAN frame, up to 8 */
73 for(i = 0 ; i < Msg[0].Size ; i++)
75 m->data[i] = Msg[0].Data[i];
81 /***************************************************************************/
82 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
86 DWORD dwWritten; /* number of written frames */
88 /* identifier of the CAN frame */
89 Msg[0].Id = m->cob_id;
93 Msg[0].Flags = VSCAN_FLAGS_STANDARD;
95 Msg[0].Flags = VSCAN_FLAGS_REMOTE;
97 /* width of the data bytes */
100 /* copy data bytes to the CAN frame, up to 8 */
101 for(i = 0 ; i < m->len; i++)
102 Msg[0].Data[i] = m->data[i];
104 /* copy CAN frame to the output buffer */
105 if (!(VSCAN_Write((VSCAN_HANDLE)fd0, Msg, (DWORD)1, &dwWritten) == VSCAN_ERR_OK && dwWritten))
107 perror("canSend_driver (VScom): error writing to output buffer.\n");
111 /* really send CAN frame */
112 if(VSCAN_Flush((VSCAN_HANDLE)fd0) != VSCAN_ERR_OK)
114 perror("canSend_driver (VScom): error flushing.\n");
122 /***************************************************************************/
123 int TranslateBaudeRate(char* optarg){
124 if(!strcmp( optarg, "1M")) return (int)VSCAN_SPEED_1M;
125 if(!strcmp( optarg, "500K")) return (int)VSCAN_SPEED_500K;
126 if(!strcmp( optarg, "250K")) return (int)VSCAN_SPEED_250K;
127 if(!strcmp( optarg, "125K")) return (int)VSCAN_SPEED_125K;
128 if(!strcmp( optarg, "100K")) return (int)VSCAN_SPEED_100K;
129 if(!strcmp( optarg, "50K")) return (int)VSCAN_SPEED_50K;
130 if(!strcmp( optarg, "20K")) return (int)VSCAN_SPEED_20K;
131 if(!strcmp( optarg, "none")) return 0;
135 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
139 baudrate = TranslateBaudeRate(baud);
143 if (VSCAN_Ioctl((VSCAN_HANDLE)fd, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
145 fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
152 /***************************************************************************/
153 CAN_HANDLE canOpen_driver(s_BOARD *board)
155 VSCAN_HANDLE fd0 = 0;
161 printf("bus %s ", board->busname);
162 fd0 = VSCAN_Open(board->busname, VSCAN_MODE_NORMAL);
165 fprintf(stderr, "canOpen_driver (VScom): error opening %s\n", board->busname);
166 return (CAN_HANDLE)fd0;
168 printf("(fd = %d)\n", fd0);
169 baudrate = TranslateBaudeRate(board->baudrate);
173 if (VSCAN_Ioctl((VSCAN_HANDLE)fd0, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
175 fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
179 return (CAN_HANDLE)fd0;
182 /***************************************************************************/
183 int canClose_driver(CAN_HANDLE fd0)
185 VSCAN_Close((VSCAN_HANDLE)fd0);