Gibberish RS485-output with BOB-10124 & Arduino Mega

I want to use a BOB-10124 RS-485 to let my Arduino Mega 2560 communicate with a peripheral (industrial) RS-485 device. But no matter what I do though, the resulting RS-485 output is gibberish.

Below is a minimal working code to reproduce the error. The board is connect to an RS485-to-USB (FTDI) hub. I’ve verified that the hub works well, same with the Serial ports on the Arduino.
The output that I receive with this code is gibberish: <0>^HIIO¤S^ž (or in HEX: 00 5E 48 49 49 4F A4 53 5E 9E).

Since it is always the same string, it feels like a timing error. The baud rate is correct though and altering it does not result in a readable string. Adding a 1 ms delay after Serial1.flush() to allow the chip’s buffer to empty before pulling the RTS low again, made no difference. I also tried a 10k resistor on the RTS-pin (as suggested elsewhere on the forum), also no solution.

Both a used BOB-10124 and a brand-new one show this strange behaviour. I have used this code previously with an ADM485 chip and that worked flawlessly. It must be something with this particular SP3485 chip that I’m missing.

Any thoughts on this? Thanks!

Edit: and now some really weird behaviour… I played around with the delays and switching the RTS-pin.
Without the delay after Serial1.flush() my terminal software continuously indicates a frame error. With this delay, the message is still corrupted though.
Altering the first delay - between setting the RTS-pin and writing the message - does not change the string I receive either. But when I remove this delay altogether, the received message is different.
Can anyone still make sense of this? :confused:

#include<Arduino.h>

const uint16_t buffersize = 1024;
const uint8_t RTS = 2;

uint32_t currTime;
uint32_t prevTime;
char data[buffersize];
char cmd[65];

//The setup function is called once at startup of the sketch
void setup()
{
 Serial1.begin(115200);
 pinMode(RTS, OUTPUT);
 digitalWrite(RTS, LOW);


 prevTime = millis();
}

// The loop function is called in an endless loop
void loop()
{
 currTime = millis();

 // Send data once per second
 if (currTime - prevTime >= 1000) {
 prevTime = currTime;

 // Send command
 strcpy(cmd, "Command\r");
 Send(cmd, data);
 }
}


void Send(const char *cmd, char *ret) {
 // use default send function
 sendCommand(cmd);

 // receive answer
 size_t bytes = Serial1.readBytesUntil('\r', ret, buffersize-1);
 // NULL-terminate to convert to string
 ret[bytes] = '\0';

 // debug: can be accessed from caller
 //strcpy(debugString, ret);

}

/*sendCommand(...)******************************************************************************
 * General function that sends command to RS485 peripheral device
 * <CR> is added to all commands
 * For RS485 communication, RTS pin has to be HIGH to allow writing to peripheral
 **********************************************************************************************/
void sendCommand(const char *cmd) {
 char tmp[32] = "";
 strcat(tmp, cmd);
 strcat(tmp, "\r"); // Add <CR>

 digitalWrite(RTS, HIGH); // set RTS to HIGH to allow writing to MAX485
 delay(1);
 Serial1.print(tmp); // send command
 Serial1.flush(); // Make sure message is fully transferred
 //delay(1);
 digitalWrite(RTS, LOW); // set RTS to LOW to receive answer

}

Was that bus terminated on both sides and was an idle state fixation in place? The Sparkfun board does only include that sides termination (with a rather strange 220Ω resistor). It also has no pull-up for the RO pin which makes this pin floating while sending out stuff.

Have you tried a different baud rate (on both sides)?

Please post a picture of your setup.

The receiver has a switchable 120 Ohm termination resistor. On or off, no difference. No termination resistor on the Arduino side, I will try that.
The RTS is pulled low after sending.

I did not try other baud rates as I need it to run at 115.2 kbps, which is also not extreme.

I had it hooked up like below. Strange enough, the board itself notes input voltage of 3-5 V, while the chip's datasheet clearly states 3.3V. I have been running them from 5V.

5V    -| VCC     |
TX1   -| RX/I   B|- Data-
RX1   -| TX/O   A|- Data+
Pin 2 -| RTS  GND|- n/c
GND   -| GND     |

The RTS is pulled low after sending.

I wrote nothing about RTS. RO is called TX-O on your board (it seems) and this must be pulled high to keep it from floating (I use a 10kΩ resistor for this). Also A and B are in a not defined state if both sides have their line driver disabled. To get to a defined state I use 470Ω resistors from A to Vcc and from B to GND. These two resistors must exist only once per bus. The termination resistors must be on both ends (to disable reflections).

Strange enough, the board itself notes input voltage of 3-5 V, while the chip's datasheet clearly states 3.3V. I have been running them from 5V.

The datasheet says the maximum Vcc voltage is 6V, so 3-5V seems to be fine.

I feel very stupid, but the answer was that both the RX & TX AND the Data +/- were switched. So the signal came out inverted.
Switching only one side resulted in no signal, so that's why I never thought of it.

With regard to your other comments, very helpful nevertheless. I will keep these in mind. So far communication works fine over a short distance, but I will try adding those for future purposes. Thanks!

I feel very stupid, but the answer was that both the RX & TX AND the Data +/- were switched. So the signal came out inverted.
Switching only one side resulted in no signal, so that's why I never thought of it.

I have no clue what your writing about. Is that in any relation to my answer?

pylon:
I have no clue what your writing about. Is that in any relation to my answer?

No, the gibberish was caused by me connecting both the Arduino and the peripheral exactly the wrong way out. So the signal came out inverted. Shame on me…