I discovered a few days ago that receiving data from a balanced line using RS485 standard requires connecting the A and B lines to the correct poles. I.e. The line with the higher default state DC voltage needs to be connected to its corresponding input on the slave (mostly "A"), and B (lower default voltage) from master needs to be connected to B on the slaves.
Now is there a way in software to compensate for swapping of the A & B lines?
This for controllers that do not use a UART chip (ie Pro Mini, ..), and receive data from the MAX485 chip directly on the TX '(connected to DI) and RX (connected to RO) terminals.
Here is the sample code I used for this particular instance.
How can/should that be changed so that the slave becomes impervious to incoming polarity on A and B?
/* http://www.utrainia.com/65-arduinocmri-and-rs485
/* Arduino CMRI and RS485
*/
#include <Auto485.h>
#include <CMRI.h>
#define CMRI_ADDR 0
#define DE_PIN 2
#define LED_PIN 13
Auto485 bus(DE_PIN); // Arduino pin 2 -> MAX485 DE and RE pins
CMRI cmri(CMRI_ADDR, 24, 48, bus); // defaults to a SMINI with address 0. SMINI = 24 inputs, 48 outputs
void setup() {
pinMode(LED_PIN, OUTPUT);
bus.begin(19200);
}
void loop() {
// 1: main processing node of cmri library
cmri.process();
// 2: update output. Reads bit 0 of T packet and sets the LED to this
digitalWrite(LED_PIN, cmri.get_bit(0));
// 3: update input. Flips a bit back and forth every second
cmri.set_bit(0, (millis() / 1000) % 2 == 0);
}