有没有办法在tkinter

时间:2019-06-28 09:01:12

标签: python tkinter arduino communication

我正在尝试通过USB电缆将RaspberryPi 3B +中的字符串发送到Arduino Uno。我使用Tkinter从GUI获取字符串中的值。当我按下Tkinter按钮时,我希望RasPi发送这些值。

def dieseFarbe():

    r = Sliderred.get ()
    g = Slidergreen.get ()
    b = Sliderblue.get ()
    w = Sliderwhite.get ()
    f = Sliderfader.get()

    value_string = "{},{},{},{}".format(r, g, b, w)

    #ser.write(value_string)

GUI完美运行。

print(value_string)

没有问题。

ser.write(value_string)

导致以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
    return self.func(*args)
  File "/home/pi/python-GUI/gui.py", line 22, in dieseFarbe
ser.write(value_string)
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 518, in write
d = to_bytes(data)
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 63, 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: '1,1,1,1'

1 个答案:

答案 0 :(得分:0)

错误告诉您您到底需要做什么:将字符串编码为字节。

您只能通过串行连接发送字节,但是python3的str是字符串“ characters”;也就是说,如果您的GUI发送了Unicode字符,则串行库将不知道如何将其编码为字节。

您可以通过以下操作解决此问题:

ser.write(value_string.encode())

encode("ascii")encode("utf-8")(以适当的为准)