I have this USB to RS485 Converter plugged on the RPi and have this TTL to RS485 connected to the Arduino Nano. I am using 9600 baud rate. I need to send max 16 characters in each direction but when I try sending, on the receiving end I receive only the first 11 characters... If I connect the Arduino Nano using usb cable to the RPi the communication is going perfectly...
This is the Arduino code:
int rs485CtrlPin = 6;
void setup() {
pinMode(rs485CtrlPin, OUTPUT);
Serial.begin(9600);
while (!Serial);
}
void loop() {
digitalWrite(rs485CtrlPin, HIGH);
Serial.println("cmd1,12345678901");
delay(10);
digitalWrite(rs485CtrlPin, LOW);
delay(5000);
}
This is the Python code:
import serial
import time
import datetime
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print st + ": Session started!"
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)
while True:
try:
if(not(ser.isOpen())):
ser.open()
cmd = ser.readline()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print st + ": " + cmd
except Exception as ex:
error = "Exception is: " + ex.__str__()
if(not(ser == None)):
ser.close()
ser = None
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
print st + ": " + error
So instead receiving "cmd1,12345678901" I get "cmd1,123456".
Have in mind that even if I connect the Arduino Nano on my PC using the same RS485 converters and use the Serial Monitor of the Arduino IDE I get the same result... If I set the baud rate to 115200 I receive 2 more characters "cmd1,12345678" but since I will use the RS485 for more than 50m distance I might need to use max 9600 baud rate...