jrmagnetics
Posts: 1
Joined: Wed Jul 01, 2020 3:29 am

RS485 pyserial code does not work.

Wed Jul 01, 2020 3:48 am

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()

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 8991
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: RS485 pyserial code does not work.

Wed Jul 01, 2020 4:15 pm

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?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.


Return to “Interfacing (DSI, CSI, I2C, etc.)”