python - 如何修复TypeError : an integer is required in python 3. 4、pyserial 2.7虚拟串口

标签 python python-2.7 serial-port usb pyserial

已编辑

你好程序员社区,
我在处理 Python 3.4 中的 pyserial 时遇到了一些问题 首先我没有串行端口,所以我使用“Eltima Software 的虚拟串行端口驱动程序 7.2”成对创建虚拟串行端口,这意味着我可以尝试从这些端口发送和接收数据,在我的例子中我只是创建COM1 连接到 COM2,然后我安装了 HW 组的 Hercules SETUP 实用程序来监视这些串口,

所以理论上,如果我在 PYTHON 中发送(写入)数据,我可以在 HERCULES 中看到它,因为此链接显示 https://www.youtube.com/watch?v=DItyttmpRtY

我正在尝试创建自己的代码

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()

购买有误

>>> 
>>> 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

谁能帮帮我

预先感谢您的帮助

最佳答案

我想出来了,------------tricky python--------请把我拉起来,检查我的其他问题(当然有答案)

ok 先单线分配串口效果不如

#Method 1
set_ser = serial.Serial(port="COM1", baudrate=9600, parity = serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS, timeout=1)

这样写

#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

然后使用方法2

第二个棘手的部分是这一行

set_ser.write(temp1.encode('utf-8'))

当 Python 分配命令 .write 时,它​​必须有 .endcode() 或 .encode('utf-8') 或 ('hex') 或 ('ascii') 等检查此链接 https://docs.python.org/2/library/codecs.html#standard-encodings 和这个 http://www.tutorialspoint.com/python/string_encode.htm

现在最终的代码是

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')

一个按钮可以调用这个函数(这段代码),它会打开串口,发送数据,直到用户写“关闭”

关于python - 如何修复TypeError : an integer is required in python 3. 4、pyserial 2.7虚拟串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27895113/

相关文章:

python - 在 Python 中使用 selenium 从 <li> 项中提取文本

python - 重复二维数组的行

python - 如何使用 BeautifulSoup 在网页中找到具有特定类的元素?

python - 在字典列表中合并相同键的值

python-2.7 - 动态上下文无关语法NLTK

python - Python 中重写 __get__ 的问题

python - 比较两个 CSV 文件之间的值并写入第三个 CSV 文件

Linux Shell,如何通过串行端口发送命令并返回?

c++ - Linux 新手 : C++ opening and closing usb port issues

c# - 串口工作7-8小时卡住或停止发送串口通讯