CAN_DEVCTL_READ_CANMSG_EXT

Read an extended CAN message

Synopsis:

#include <sys/can_dcmd.h>

#define CAN_DEVCTL_READ_CANMSG_EXT   __DIOF(_DCMD_MISC, CAN_CMD_CODE + 8, struct can_msg)

Arguments to devctl():

Argument Value
filedes A file descriptor that you obtained by opening the device
dcmd CAN_DEVCTL_READ_CANMSG_EXT
dev_data_ptr A pointer to a struct can_msg
n_bytes sizeof(struct can_msg)
dev_info_ptr NULL

Description:

This command reads an extended CAN message, blocking if there's no message available unless you opened the device with O_NONBLOCK.

Input:

None.

Output:

A filled-in can_msg structure, which is defined in <sys/can_dcmd.h> as follows:

#define CAN_MSG_DATA_MAX  0x8 /* Max number of data bytes in a CAN message
                                 as defined by CAN spec */

/* Extended CAN Message */
typedef struct can_msg_ext {
    uint32_t        timestamp;         /* CAN message timestamp */
    uint32_t        is_extended_mid;   /* 1=29-bit MID, 0=11-bit MID */
    uint32_t        is_remote_frame;   /* 1=remote frame request, 0=data frame */
} CAN_MSG_EXT;

/* CAN Message */
typedef struct can_msg {
        /* Pre-allocate CAN messages to the max data size */
        uint8_t        dat[CAN_MSG_DATA_MAX];  /* CAN message data */
        uint8_t        len;                    /* Actual CAN message data length */
        uint32_t       mid;                    /* CAN message identifier */
        CAN_MSG_EXT    ext;                    /* Extended CAN message info */
} CAN_MSG;

Errors:

The devctl() function can return the following, in addition to the error codes listed in its entry in the C Library Reference:

EAGAIN
There are no messages in the queue, and you opened the device with O_NONBLOCK.
EINVAL
The file descriptor doesn't correspond to a receive mailbox.

Example:

int      ret;
unsigned i;
uint8_t  dat[CAN_MSG_DATA_MAX];
char     dat_str[sizeof(dat) + 1]; /* max size of a CAN message + '\0' */

if( (fd = open( "/dev/can1/rx0", O_RDWR)) == -1 )
{
    printf("open of %s failed \n", devname);
    exit(EXIT_FAILURE);
}

if(EOK != (ret = devctl(fd, CAN_DEVCTL_READ_CANMSG_EXT, &canmsg, sizeof(canmsg),
                        NULL)))
{
    fprintf(stderr, "devctl CAN_DEVCTL_READ_CANMSG_EXT: %s\n", strerror(ret));
} else {
    printf("READ_CANMSG_EXT:\n");
    printf("mid = 0x%X\n", canmsg.mid);
    printf("timestamp = 0x%X\n", canmsg.ext.timestamp);
    printf("dat len = %d\n", canmsg.len);

    /* Copy the data */
    memcpy(dat, canmsg.dat, canmsg.len);

    /* Print the data */
    printf("dat =");
    for (i=0; i < canmsg.len; i++) {
        printf(" %.2x", dat[i]);
        dat_str[i] = isprint(dat[i]) ? dat[i] : '.';
    }
    dat_str[i] = '\0';
    printf("\t%s\n", dat_str);
}

See also:

devctl() in the QNX Neutrino C Library Reference

canctl in the Utilities Reference