0

I am working on a simple project using the SocketCAN library to send some messages over CAN. But now I am stuck with a problem that I can not explain by myself... When I am using the socket command write within a custom class it fails with invalid argument (errno: 22 - EINVAL). To find my problem I made the whole class public and by using the same functionalities as within my class in my main method it works...

Custom Class (not working):

void Can::SendFrame(const can_frame &canFrame) const
{
bcm_message request = CreateSingleNonCyclicFrameTransmissionRequest(canFrame);
int result = write(canSocket, &request, sizeof(request));
}

Main (working):

Can can;
bcm_message = can.CreateSingleNonCyclicFrameTransmissionRequest(canFrame);
int result = write(can.canSocket, &request, sizeof(request));

I am using a BCM socket. The socket is connected and working fine (from the main method).

Any idea what might be the problem?

Edit: If I create the can_frame within SendFrame() write() does not fail.

void Can::SendFrame(const can_frame &canFrame) const
{
can_frame testFrame;
testFrame.can_dlc = 1;
testFrame.can_id = 0x321;
testFrame.data[0] = 0xde;
auto request2 = CreateSingleNonCyclicFrameTransmissionRequest(testFrame);
int result2 = write(canSocket, &request2, sizeof(request2));
// as before:
bcm_message request = CreateSingleNonCyclicFrameTransmissionRequest(canFrame);
int result = write(canSocket, &request, sizeof(request));
}

Edit2: There was a difference in the flags field of the created requests. In request2 it is initialized with 0x01 and for request with 0x7effb80... No idea where this comes from... But explicitly setting it to 0x0 solved the problem

royalTS
  • 603
  • 4
  • 22
  • "not working" is not a useful question to post. What error(s) are you getting? – Andrew Henle Feb 21 '18 at 13:23
  • Where does `canSocket` come from in your class? Are you sure it has the same value as in main? – Mat Feb 21 '18 at 13:23
  • @Andrew Henle It fails with an errno saying that an argument is invalid, but I don't see what this might be and I can't spot any difference between the main part and the custom class part... – royalTS Feb 21 '18 at 13:25
  • @Mat it is a class member and is created before with an initialization. The debugger shows me the same value (0x03) for both the main and the class – royalTS Feb 21 '18 at 13:27
  • launch it under gdb, set breakpoints at `write` calls and check its arguments. If they are the different, there you go, if not, the problem is in the global state, I would swap the order of `write` calls then just to check. – Fusho Mar 10 '18 at 16:12

0 Answers0