Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

Sending CAN-bus J1939 messages with no Data in linux

I need to send a CAN-bus J1939 message with no data using socketcan. The reason why the message needs to be sent with no data is to follow the spec J1939-73 DM11 (PGN=0x00FED3).

The C code below works fine when sending data that is at least 1 byte in length. When sizeof(dat) == 0, the sendto() function does not send the message at all. We can verify this with candump vcan0 .

Is there any way to send a message with 0 byte of data using socket(PF_CAN, SOCK_DGRAM, CAN_J1939) ? It can confirm that it can be done in raw CAN using socket(AF_CAN, SOCK_RAW, CAN_RAW) but I need it to work with the J1939 protocol socket.

How can we contact the kernel developers to submit a bug/enhancement request?

The J1939 linux kernel documentation can be found here: https://www.kernel.org/doc/html/v5.8/networking/j1939.html

Thank you.

#include <inttypes.h>
#include <linux/can/j1939.h>
#include <sys/socket.h>
#include <net/if.h>

int main() {
    int sock;
    const char device[] = "vcan0";
    uint8_t dat[2];
    dat[0] = 0;
    dat[1] = 1; 

    struct sockaddr_can baddr = {
        .can_family = AF_CAN,
        .can_addr.j1939 = {
            .name = J1939_NO_NAME,
            .addr = 0x20,
            .pgn = J1939_NO_PGN,
        },
        .can_ifindex = if_nametoindex(device),
    };

    sock = socket(PF_CAN, SOCK_DGRAM, CAN_J1939);
    bind(sock, (void *)&baddr, sizeof(baddr));

    struct sockaddr_can saddr = {
        .can_family = AF_CAN,
        .can_addr.j1939 = {
            .name = J1939_NO_NAME,
            .addr = 0x30,
            .pgn = 0xFED3,
        },
    };

    // sendto(sock, dat, sizeof(dat), 0, (const struct sockaddr *)&saddr, sizeof(saddr));
    sendto(sock, dat, 0, 0, (const struct sockaddr *)&saddr, sizeof(saddr));

    return 0;
}
question from:https://stackoverflow.com/questions/66049756/sending-can-bus-j1939-messages-with-no-data-in-linux

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...