Skip to content

Simultaneous sending and receiving of data via CAN0 #87

@brightproject

Description

@brightproject

I have two devices connected by a CAN line, based on SN65HVD230DR.
One device, let's call it device B, transmits CORRECTION_ID to the CAN bus, device B also receives data on the CORRECTION_ID change via the CAN line and writes it to the EEPROM, after the CORRECTION_ID has changed in the EEPROM, it starts transmitting the updated value to the CAN bus.
Here we are not interested in device B, we are more interested in device A, which receives the CORRECTION_ID value via the CAN bus and displays it on the display.
Device A is based on the esp32 and uses the esp32_can library for the CAN bus.
The code for reading the CAN0 bus is as follows:

CAN_FRAME message;

void printCANFrame(CAN_FRAME &frame) {

    if (frame.id == CORRECTION_ID) {
        if (frame.length == 8) {

        CORR_ID= (static_cast<int>(frame.data.uint8[2]) << 8) | 
                (static_cast<int>(frame.data.uint8[1]));
      }
    }

void canTask(void *pvParameters) {
  
    Serial.println("Start CAN bus setup");
    CAN0.setCANPins(GPIO_NUM_21, GPIO_NUM_18);
	if(CAN0.begin(500000))
	{
		Serial.println("Builtin CAN Init OK");
	} else {
		Serial.println("BuiltIn CAN Init Failed");
	}
	CAN0.watchFor(CORRECTION_ID, 0x7FF);
	Serial.println("CAN bus Ready");
 while (true) {
        if (CAN0.read(message)) {
            printCANFrame(message); 
        }
        vTaskDelay(2 / portTICK_PERIOD_MS);
  }
}

void setup()
{
    xTaskCreate(canTask, "CAN Task", 2048 + 1024, NULL, 2, &canTaskHandle);
}

void loop()
{
}

This code perfectly receives and filters messages from the CAN broadcast bus and on the display of the device A I see the value CORRECTION_ID.

Now I needed to send an updated value of CORRECTION_ID from device A to the CAN bus.
The code is as follows:

if (CAN0.read(message)) { 

  frame_tx.id = CORRECTION_ID;
  frame_tx.length = 3;
  frame_tx.extended = false;

  frame_tx.data.uint8[0] = 0x00;
  frame_tx.data.uint8[1] = CORR_ID;
  frame_tx.data.uint8[2] = CORR_ID>> 8;
  
  CAN0.sendFrame(message);
        
  printCANFrame(message);

  }

But after loading such code into device A, it stops receiving anything from the CAN bus, and does not transmit anything to the CAN bus either.
Consider this code as pseudo-code😀
I understand that in the while (true) {} loop, data will be transmitted and received to the CAN bus almost simultaneously, but I tried adding millis, like in the example:

https://github.com/collin80/due_can/blob/f88f42ef0a18f65f7d94157dec1d68dbba45ebe7/examples/CAN_SendingTest/CAN_SendingTest.ino#L48

In general, for some reason, there are no normal modern examples for the esp32_can library, there are many examples in the due_can library, but they also cannot boast of completeness and novelty.
It would be great to update the examples or fill the WIKI, without this the library is very difficult to use🙄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions