Page 1 of 1

RS485 pyserial code does not work.

Posted: Wed Jul 01, 2020 3:48 am
by jrmagnetics
I have scoured the Internet trying to find any contemporary working Python code that works besides that published by Pyserial examples.
Miniterm receives just fine, but does not transmit.

I am using Buster on a Model 2B. The RS485 interface works with other machines over serial lines. So, the hardware is not an issue. This is a code problem. USB to RS485 interface also works with canned code, but not anything Python except example code supplied by PySerial. Putty can receive, but not transmit.

This Unicode Byte improvement looks like a hack to me.

Any solutions?

Code: Select all

#!/user/bin/env python3
import RPi.GPIO as GPIO
#import threading
import serial
import serial.rs485
import os
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT);

ser = serial.Serial(
       port= '/dev/ttyAMA0',
       baudrate= 57600,
       parity= serial.PARITY_NONE,
       stopbits= serial.STOPBITS_ONE,
       bytesize= serial.EIGHTBITS,
       timeout=1
)

GPIO.output(11,1) #set high/transmit

if (ser.isOpen() == True):
    ser.close()
ser.open()
ser.reset_input_buffer()

line = 'A255I2\n'
i=0

while True:
    #ser.write(line.encode('ascii'))
    #ser.write(line.encode())
    ser.write(b'A255I2')
    print(line + str(i))
    i += 1
    time.sleep(3)

ser.close()
GPIO.cleanup()

Re: RS485 pyserial code does not work.

Posted: Wed Jul 01, 2020 4:15 pm
by 6by9
RS485 is a half-duplex interface, so something needs to control the direction of the transceiver. Often that will be the RTS line from the UART. Have you wired that up on the Pi?
What is your actual transceiver hardware?

Re: RS485 pyserial code does not work.

Posted: Wed Jul 01, 2020 4:48 pm
by danjperron