0

I'm trying to print the information, which is received from the other ESP32 by CAN transceiver, on an OLED. The CAN bus is working perfectly, as the 'HELLO WORLD' is already be received. But I can't print the received 'HELLO WORLD' on OLED (also the OLED is work). The code shows below,

#include <CAN.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);

  Serial.println("CAN Receiver");
  CAN.setPins(16, 17);
  // start the CAN bus at 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = CAN.parsePacket();

  if (packetSize) {
    // received a packet
    Serial.print("Received ");

    if (CAN.packetExtended()) {
      Serial.print("extended ");
    }

    if (CAN.packetRtr()) {
      // Remote transmission request, packet contains no data
      Serial.print("RTR ");
    }

    Serial.print("packet with id 0x");
    Serial.print(CAN.packetId(), HEX);

    if (CAN.packetRtr()) {
      Serial.print(" and requested length ");
      Serial.println(CAN.packetDlc());
    } else {
      Serial.print(" and length ");
      Serial.println(packetSize);

      // only print packet data for non-RTR packets
      while (CAN.available()) {
        Serial.print((char)CAN.read());
      }
      Serial.println();
    }

    Serial.println();
  }
  delay(5000);

  display.clearDisplay();

  display.print((char)CAN.read());

  display.display();
}
9
  • 1
    To show code in your question, please edit your question, highlight the entire code segment, and click the '{}' button. It will be much easier for potential contributors to read it and answer your question. – JRobert Feb 26 '20 at 22:04
  • 1
    please format your code ... use the {} button or put ``` before and after your code – jsotola Feb 26 '20 at 22:05
  • Are we asking noah to fix his post formatting so he learns how to post a question the includes formatted code rather than editing his question and doing the formatting for him? – Duncan C Feb 26 '20 at 23:00
  • 6
    Just guessing but looks to me like you read the CAN message and print it character by character to the Serial console but you don't save those characters in a buffer as you get them from CAN.read(). So when you then try to display.print((char)CAN.read()); at that point there are no more characters to available for the CAN.read() to read so nothing is printed by the display.print(). I'm just guessing though. – Richard Chambers Feb 27 '20 at 2:09
  • 1
    @RichardChambers Thanks mate! you save my life. – noah Feb 29 '20 at 1:26
2

Just guessing but looks to me like you read the CAN message and print it character by character to the Serial console but you don't save those characters in a buffer as you get them from CAN.read().

So when you then try to display.print((char)CAN.read()); at that point there are no more characters to available for the CAN.read() to read so nothing is printed by the display.print().

So what you need to do is to read the characters into a buffer so that you save them after reading them so that you can use the text string for other purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.