Help. equivalent device file binary write function to C lang

Need advice

I’m working on CAN4LINUX trying to use ruby to send and receive CAN
packet but not success.

This is my test codeine RUBY that doesn’t work

a = [0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x06, 0x10,
0x00, 0xE9, 0xED, 0xE2, 0xB6, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00]
File.open("/dev/can0", ‘wb’ ) do |output|
output.write a.pack(“C*”)
end

C

uint8_t can_pkg[] = {0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x06, 0x10, 0x00, 0xE9, 0xED, 0xE2, 0xB6, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00};

canfd = open("/dev/can0", O_RDWR | O_NOCTTY | O_NONBLOCK);

ret = write(canfd, can_pkg, 1); // ! count is number of frames, not
byte
printf(“write ret = %d\n”, ret);
close(canfd);

Please describe in detail, in what way it doesn’t work. The code looks
fine at first glance, but I don’t know about /dev/can0.

For testing, I would first write to a regular file and look at the
result using a hex viewer. By doing this, you see at least whether the
problem is in the logic of writing binary data, or has to do with some
peculiarity of the can0 device.

Ronald

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs