source: pacpussensors/trunk/CanGateway/driver/PeakCanDriver.cpp@ 22

Last change on this file since 22 was 1, checked in by DHERBOMEZ Gérald, 12 years ago

ajout des capteurs CanGateway et Alasca

File size: 6.4 KB
RevLine 
[1]1/********************************************************************
2// created: 2011/02/21 - 10:46
3// filename: PeakCanDriver.cpp
4//
5// author: Sergio Rodriguez
6//
7// version: $Id: PeakCanDriver.cpp srodrigu $
8//
9// purpose: Implementation of the PeakCanDriver class
10//
11*********************************************************************/
12
13
14#include "PeakCanDriver.h"
15#include <stdio.h>
16#include <cstring>
17#include <cstdlib>
18#include <iostream>
19
20#define DEFAULT_NODE "/dev/pcanusb0"
21#define WAIT_RECEIVING_FRAME_TIMEOUT 1000
22
23/**
24* Constructor which enables to initialize the different attributes of the class with default values.
25*/
26PeakCanDriver::PeakCanDriver (void)
27{
28 printf("Notice : PEAK CAN Driver used\n");
29 //szDevNode_ = DEFAULT_NODE;
30 mode_ = ReadOnly;
31}
32
33/**
34* Constructor which enables to initialize the different attributes of the class with default values.
35*/
36PeakCanDriver::PeakCanDriver (int channel)
37{
38 printf("Notice : PEAK CAN Driver used\n");
39 //szDevNode_ = DEFAULT_NODE;
40 mode_ = ReadOnly;
41}
42
43/**
44* Constructor which enables to initialize the different attributes of the class with default values.
45*/
46PeakCanDriver::PeakCanDriver (int channel, unsigned int bitRate)
47{
48 printf("Notice : PEAK CAN Driver used\n");
49 //szDevNode_ = DEFAULT_NODE;
50 mode_ = ReadOnly;
51}
52
53/**
54* Constructor which enables to initialize the different attributes of the class with default values.
55*/
56PeakCanDriver::PeakCanDriver(const char* port, const char* mode)
57{
58 szDevNode_ =(char*) malloc(14*sizeof(char));
59 strcpy(szDevNode_,DEFAULT_NODE);
60
61 printf("Notice : PEAK CAN Driver used\n");
62 //strcpy(szDevNode_, port);
63 printf("Driver to be connected at port: %s",szDevNode_);
64
65 if (!strcmp(mode,"ReadOnly")){
66 mode_ = ReadOnly;
67 printf(" in ReadOnly mode\n");
68 }
69 else if (!strcmp(mode,"WriteOnly")){
70 mode_ = WriteOnly;
71 printf(" in WriteOnly mode\n");
72 }
73 else if (!strcmp(mode,"ReadWrite")){
74 mode_ = ReadWrite;
75 printf(" in ReadWrite dual mode\n");
76 }
77 else{
78 mode_= ReadOnly;
79 printf(" in ReadOnly mode since mode was not identified.\n");
80 }
81}
82
83/**
84* Destructor which clean up the different attributs of the class.
85*/
86PeakCanDriver::~PeakCanDriver (void)
87{
88
89}
90
91/**
92* Member used to initialise the configuration of the CAN Card.
93* @see cleanUpPort (void)
94* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
95*/
96short PeakCanDriver::initPort (void)
97{
98
99 switch(mode_)
100 {
101 case ReadOnly:
102 canDeviceHandle_ = LINUX_CAN_Open(szDevNode_, O_RDONLY);
103 break;
104 case WriteOnly:
105 canDeviceHandle_ = LINUX_CAN_Open(szDevNode_, O_WRONLY);
106 break;
107 case ReadWrite:
108 canDeviceHandle_ = LINUX_CAN_Open(szDevNode_, O_RDWR);
109 break;
110 default:
111 printf("Error on CAN port operation mode selection.\n");
112 break;
113 }
114
115 return (!canDeviceHandle_)?1:PSUCCESS;
116}
117
118/**
119* Member used to clean up the configuration of the CAN Card.
120* @see initPort (void)
121* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
122*/
123short PeakCanDriver::cleanUpPort (void)
124{
125 if(canDeviceHandle_)
126 {
127 printf("Closing CAN Driver..");
128 CAN_Close(canDeviceHandle_);
129 return PSUCCESS;
130 }
131 printf("CAN Driver finished.\n");
132 return 1;
133}
134
135
136/**
137* Member which permit to send a frame on the CAN bus and test if the frame is well acknowledged.
138* @param flags a character which contain the flags of the sent frame.
139* @param dlc a character which defined the number of characters of the sent frame.
140* @param data a table of characters with the data of the sent frame.
141* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
142* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
143*/
144short PeakCanDriver::sendFrame (struct CanFrame frame)
145{
146 return PSUCCESS;
147}
148
149
150/**
151* Member which permit to receive of a frame on the CAN bus.
152* @param flags a character pointer which contain the flags of the received frame.
153* @param dlc a character pointer which defined the number of characters of the received frame.
154* @param data a pointer of table of characters with the data of the received frame.
155* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
156* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
157*/
158short PeakCanDriver::receiveFrame (struct CanFrame &frame)
159{
160 TPCANRdMsg message;
161 __u32 status;
162
163 //if ((errno = CAN_Read(canDeviceHandle_, &message)))
164 if ((errno = LINUX_CAN_Read_Timeout(canDeviceHandle_, &message, READ_TIMEOUT)))
165 {
166 perror("application: CAN_Read()");
167 return errno;
168 }
169 else
170 {
171 //print_message(&message.Msg);
172 // check if a CAN status is pending
173 if (message.Msg.MSGTYPE & MSGTYPE_STATUS)
174 {
175 status = CAN_Status(canDeviceHandle_);
176 if ((int)status < 0)
177 {
178 errno = nGetLastError();
179 perror("application: CAN_Status()");
180 return errno;
181 }
182 else
183 printf("application: pending CAN status 0x%04x read.\n", (__u16)status);
184 }
185 }
186
187 /* Return the flags field, dlc field and data field of the sent frame */
188 //frame.flags = pEvent->tagData.msg.flags;
189 frame.dlc = message.Msg.LEN; //ok
190 frame.id = message.Msg.ID;
191
192 if( ( frame.dlc > 8 ) || ( frame.dlc < 0 ))
193 frame.dlc = 8;
194 memcpy(frame.data,message.Msg.DATA, frame.dlc);
195
196 return PSUCCESS;
197}
198
199
200/**
201* Member which wait the reception of a frame on the CAN bus.
202* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
203* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
204*/
205void PeakCanDriver::waitReceivingFrame(void)
206{
207
208}
209// print out the contents of a CAN message
210void PeakCanDriver::print_message(TPCANMsg *m)
211{
212 int i;
213
214 // print RTR, 11 or 29, CAN-Id and datalength
215 printf("message: %c %c 0x%08x %1d ",
216 (m->MSGTYPE & MSGTYPE_RTR) ? 'r' : 'm',
217 (m->MSGTYPE & MSGTYPE_EXTENDED) ? 'e' : 's',
218 m->ID,
219 m->LEN);
220
221 // don't print any telegram contents for remote frames
222 if (!(m->MSGTYPE & MSGTYPE_RTR))
223 for (i = 0; i < m->LEN; i++)
224 printf("0x%02x ", m->DATA[i]);
225
226 printf("\n");
227}
Note: See TracBrowser for help on using the repository browser.