RPi to Arduino thru rs485 not receiving all characters

I asked this same question on StackOverflow but I think that I will find more skillable people here about the topic...

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...

It seems like you need help with your Python code and this is not the place for that.

Does your system work if you connect directly to the RPI using a USB connection?

This Python - Arduino demo may help.

...R

Use Serial.flush() instead of the delay(10); in your arduino code as it sounds like you switching the control pin from TX to RX before all the serial data is sent.

Robin2 I am not asking for help with my Python code, as I said:

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

so its not the Python code fault... This parts are just a small part of my home automation system and the code that I posted here are just the part that is connected with the issue I have, the full Python script is more than 300 lines of code and the Arduino code is more than 100 lines of code (in near future I plan to switch to pure C++ code in Atmel Studio because the Arduino code is way too heavy)... Thanks for your suggestion and thanks for trying to help.

Riva thanks for your suggestion, I will test your solution and report if it works... You are right I am switching the "rs485CtrlPin" pin for TX and RX since the Arduino Nano is the slave in my setup and MAX485 requires a control pin to be switched HIGH when in transmitting mode... The USB to RS485 does this automatically but for the Serial to RS485 converter you need to do this manually...

By the way, the USB to RS485 converter is made using CH340 (which is USB to Serial chip) and MAX485 (which is Serial to RS485 chip), I suspect that it might be a problem with the ability of the CH340 but I cant be sure until I get another Serial to RS485 converter and test this...

svarc:
You are right I am switching the "rs485CtrlPin" pin for TX and RX since the Arduino Nano is the slave in my setup and MAX485 requires a control pin to be switched HIGH when in transmitting mode...

From your example code I assume you have both RE & DE connected to pin 6 and not just one of them.

Riva yes, they are both connected and controlled as rs485CtrlPin... I tried your suggestion with the Serial.flush() it doesnt change the result unfortunately :frowning:

svarc:
Riva yes, they are both connected and controlled as rs485CtrlPin... I tried your suggestion with the Serial.flush() it doesnt change the result unfortunately :frowning:

Just to be sure can you put a delay(1000); after the Serial.flush and before the direction change just to see if the whole string is sent. This problem seems so much like the data is not being sent correctly and this should make sure.

I'm not up on Python programming but ser.readline() can timeout if the EOL character is not received in time. What is the defined timeout & EOL character?

Riva 9600 baud rate is 1200Bps, that is 12B in 10ms, so your suggestion holds water... If we calculate in the starting bit and "\n\r" its equaling the 12B transfer in the time of the 10ms delay... I will test this first thing tomorrow...

ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)
I set the timeout to be 0ms so if the ser.readline() fails its instantly thinking that its the end of transmission... The Python code 100% is not the problem since when I connect the Arduino Nano using the same converters to my PC using the Serial Monitor of the Arduino IDE I get the same results...

The difference between sending the data over serial cable and sending over Lora is the Lora link will be using packet information and the receiver module could maybe be drip feeding to the RPi, the cable will be a continual stream. Maybe try adding a longer timeout to the Python code just to test and be sure that is not a problem.

Sorry for not replying so long but for some reason I was not able to login to the forum...

Riva your suggestion about making the delay longer between sending data and turning off the transmit pin on the Arduino side worked...

So to be able to use all the full 1200 bytes bandwidth that is available using the 9600 baud rate in any data transfer protocols, the data needs 1 second to be transferred successfully (which when i think now is obvious but I didn't even think about it until Riva suggestion).

Thanks Riva for your help and for the real solution to the problem.

I do not understand why Serial.flush() did not work. Assuming your using Arduino IDE > 1.0 then it should block until the last byte in the Serial write buffer is sent.

Glad you have the problem licked though.