iCub-main
PlxCan.cpp
Go to the documentation of this file.
1 // -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4 * Copyright (C) 2008 Robotcub Consortium
5 * Author: Lorenzo Natale
6 * CopyPolicy: Released under the terms of the GNU GPL v2.0.
7 *
8 */
12 
13 #include "PlxCan.h"
14 #include "PexApi.h"
15 #include <yarp/dev/CanBusInterface.h>
16 #include <yarp/os/Bottle.h>
17 
18 using namespace yarp::dev;
19 using namespace yarp::os;
20 
22 {
23  handle = new PLXCAN_HANDLE;
24 }
25 
27 {
28  if (handle!=0)
29  delete handle;
30 }
31 
32 bool PlxCan::canSetBaudRate(unsigned int rate)
33 {
34  printf("PlxCan::canSetBaudRate not yet implemented\n");
35  return true;
36 }
37 
38 bool PlxCan::canGetBaudRate(unsigned int *rate)
39 {
40  printf("PlxCan::canSetBaudRate not yet implemented\n");
41  *rate=0;
42  return true;
43 }
44 
45 bool PlxCan::canIdAdd(unsigned int id)
46 {
47  int res=plxCanIdAdd(*handle, id);
48  if (res!=NTCAN_SUCCESS)
49  return false;
50 
51  return true;
52 }
53 
54 bool PlxCan::canIdDelete(unsigned int id)
55 {
56  printf("PlxCan::canIdDelete not yet implemented\n");
57  return true;
58 }
59 
60 bool PlxCan::canRead(CanBuffer &msgs,
61  unsigned int size,
62  unsigned int *read,
63  bool wait)
64 {
65  int res;
66  *read=size;
67  PLXCAN_MSG *tmp=reinterpret_cast<PLXCAN_MSG *>(msgs[0].getPointer());
68  if (wait)
69  res=plxCanRead(*handle, tmp, read);
70  else
71  res=plxCanTake(*handle, tmp, read);
72 
73  if(res!=NTCAN_SUCCESS)
74  return false;
75 
76  return true;
77 }
78 
79 bool PlxCan::canWrite(const CanBuffer &msgs,
80  unsigned int size,
81  unsigned int *sent,
82  bool wait)
83 {
84  int res;
85  *sent=size;
86 
87  CanBuffer &buffer=const_cast<CanBuffer &>(msgs);
88  const PLXCAN_MSG *tmp=reinterpret_cast<const PLXCAN_MSG *>(buffer[0].getPointer());
89 
90  if (wait)
91  res=plxCanWrite(*handle, const_cast<PLXCAN_MSG *>(tmp), sent);
92  else
93  res=plxCanSend(*handle, const_cast<PLXCAN_MSG *>(tmp), sent);
94 
95  if (res!=NTCAN_SUCCESS)
96  return false;
97 
98  return true;
99 }
100 
101 bool PlxCan::open(yarp::os::Searchable &par)
102 {
103  int txQueueSize=0;
104  int rxQueueSize=0;
105  int netId =-1;
106  int txTimeout=0;
107  int rxTimeout=0;
108 
109  netId=par.check("CanDeviceNum", Value(-1), "numeric identifier of the can device").asInt32();
110  if (netId == -1) netId=par.check("canDeviceNum", Value(-1), "numeric identifier of the can device").asInt32();
111 
112  txTimeout=par.check("CanTxTimeout", Value(0), "timeout on transmission [ms]").asInt32();
113  if (txTimeout == 0) txTimeout=par.check("canTxTimeout", Value(0), "timeout on transmission [ms]").asInt32();
114 
115  rxTimeout=par.check("CanRxTimeout", Value(0), "timeout on receive when calling blocking read [ms]").asInt32() ;
116  if (rxTimeout == 0) rxTimeout=par.check("canRxTimeout", Value(0), "timeout on receive when calling blocking read [ms]").asInt32() ;
117 
118  int res = plxCanOpen (netId, txQueueSize, rxQueueSize, txTimeout, rxTimeout, handle);
119  if (res != NTCAN_SUCCESS)
120  {
121  fprintf(stderr, "PlxCan::open() returning false\n");
122  return false;
123  }
124 
125  return true;
126 }
127 
129 {
130  int res;
131  if (!handle)
132  return false;
133 
134  res=plxCanClose (*handle);
135 
136  if (res!=NTCAN_SUCCESS)
137  return false;
138 
139  delete handle;
140  handle=0;
141  return true;
142 }
143 
144 bool PlxCan::canGetErrors(CanErrors &err)
145 {
146  PLXCAN_ERRORS error;
147  if (plxCanErrors(*handle, &error)!=NTCAN_SUCCESS)
148  return false;
149 
150  err.busoff=error.busoff;
151  err.txCanErrors=error.errors;
152  err.rxCanErrors=error.canOvr;
153  err.rxCanFifoOvr=error.canOvr;
154  err.txCanFifoOvr=error.inputBuffOvr;
155  err.rxBufferOvr=error.inputBuffOvr;
156  err.txBufferOvr=error.outputBuffOvr;
157 
158  return true;
159 }
virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *read, bool wait=false)
Definition: PlxCan.cpp:60
virtual bool close()
Definition: PlxCan.cpp:128
virtual bool open(yarp::os::Searchable &par)
Definition: PlxCan.cpp:101
virtual bool canSetBaudRate(unsigned int rate)
Definition: PlxCan.cpp:32
virtual bool canIdDelete(unsigned int id)
Definition: PlxCan.cpp:54
virtual bool canGetErrors(CanErrors &errs)
Definition: PlxCan.cpp:144
virtual bool canIdAdd(unsigned int id)
Definition: PlxCan.cpp:45
virtual bool canGetBaudRate(unsigned int *rate)
Definition: PlxCan.cpp:38
virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false)
Definition: PlxCan.cpp:79
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
Definition: ethParser.cpp:92
static pcap_t * handle
fprintf(fid,'\n')