简体   繁体   English

Python串口写功能不起作用

[英]Python Serial write function not working

I am very new to python and trying to write data using serial and python 3. Please help with the error below. 我是python的新手,并尝试使用serial和python 3编写数据。请帮助解决下面的错误。

>>> import serial
>>> check=serial.Serial(1)
>>> print(check.name)
COM2
>>> check.write("test")
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    check.write("test")
  File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 283, in write
    data = to_bytes(data)
  File "C:\Python34\lib\site-packages\serial\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
import serial
check=serial.Serial(1)
check.write(b"test")

Previous versions of Python did a lot of the string to bytes conversion for you (personally I liked the old methods quite a lot). 以前的Python版本为你做了很多stringbytes转换(我个人非常喜欢旧的方法)。 Nowadays, in Python3+ you need to handle the data-type conversion yourself depending on your output/input, which makes more sense even though I hate it... 现在,在Python3 +中,您需要根据输出/输入自己处理数据类型转换,即使我讨厌它也更有意义...

This goes for sockets and numerous other objects as well. 这适用于套接字和许多其他对象。
So in order to send data via serial or sockets, all you need to do is convert your string into a bytes object. 因此,为了通过串口或套接字发送数据,您需要做的就是将字符串转换为字节对象。 Do this by declaring b"this is bytes" or bytes(MyString, 'UTF-8') . 通过声明b"this is bytes"bytes(MyString, 'UTF-8')做到这一点。

For instance: 例如:

import serial
check=serial.Serial(1)
data = "test"
check.write(bytes(data, 'UTF-8'))

When the machine (your pc) is working with data on lower levels (serial, sockets etc) it's used to working with binary code, that's the 01010101 you might have heard of. 当机器(你的电脑)使用较低级别的数据(串口,插座等)时,它习惯于使用二进制代码,这就是你可能听说过的01010101
But you're used to working with strings such as "Hello world" . 但是你习惯于处理诸如"Hello world"字符串。 There's a middle layer in between, which is closer to machine code but still usable and understandable by us humans, and that's byte object. 中间有一个中间层,它更接近机器代码,但我们的人类仍然可以使用和理解,那就是byte对象。

a -> 97 -> 01100001

Previously (as mentioned) python converted your a into 97 for you in order to prep it for the driver that handles the machine-code. 以前(如上所述)python将你的a转换为97 ,以便为处理机器代码的驱动程序做好准备。 This caused a lot of problems in the Python source and therefore it's decided that you yourself need to convert the data when you need to instead of Python trying to figure it out for you. 这在Python源代码中引起了很多问题,因此决定你需要在需要时转换数据而不是Python试图为你解决问题。 So before sending it to the socket layer (serial uses sockets as well) you simply convert it one step closer to something the machine will understand :) 所以在将它发送到套接字层之前(串口也使用套接字)你只需将它转换为机器将理解的东西更近一步:)

  • Here you'll find a complete list of what values each character have. 在这里,您可以找到每个角色所拥有的值的完整列表。

Ps: This is an oversimplification of how stuff works, if I got the terminology mixed up or something just leave a comment and I'll correct it. Ps:这是一个过于简单的东西如何工作,如果我把术语混淆或只是留下评论,我会纠正它。 This is just to explain WHY you put a b in front of your strings :) 这只是为了解释为什么你把b放在你的字符串前面:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM