-1

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()
Darth Vader
  • 4,096
  • 24
  • 42
  • 67
Mehdi Sajjadi
  • 1
  • 1
  • 1
  • 3
  • Unless you are trying to talk to Bluetooth your serial settings are wrong See [How-do-i-make-serial-work-on-the-raspberry-pi3](http://raspberrypi.stackexchange.com/a/45571/8697) You also appear to have Python problems, but as you haven't listed the code who knows? – Milliways Mar 05 '17 at 21:43
  • Have you tried using python2 and python3? – Benjamin Ashbaugh 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 04:56

2 Answers2

2

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.”

rsp
  • 622
  • 1
  • 6
  • 16
  • 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 05 '17 at 11:43