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