Python Forum
Help reading data from serial RS485
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help reading data from serial RS485
#2
Serial expects data to be bytes and you give it a str. I am surprised this didn't raise an exception. What version of Python are you using? In pre-3.0 Python str and bytes were the same. Now str is made up of unicode characters.

This is some serial code I extracted from a recent project. I did some editing, so this code may not run as is.
import serial

class SerialDevice():
    def __init__(self, port_name='COM1', baudrate=19200, timeout=0.1):
        self.port = serial.Serial()
        self.port.port = port_name
        self.port.baudrate = baudrate
        self.port.timeout = timeout

    def open(self, port_name=None, baudrate=None, timeout=None):
        if port_name is not None:
            self.port.port = port_name
        if baudrate is not None:
            self.port.baudrate = baudrate
        if timeout is not None:
            self.port.timeout = timeout

        self.close()

        self.port.open()
        if not self.port.is_open:
            raise IOError(f'Open failed on port {self.port.port}')

    def close(self):
        if self.port.is_open:
            self.port.close()

    def send_cmd(self, command):
        """Send command and read reply"""
        if not self.port.is_open:
            raise IOError('Port is not open')
        self.port.write(f'{command}\r'.encode())
        reply = self.port.read_until(b'\r')   # My device uses /r as a terminator
        if not reply:
            raise IOError('Read timeout error')
        return reply.decode()
The main things you are missing are encoding the strs to bytes before sending/writing with a corresponding decoding on the other side, and a timeout. Every serial program that uses read (and especially readline) should specify a timeout.
Reply


Messages In This Thread
Help reading data from serial RS485 - by korenron - Nov-10-2021, 02:24 PM
RE: Help reading data from serial RS485 - by deanhystad - Nov-10-2021, 06:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,880 Aug-24-2023, 07:56 AM
Last Post: gowb0w
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,244 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,430 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  help with code for USB-RS485 korenron 3 3,761 Nov-17-2022, 09:04 AM
Last Post: wiseweezer
  Reading Data from JSON tpolim008 2 1,142 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Help with WebSocket reading data from anoter function korenron 0 1,366 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 2,240 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Reading data to python: turn into list or dataframe hhchenfx 2 5,467 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Reading data from mysql. stsxbel 2 2,260 May-23-2021, 06:56 PM
Last Post: stsxbel
  reading canbus data as hex korenron 9 6,419 Dec-30-2020, 01:52 PM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020