Report
3 years, 3 months ago.

mbedRPC write feature not working for Python 3?

Has anyone ran into issues with sending serial commands to the mbed using Python 3. I'm currently using Python 3.5 and Pyserial 2.7 (had issues with Pyserial >3).

_____Code___

from mbedRPC_py3 import *

serdev = 3

mbed = SerialRPC(serdev, 9600)

x = RpcDigitalOut(mbed, "LED1")

x.write(1)

/LED1/write 1

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "C:\...\mbedRPC_py3.py", line 99, in write

r = self.mbed.rpc(self.name, "write", [str(value)])

File "C:\...\mbedRPC_py3.py", line 56, in rpc

self.ser.write(str)

File "C:\...\serialwin32.py", line 283, in write

data = to_bytes(data)

File "C:\...\serialutil.py", line 76, in to_bytes

b.append(item) # this one handles int and str for our emulation and ints for Python 3.x

TypeError: an integer is required

_______

I'm clearly sending an int, but I think there is a problem because any value I write gets converted to a string in mbedRPC.py (In either "Class mbed_interface()" or "Class SerialRPC(mbed)"). I've been successfully sending serial commands to an mbed on another computer but I was using both Python and Pyserial versions 2

Question relating to:

Just an update, you have to use the .encode() method on strings in python 3. I changed self.ser.write(str) to self.ser.write(str.encode()) under def rpc in Class SerialRPC(mbed) in mbedRPC.py

posted by Marques Hardin 10 Jan 2016
Comment on this question
Be the first to answer this question.

To post an answer, please log in.