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