0

I am trying to to send a number from serial ports on my Pi 3 to a motor driver, (RoboLCaw) by using Python.

I have read lots of articles on the internet about how to to do very simple number sending through serial ports but till now it has not worked.

The main error message that I am receiving is:

Traceback (most recent call last):
File "test.py", line 15, in <module>
ser.write("This is a test")
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 475, in write
n = os.write(self.fd, d)
TypeError: 'str' does not support the buffer interface

There are different solutions for that error, some of them say byte-arrays, some say strings and so on. I would appreciate any hint or guidance that let me know what kind of input I can enter by using the write function

Below is an example of a program that I have used to check the serial port write and read:

import serial
ser = serial.Serial(
  port='/dev/ttyAMA0',
  baudrate = 9600,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
  timeout=1
)
print ("Serial is open: " + str(ser.isOpen()))
print ("Now Writing")
ser.write("This is a test")
print ("Did write, now read")
x = ser.readline()
print ("got '" + x + "'")
ser.close()
  • Unless you are trying to talk to Bluetooth your serial settings are wrong See How-do-i-make-serial-work-on-the-raspberry-pi3 You also appear to have Python problems, but as you haven't listed the code who knows? – Milliways Mar 5 '17 at 21:43
  • Have you tried using python2 and python3? – scitronboy May 15 '18 at 22:29
  • shouldn't the port be ttyS0 because AMA0 targets bluetooth unless you switched it off. – MaNyYaCk Jul 20 '18 at 4:56
1

I see from your print statements (Using print() as function) that you are using python3. In this case write and read functions handle bytes objects.

For example, instead of using: ser.write("This is a test") use ser.write("This is a test".encode()) which converts "This is a test" to b'This is a test'.

This is one of the changes from Python2 to Python3.

0

I'm guessing that the problem has to do with python's changes in string handling. From Dive into python 3: "Python 2 had “strings” and “Unicode strings.” Python 3 has “bytes” and “strings.”

  • Thanks @rsp, you mean that format of input for "ser.write" function must be string such as common strings that we use?Here in below is an example of program that I have used to check serial port write and read: import serial ser = serial.Serial( port='/dev/ttyAMA0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) print ("Serial is open: " + str(ser.isOpen())) print ("Now Writing") ser.write("This is a test") print ("Did write, now read") x = ser.readline() print ("got '" + x + "'") ser.close() – Mehdi Sajjadi Mar 5 '17 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.