我的Python 3代码:
from tkinter import *
import serial
import struct
ser = serial.Serial('COM4',9600)# establish a connection with port
root = Tk()
root.geometry('500x500+50+50')
root.title('ARDIUNO LED SLIDER')
##################################
def print_value(val):
#struct.pack('>B', 45)# tried this but not working
ser.write(str(val).encode())
print(ser.readline())
###############################
#Note that to pass the from option as a keyword argument,
# you need to add a trailing underscore (from is a reserved keyword in Python).
w = Scale(root, from_=0, to=100,length=400, orient=HORIZONTAL,command=print_value)
w.pack(side=TOP,anchor=S)
root.mainloop()
我的Arduino代码:
这是在Uno上完成的Arduino代码
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led,OUTPUT);// declare pin 9 to be an o/p
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) { // looking for availability of data in serial monitor
data= Serial.read();// read the data from serial monitor
//Serial.println(int(data));
Serial.write(data);
delay(100);
}
}
我得到的错误是:
其他时候移动滑块时出现以下错误消息
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: '10'