Documentation to use / test the driver in RTAI?

This forum covers PCAN-Linux and Linux development issues concerning our products
User avatar
S.Grosjean
Software Development
Software Development
Posts: 357
Joined: Wed 4. Jul 2012, 17:02

Re: Documentation to use / test the driver in RTAI?

Post by S.Grosjean » Mon 24. Oct 2016, 13:21

Hi,

I took time to read and modify your multiport.c program:

Code: Select all

$ sudo ./multiport_rt
pcan3 status = Broken pipe
pcan4 status = Broken pipe
Sending ... frame: m  s 0x00000008 4 01 02 03 04
Receiving ... frame: m  s 0x00000008 4 01 02 03 04
Here are my remarks:

1/ CAN_Open() enumerates ports from 1 (not from 0) so please change:

Code: Select all

#define CAN_PORT_1 1 // logical port number for physical port 1
#define CAN_PORT_2 2 // logical port number for physical port 2
2/ Add a test on rtx_task value (yes, it might be NULL, especially when you don't run with root privileges. This could prevent from swithing the Linux process into hard RT next):

Code: Select all

   trx_task = rt_task_init_schmod(102, 2, 0, 0, SCHED_FIFO, 0xF);
   if (!trx_task) {
        printf("rt_task_init_schmod() failed errno=%d\n", errno);
        exit(1);
   }
2/ DON't write "errno = CAN_xxx()" because errno is a global set by the system.
Write this instead (for example):

Code: Select all

   err = CAN_Read(h2, &msg); // this will block/wait until either an error occur
s or data is coming
   if (err) {
      printf("error %d while reading: %s\n", errno, strerror(errno));
      goto error;
   }
3/ DON'T write "strerror(CAN_Status())" since CAN_Status() doesn't return an errno (except in error cases). This is the explanation of the above Broken pipe message...

4/ Re-order the way you exit your program, by closing handles before exiting RT mode, to use the RT close entry points of the RT driver:

Code: Select all

error:
   // close ports
   CAN_Close(h2);
error_2:
   CAN_Close(h1);

   // back to user mode
exit_user:
   rt_make_soft_real_time();
   rt_task_delete(trx_task);
5/ You SHOULD add a signal handler for SIGINT so that ^C will be caught and you will be able to gently close all the handles by yourself.

6/ You SHOULD add a test on MSGTYPE when analysing a received msg since you might receive messages that are not CAN frames:

Code: Select all

   if (m->MSGTYPE & MSGTYPE_STATUS)
        printf("status: id=%u ", m->ID);
   else
        printf("frame: %c %c %c 0x%08x %1d ",
Regards,

Stéphane
— Stéphane

Post Reply