Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not receive CAN packets with DLC=0 #4

Open
balazsracz opened this issue Aug 14, 2023 · 8 comments
Open

Can not receive CAN packets with DLC=0 #4

balazsracz opened this issue Aug 14, 2023 · 8 comments

Comments

@balazsracz
Copy link

The API for parsePacket is defined as returning 0 when there is no packet in the input buffer, and returning DLC when there is a packet to read in the input buffer. Therefore packets with DLC=0 will appear the same as when no packets have arrived.

As a consequence of this confusion, the callback onReceive will not be executed when a packet with DLC=0 arrives on the CAN-bus.

Packets with DLC=0 (no data payload bytes) are perfectly valid packets on a CAN-bus. They should be able to be sent to and received from the bus using this library.

@RufusVS
Copy link

RufusVS commented Mar 23, 2024

I verified this issue and the example in Adafruit_MCP2515 has the same flaw. It can be worked around by checking the packetId in the other library because it is set to -1 when there is no message. The driver here doesn't do that, so it will have to be modified.

@fastbike
Copy link

fastbike commented Apr 26, 2024

I am experiencing this same problem. I have a node from another system that is sending a message with RTR set and DLC = 0.
The message is not being intercepted by this library, which makes it a bit useless for my purposes (my system starts by scanning all nodes - sending an RTR message - and the receiving node is needs to answer to identify its type.)

My work around is to comment out the code at the start of _parsePacket and to change the last return statement to this

  //https://github.com/adafruit/Adafruit_CAN/issues/4
  if (!hw->RXF0S.bit.F0FL) {
    return -1;
  }
  return _rxDlc;

@caternuson
Copy link
Contributor

The general issue with the existing logic is pretty simple and obvious. However, the question about what the behavior should be seems to still be a point of discussion. See discussion here:
sandeepmistry/arduino-CAN#7
and here:
sandeepmistry/arduino-CAN#22

@TheKitty
Copy link

@caternuson so this issue is in limbo? We have two comments that the receive code on the guide doesn't work.

@balazsracz
Copy link
Author

balazsracz commented May 14, 2024 via email

@caternuson
Copy link
Contributor

@TheKitty They should post in the forums with details. This issue is for a specific scenario. More than just "receive code doesn't work", which could be anything (soldering, etc.).

@balazsracz Yes, question is what's a good suggested API update to deal with this scenario, ideally one that is non-breaking change (i.e., backwards compatible). It's tricky, since the return is currently an int, which leads to 0 being ambiguous.

@balazsracz
Copy link
Author

TBH I described pretty well what the issue is in the opening post here. The minimum change that is necessary is for the onReceive callback to be invoked for a zero-DLC incoming CAN frame.

There are perfectly valid protocols that use zero-DLC frames for perfectly valid reasons. I don't think that can be argued.

So until there is a change in the implementation of this driver, fixing onReceive or adding a new API that actually is usable, I will keep recommending to people not to purchase the adafruit CAN board, because the software supporting it is incompatible with the protocol I care about.

(Btw This is not the only flaw in the API. There are some more in the email linked above. For example this entire parsePacket API is fundamentally flawed, because an interrupt will asynchronously overwrite all local variables even if the application code has not read them out yet. So this API has a conceptual race condition.)

@caternuson
Copy link
Contributor

There is a proposed fix in the following pull request:
#6

Please test that with your setups to see if it solves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants