It turns out that the string needed to be turned into a bytearray and to do this I editted the code to
ser.write("%01#RDD0010000107**\r".encode())
This solved the problem
Answer from Garvin on Stack Overflowserial - How to use pyserial to write two separate message? - Arduino Stack Exchange
PySerial not sending string to serial port of Arduino
python - Ser.write() function input - Raspberry Pi Stack Exchange
help with sending string using pyserial
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.
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.”
I'm trying to (learn and) use python to send a serial command to a servo controller. My current code is as follows:
import serial
import time
ser=serial.Serial('COM7',9600)
i=0
print 'START'
while(i<5):
ser.write('#0P500S750<cr>')
time.sleep(2)
ser.write('#0P2500S750<cr>')
time.sleep(2)
print 'sweep'
i=i+1
print 'STOP'The device moves the servo based on the received serial command. #0P500S750<cr> translates to port 0, position 2500, speed 750. The command is terminated for the device with <cr>.
I'm not sure what I'm doing wrong. I am currently using python 2.7.13
thanks in advance
Good day. I'm trying to send a 'list' through a serial port. The list has three items. [string, string, tuple]
I can send individual strings without an issue, but when I try to send a list items it doesn't work. It says must be a 'string'.
Perhaps there is a way to easily convert a list into a 'string' or 'byte' object. I've tried that, but I have issues converting the 'list' from 'string' back to the original 'list' on the other side.
Any suggestions or guidance would be appreciated.
import serial
lst_data = []
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1)
if ser.isOpen == True:
ser.close()
def write(string):
if ser.isOpen() == False:
ser.open()
ser.write(string)
ser.close()I want to be able to pass 'write(['hello', 'world', ('hello','world')])'
Someone suggested using ",".join(list), but it doesn't work. I can't because list[2] is a tuple.
>>string_data = ",".join(data) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence item 2: expected string, tuple found