Skip to content
Snippets Groups Projects
Verified Commit 1110e8e2 authored by Moritz Lammerich's avatar Moritz Lammerich
Browse files

fix spurious false failures during SDO transfers

SDO transfers could fail even though the answer frame was actually
received if the CAN client's receive buffer was already full before
starting the transfer. Flush the receive buffer before starting the
transfer and try receiving as many times as the can be frames in a full
buffer to make sure we don't miss any received answer frames.
parent 6c99c447
Branches
No related tags found
No related merge requests found
......@@ -19,10 +19,11 @@
*/
class CAN {
public:
static constexpr size_t bufferSize = 16;
class CANSocket {
std::thread thread;
// this MUST be a list! It is paramount that modifying the list does not invalidate any iterators
std::list<channels::Channel<can_frame, 16>> receiveChannels;
std::list<channels::Channel<can_frame, bufferSize>> receiveChannels;
std::mutex receiveChannelMutex;
std::atomic_bool running{true};
public:
......
......@@ -78,12 +78,13 @@ int CANopen::SDOupload(uint8_t node, uint16_t index, uint8_t subindex, uint32_t
frame.payload.sdo.indexMSB = index >> 8;
frame.payload.sdo.subindex = subindex;
frame.dataLen = 8;
can.flush();
if (frameSend(frame ) != 0) {
if (debug) cout << "DEBUG: Sending not successfull" << endl;
return 1;
}
for(auto tries = 0; tries < 10; ++tries) {
for(auto tries = 0; tries < CAN::bufferSize; ++tries) {
if(frameRecv(frame) != 0) continue;
else if(node != frame.id) continue;
else if(frame.fnctCode != FNCT_CODE::SDO_TX) continue;
......@@ -121,12 +122,13 @@ int CANopen::SDOdownload(uint8_t node, uint16_t index, uint8_t subindex, uint32_
frame.payload.sdo.data[3] = data >> 24;
frame.dataLen = 8;
can.flush();
if (frameSend(frame) != 0) {
if (debug) cout << "DEBUG: SDO sending was not successfull" << endl;
return -1;
}
for(int tries = 0; tries < 10; ++tries) {
for(int tries = 0; tries < CAN::bufferSize; ++tries) {
if(frameRecv(frame) < 0) {
if(debug) cout << "DEBUG: [SDO answer try " << tries << "] timed out waiting for frame\n";
continue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment