26 #include <yarp/dev/CanBusInterface.h>
27 #include <yarp/os/Bottle.h>
28 #include <yarp/os/Value.h>
29 #include <sys/types.h>
30 #include <yarp/os/Time.h>
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
34 #include <arpa/inet.h>
35 #include <linux/can.h>
36 #include <linux/can/raw.h>
37 #include <sys/ioctl.h>
56 using namespace yarp::os;
97 unsigned int *readout,
124 printf(
"Asked for %d messages\n", size);
126 for (i=0; i<size; i++)
128 can_frame *frm=
reinterpret_cast<can_frame *
>(msgs[i].getPointer());
131 bytes_read =
read( skt, frm,
sizeof(*frm) );
133 printf(
"finished reading. read %d bytes\n",bytes_read);
135 if (bytes_read<=0)
break;
138 printf(
"Read: %d \n ",bytes_read);
139 printf(
"len %d ", frm->can_dlc);
140 printf(
"id %d ", frm->can_id);
142 for(
int j=0;j<frm->can_dlc;j++)
143 printf(
"%2x ", frm->data[j]);
151 printf(
"Read %d messages\n", *readout);
213 for (i=0; i<size; i++)
215 CanBuffer &buffer=
const_cast<CanBuffer &
>(msgs);
216 const struct can_frame *
tmp=
reinterpret_cast<const struct can_frame*
>(buffer[i].getPointer());
224 fprintf(stderr,
"Error: SocketCan::canWrite() was unable to send message.\n");
231 fprintf(stderr,
"Error: SocketCan::canWrite() not all messages were sent.\n");
246 netId=par.check(
"CanDeviceNum", Value(-1),
"numeric identifier of the can device").asInt32();
247 if (netId == -1) netId=par.check(
"canDeviceNum", Value(-1),
"numeric identifier of the can device").asInt32();
249 txTimeout=par.check(
"CanTxTimeout", Value(500),
"timeout on transmission [ms]").asInt32();
250 if (txTimeout == 500) txTimeout=par.check(
"canTxTimeout", Value(500),
"timeout on transmission [ms]").asInt32();
252 rxTimeout=par.check(
"CanRxTimeout", Value(500),
"timeout on receive when calling blocking read [ms]").asInt32() ;
253 if (rxTimeout == 500) rxTimeout=par.check(
"canRxTimeout", Value(500),
"timeout on receive when calling blocking read [ms]").asInt32() ;
255 canTxQueue=par.check(
"CanTxQueue", Value(
TX_QUEUE_SIZE),
"length of tx buffer").asInt32();
258 canRxQueue=par.check(
"CanRxQueue", Value(
RX_QUEUE_SIZE),
"length of rx buffer").asInt32() ;
261 int so_timestamping_flags = 0;
263 skt = socket(
PF_CAN, SOCK_RAW, CAN_RAW );
267 sprintf (ifr.ifr_name,
"can%d",netId);
268 ioctl(skt, SIOCGIFINDEX, &ifr);
271 struct sockaddr_can addr;
273 addr.can_ifindex = ifr.ifr_ifindex;
274 bind( skt, (
struct sockaddr*)&addr,
sizeof(addr) );
277 if (-1 == (flags = fcntl(skt, F_GETFL, 0))) flags = 0;
278 fcntl(skt, F_SETFL, flags | O_NONBLOCK);
virtual bool canIdAdd(unsigned int id)
virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *read, bool wait=false)
virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false)
virtual bool open(yarp::os::Searchable &par)
virtual bool canIdDelete(unsigned int id)
virtual bool canSetBaudRate(unsigned int rate)
virtual bool canGetBaudRate(unsigned int *rate)
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
bool write(const std::string filename, const FullRegulation ®)