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
#8
bytes is a collection of bytes (8 bit values). str is a collection of unicode characters. encode() converts a str to bytes. decode() converts bytes to a str.

You are not using decode() correctly. data = x.decode(), not data = str(x).decode().

str(x) does not convert x from bytes to a str. it asks the class bytes to make a string representation of x, usually for the purpose of printing. bytes creates a str containing a bunch of backslashes and hex codes thinking that since it is "bytes" you want to see the hex values of the bytes.

x.decode(encoding) converts the bytes (x) to a str. This consists of grabbing one or two bytes at a time and looking up the associated unicode character. Without an "encoding" argument it assumes bytes were 8 bit ascii characters

str(x).decode() is an error str does not have a decode() method. Why would it? decode() is used to convert something else to a str. If you are already a str, you don't need to be converted to a str.

Even though you probably don't need to encode or decode in Python2 (I've never used it so I don't know for sure), I would still use encode() and decode() and bytes if such things are supported. Then the code would be mostly compatible with Python3.
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-11-2021, 06:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading an ASCII text file and parsing data... oradba4u 2 220 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 5,761 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,493 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,502 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  help with code for USB-RS485 korenron 3 3,840 Nov-17-2022, 09:04 AM
Last Post: wiseweezer
  Reading Data from JSON tpolim008 2 1,213 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Help with WebSocket reading data from anoter function korenron 0 1,405 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 2,292 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Reading data to python: turn into list or dataframe hhchenfx 2 5,572 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Reading data from mysql. stsxbel 2 2,298 May-23-2021, 06:56 PM
Last Post: stsxbel

Forum Jump:

User Panel Messages

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