first
commit
01a1c6b8a3
|
|
@ -0,0 +1,78 @@
|
|||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
|
||||
// OLED setup
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 64
|
||||
#define OLED_ADDRESS 0x3C
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
||||
|
||||
// RS485 control pins
|
||||
#define DE_PIN 20
|
||||
#define RE_PIN 10
|
||||
|
||||
void setup() {
|
||||
// Initialize Serial1 for RS485 (TX=21, RX=9)
|
||||
Serial.begin(115200);
|
||||
Serial1.begin(1000000, SERIAL_8N1, 9, 21);
|
||||
|
||||
// Set DE and RE as outputs
|
||||
pinMode(DE_PIN, OUTPUT);
|
||||
pinMode(RE_PIN, OUTPUT);
|
||||
|
||||
// Enable receive mode
|
||||
digitalWrite(DE_PIN, LOW);
|
||||
digitalWrite(RE_PIN, LOW);
|
||||
|
||||
// Initialize OLED
|
||||
Wire.begin(3, 4); // SDA=3, SCL=4
|
||||
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
|
||||
while (true)
|
||||
; // Halt if display fails
|
||||
}
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.println("Waiting for RS485...");
|
||||
display.display();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial1.available()) {
|
||||
uint8_t buffer[32];
|
||||
int count = 0;
|
||||
|
||||
// Read all available bytes
|
||||
while (Serial1.available() && count < sizeof(buffer)) {
|
||||
buffer[count++] = Serial1.read();
|
||||
}
|
||||
|
||||
// Display on Serial
|
||||
Serial.print("recv: ");
|
||||
for (int i = 0; i < count; i++) {
|
||||
Serial.print("0x");
|
||||
if (buffer[i] < 0x10) Serial.print("0");
|
||||
Serial.print(buffer[i], HEX);
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Display on OLED
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.println("Received:");
|
||||
display.setCursor(0, 12);
|
||||
display.setTextSize(1);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
display.print("0x");
|
||||
if (buffer[i] < 0x10) display.print("0");
|
||||
display.print(buffer[i], HEX);
|
||||
display.print(" ");
|
||||
}
|
||||
display.display();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue