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();
}
1 Answer 1
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.
Explore related questions
See similar questions with these tags.
{}
button or put ``` before and after your codeCAN.read()
. So when you then try todisplay.print((char)CAN.read());
at that point there are no more characters to available for theCAN.read()
to read so nothing is printed by thedisplay.print()
. I'm just guessing though.