Page 1 of 1

RS485 uart trouble (solved)

Posted: Fri Jun 03, 2022 3:50 am
by wobbletop
Hi,
First time working with the ESP32 and interfacing with RS485.
I'm using this shield with a M3485 chip.
https://www.electrodragon.com/product/e ... ce-shield/
For now, I'm just trying to receive data over the RS485 interface and echo it to the serial monitor.

Code: Select all

#define TXD 17
#define RXD 16

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial2.begin(19200, SERIAL_8N1, RXD, TXD);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial2.available()) {
    inByte = Serial2.read();
    Serial.print(inByte, HEX);
  }
 

}
I'm seeing activity on the on the GIO ports but the only thing that gets sent to the serial monitor is zeros. "0000000"

The activity on the GPIO16 input looks like it's active low for some reason.

Image

Any tips on getting this working? Is this the right place to ask the question?

Thanks!

Re: RS485 uart trouble

Posted: Mon Jun 06, 2022 1:12 am
by Angle Gao
Don't use Serial1 and Serial2! Use HardwareSerial to define your Uart and name it to different name.

HardwareSerial RS485Uart(1);

void setup() {

DongleUart.begin(19200, SERIAL_8N1, RXD,TXD);

}

Re: RS485 uart trouble

Posted: Mon Jun 06, 2022 7:06 pm
by wobbletop
Thanks for replying! It's appreciated.

I made some changes to the code as suggested, but I'm still just getting "0000" zeros.

Code: Select all

HardwareSerial pump(2);
#define TXD 17
#define RXD 16

char lastByte = 0;
char inByte = 0;
int count = 0;

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial.println("Starting...");
  pump.begin(19200, SERIAL_8N1, RXD, TXD);
}

void loop() {
  if (pump.available()) {
    inByte = pump.read();
    Serial.print(inByte, HEX);
    count++;
  }
  //insert CR to make output readable
  if (count >= 40) {
    count = 0;
    Serial.println("");
  }
  //check if start of packet
  if (lastByte == 0x10 && inByte == 0x03) {
    Serial.println();
  }
  lastByte = inByte;
}

Re: RS485 uart trouble (solved)

Posted: Tue Jun 07, 2022 7:53 pm
by wobbletop
Turns out the baud rate was wrong and I should have figured that out from the waveform.

I used the automatic baud rate detection feature.

Code: Select all

HardwareSerial pump(2);
#define TXD 17
#define RXD 16

char lastByte = 0;
char inByte = 0;
int count = 0;

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  pump.begin(0, SERIAL_8N1, RXD, TXD);
  unsigned long detectedBaudRate = pump.baudRate();
     if(detectedBaudRate) {
       Serial.printf("Detected baudrate is %lu\n", detectedBaudRate);
     } else {
       Serial.println("No baudrate detected, Serial1 will not work!");
     }
  Serial.println("Starting...");

}

void loop() {
  if (pump.available()) {
    inByte = pump.read();
    Serial.print(inByte, HEX);
    count++;
  }
  if (count >= 40) {
    count = 0;
    Serial.println("");
  }
  if (lastByte == 0x10 && inByte == 0x03) {
    Serial.println();
  }
  lastByte = inByte;
}