Синхронизировать pyserial с Arduino [дубликат] ⇐ Python
-
Anonymous
Синхронизировать pyserial с Arduino [дубликат]
I want to use Serial communication with Arduino in Python in order to make a motor move according to a specified color. I have a problem with pyserial synchronisation with Arduino.
Here is my Python code:
import serial # Serial object for communication with Arduino ser = serial.Serial(port='COM8', baudrate=9600, timeout=.1) # write distance and radius to Arduino through Serial Communication ser.write(str(length)) ser.write(bytes('#', "UTF-8")) ser.write(bytes(radius)) ser.write(bytes('/', "UTF-8")) and here's my Arduino code:
void loop() { while(Serial.available()) { inChar = Serial.read(); if(inChar == '#'){ while(Serial.available()){ inChar = Serial.read(); if(inChar == '/'){ break; } inputStringR += inChar; }break; } inputStringL += inChar; } and here's my error:
Traceback (most recent call last): File "d:\project gdevelop\belajar web1\clor.py", line 69, in ser.write(str(length)) File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialwin32.py", line 310, in write data = to_bytes(data) ^^^^^^^^^^^^^^ File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialutil.py", line 65, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: '267' How to resolve this issue?
Источник: https://stackoverflow.com/questions/745 ... th-arduino
I want to use Serial communication with Arduino in Python in order to make a motor move according to a specified color. I have a problem with pyserial synchronisation with Arduino.
Here is my Python code:
import serial # Serial object for communication with Arduino ser = serial.Serial(port='COM8', baudrate=9600, timeout=.1) # write distance and radius to Arduino through Serial Communication ser.write(str(length)) ser.write(bytes('#', "UTF-8")) ser.write(bytes(radius)) ser.write(bytes('/', "UTF-8")) and here's my Arduino code:
void loop() { while(Serial.available()) { inChar = Serial.read(); if(inChar == '#'){ while(Serial.available()){ inChar = Serial.read(); if(inChar == '/'){ break; } inputStringR += inChar; }break; } inputStringL += inChar; } and here's my error:
Traceback (most recent call last): File "d:\project gdevelop\belajar web1\clor.py", line 69, in ser.write(str(length)) File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialwin32.py", line 310, in write data = to_bytes(data) ^^^^^^^^^^^^^^ File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\serial\serialutil.py", line 65, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: '267' How to resolve this issue?
Источник: https://stackoverflow.com/questions/745 ... th-arduino