问题描述
已编辑
你好程序员社区,
我在处理 Python 3.4 中的 pyserial 时遇到了一些问题首先我没有串口,所以我使用Eltima Software的Virtual Serial Port Driver 7.2"来成对创建虚拟串口,这意味着我可以尝试从这些端口发送和接收数据,在我的情况下我只是创建COM1 连接到 COM2,然后我通过 HW 组安装 Hercules SETUP 实用程序来监控这些串口,
所以理论上,如果我在 PYTHON 中发送(写入)数据,我可以在 HERCULES 中看到它,如此链接所示 https://www.youtube.com/watch?v=DItyttmpRtY
我正在尝试创建自己的代码
def mInitizalise():set_ser = serial.Serial(端口=COM1",波特率=9600,奇偶校验 = serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,字节大小 = 串行.EIGHTBITS,超时=1)set_ser.close()set_ser.open()如果 set_ser.isOpen():打印('打开:' + set_ser.portstr)temp = input('输入要发送的内容,按回车:\r\n')set_ser.write(temp)set_ser.close()
购买有误
>>>>>>打开:COM1输入您要发送的内容,按回车键:你好Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Python34\lib\idlelib\run.py",第 121 行,在 mainseq, request = rpc.request_queue.get(block=True, timeout=0.05)文件C:\Python34\lib\queue.py",第 175 行,在 get提高空队列.空在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):文件C:\Users\me\Downloads\SerialTest.py",第 25 行,在 <module> 中.ser.write(temp)文件C:\Python34\lib\site-packages\serial\serialwin32.py",第283行,写入数据 = to_bytes(data)文件C:\Python34\lib\site-packages\serial\serialutil.py",第 76 行,to_bytesb.append(item) # 这个处理我们模拟的 int 和 str 以及 Python 3.x 的 ints类型错误:需要一个整数谁能帮帮我
在此先感谢您的帮助
我想通了,----------tricky python---------请拉我上来看看我的其他问题(当然有答案)
ok 先单行分配串口不会像
#方法一set_ser = serial.Serial(port="COM1", baudrate=9600, parity = serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS, timeout=1)
这样写
#方法二set_ser = serial.Serial()set_ser.port="COM1"set_ser.baudrate=9600set_ser.parity = serial.PARITY_NONEset_ser.stopbits=serial.STOPBITS_ONEset_ser.bytesize = serial.EIGHTBITSset_ser.timeout=1
然后使用方法2
第二个棘手的部分是这条线
set_ser.write(temp1.encode('utf-8'))
当 Python 分配命令 .write 它必须有 .endcode() 或 .encode('utf-8') 或 ('hex') 或 ('ascii') 等检查此链接https://docs.python.org/2/library/codecs.html#标准编码还有这个http://www.tutorialspoint.com/python/string_encode.htm
现在最终的代码是
def mSend():全局 set_ser,一个set_ser = serial.Serial()set_ser.port="COM1"set_ser.baudrate=9600set_ser.parity = serial.PARITY_NONEset_ser.stopbits=serial.STOPBITS_ONEset_ser.bytesize = serial.EIGHTBITSset_ser.timeout=1打印('打开:' + set_ser.portstr)一 = 0而 a==0:temp = input('写打开\r\r')如果(温度==打开"):set_ser.close()set_ser.open()而 set_ser.isOpen():temp1 = input('输入要发送的内容,按回车:\r\n')set_ser.write(temp1.encode('utf-8'))如果(temp1 ==关闭"):set_ser.close()打印('\n\n关闭')一 = 1elif(温度==关闭"):set_ser.close()一 = 1别的:打印('除非你写打开或关闭你不能移动')
一个按钮可以调用这个函数(这段代码),它会打开串口,发送数据直到用户写关闭"
EDITED
Hello programmers Community,
I have some problems dealing with pyserial in Python 3.4first I do not have serial ports so I have used "Virtual Serial Port Driver 7.2 by Eltima Software" to create virtual serial ports in pairs, which means that I can try to send and receive data from these ports, in my case I just create COM1 connected to COM2, then I installed Hercules SETUP utility by HW group to monitor these serial ports,
so IN THEORY IF I SEND(write) DATA IN PYTHON I CAN SEE IT IN HERCULES AS THIS LINK SHOWS https://www.youtube.com/watch?v=DItyttmpRtY
I was trying to create my own code
def mInitizalise():
set_ser = serial.Serial(port="COM1", baudrate=9600,
parity = serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout=1)
set_ser.close()
set_ser.open()
if set_ser.isOpen():
print ('Open: ' + set_ser.portstr)
temp = input('Type what you want to send, hit enter:\r\n')
set_ser.write(temp)
set_ser.close()
Buy there is an error
>>>
>>> Open: COM1
Type what you want to send, hit enter:
hello
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\me\Downloads\SerialTest.py", line 25, in <module>
ser.write(temp)
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
CAN SOMEBODY HELP ME
thanks beforehand for your help
I figure it out, ----------tricky python--------- please pull me up, and check my other questions (with answers of course)
ok first assigning the serial port in a single line will not work as good as
#Method 1
set_ser = serial.Serial(port="COM1", baudrate=9600, parity = serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS, timeout=1)
writing it in this way
#Method 2
set_ser = serial.Serial()
set_ser.port="COM1"
set_ser.baudrate=9600
set_ser.parity = serial.PARITY_NONE
set_ser.stopbits=serial.STOPBITS_ONE
set_ser.bytesize = serial.EIGHTBITS
set_ser.timeout=1
then USE method 2
second tricky part is this line
set_ser.write(temp1.encode('utf-8'))
when Python assign the command .write IT MUST HAVE .endcode() or .encode('utf-8') or ('hex') or ('ascii') etc check this linkhttps://docs.python.org/2/library/codecs.html#standard-encodingsand this onehttp://www.tutorialspoint.com/python/string_encode.htm
now the final code is
def mSend():
global set_ser, a
set_ser = serial.Serial()
set_ser.port="COM1"
set_ser.baudrate=9600
set_ser.parity = serial.PARITY_NONE
set_ser.stopbits=serial.STOPBITS_ONE
set_ser.bytesize = serial.EIGHTBITS
set_ser.timeout=1
print ('Open: ' + set_ser.portstr)
a = 0
while a==0:
temp = input('write open\r\r')
if (temp == "open"):
set_ser.close()
set_ser.open()
while set_ser.isOpen():
temp1 = input('Type what you want to send, hit enter:\r\n')
set_ser.write(temp1.encode('utf-8'))
if (temp1 == "close"):
set_ser.close()
print ('\n\nClosed')
a = 1
elif (temp == "close"):
set_ser.close()
a = 1
else:
print ('unless you write open or close you can not move')
a button can call this function (this code) and it will open the serial port, send data until the user write "close"
这篇关于如何修复 TypeError:python 3.4、pyserial 2.7 虚拟串口中需要一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!