Newby Tyro
Posts: 7
Joined: Tue Mar 17, 2020 3:02 pm

Really simple problem

Wed Apr 15, 2020 4:29 pm

This is on Raspberry Pi model 2b, using python 3.7.3 and Raspbian - latest version as of a week ago

I need to push some data through the pi serial port. I find that I have to call python as root user in order to be permitted to open the serial port. I import serial, open the port ttyAMA0 at 9600 baud as ser, and use ser.write(b'Hello'). This works fine. BUT I want to send a message that is not a fixed string: I use msg = input('message = ? ') to get the data - that works fine. But I haven't yet found the necessary conversion to get ser.write() to accept - I know I need bytes, but nothing I have found in the python library or in PySerial has worked - yet. I have tried many many things, but python (or the Pi) finds something wrong with all of them.

If you are thinking of being kind enough to help, please be very specific - I seem to misunderstand rather easily!!

import serial
import time
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout = 4)
while True:
msg = input('message = ? ')
# here all the different attempts to manipulate msg
ser.write(.......)
rmsg = ser.read(100) #this works fine, too
....

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Really simple problem

Wed Apr 15, 2020 4:50 pm

Code: Select all

ser.write(msg.encode("utf-8"))

BluPants
Posts: 5
Joined: Mon Apr 13, 2020 9:26 pm

Re: Really simple problem

Wed Apr 15, 2020 5:13 pm

Try str.encode()
Last edited by BluPants on Sun Apr 19, 2020 5:21 am, edited 1 time in total.

Newby Tyro
Posts: 7
Joined: Tue Mar 17, 2020 3:02 pm

Re: Really simple problem

Wed Apr 15, 2020 6:49 pm

Thanks very much!! I searched for headings such as bytes, conversion, serial, you name it, but for some reason I did not think of encode - ?

Both replies are, I believe, equivalent - so thanks to you both for this simple solution to my simple problem!!

hippy
Posts: 7458
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Really simple problem

Wed Jul 08, 2020 4:44 pm

I have been battling a similar problem; trying to convert code which works fine in Python 2 and uses strings which contain 8-bit character values greater than 127 to something a Python 3 serial .write() will send accurately.

I gave up on .encode() and variants, other 'clever tricks', which did not actually work in practice, and ended up doing it by brute force creating a byte array to send. This works for me with Python 3.7.3 and using serial .write() as well as print -

Code: Select all

s = chr(0xFA) + chr(0xAF)

e = s.encode()

b = bytearray()
for c in s:
  b.append(ord(c))

print("Raw       :", len(s), s)
print("Encoded   :", len(e), e)
print("ByteArray :", len(b), b)

Code: Select all

Raw       : 2 ú¯
Encoded   : 4 b'\xc3\xba\xc2\xaf'
ByteArray : 2 bytearray(b'\xfa\xaf')

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Really simple problem

Wed Jul 08, 2020 5:23 pm

danjperron wrote:
Wed Apr 15, 2020 4:50 pm

Code: Select all

ser.write(msg.encode("utf-8"))
Maybe that should be

Code: Select all

ser.write(msg.encode("ascii", "replace"))
To get 8 bit ascii codes. It could depend what's on the receiving side. Does it understand utf-8?

Return to “Python”