【问题标题】:Reading data in python from a COM port从 COM 端口读取 python 中的数据
【发布时间】:2015-11-13 12:14:33
【问题描述】:
import serial
while True:
    ser=serial.Serial(port='COM30',baudrate=9600)
    print "try"
    s=ser.read(100) #reading up to 100 bytes
    print s
ser.close()

设备管理器:


我正在尝试使用 python 从端口读取数据。 但它显示错误:

Traceback (most recent call last):
  File "new_python.py", line 3, in <module>
    ser=serial.Serial(port='COM30',baudrate=9600)
  File "C:\Anaconda\lib\site-packages\serial\serialwin32.py", line 38, in __init__
    SerialBase.__init__(self, *args, **kwargs)
  File "C:\Anaconda\lib\site-packages\serial\serialutil.py", line 282, in __init__
    self.open()
  File "C:\Anaconda\lib\site-packages\serial\serialwin32.py", line 66, in open
    raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM30': WindowsError(5, 'Access is denied.')

***Repl Closed***

现在从堆栈上的先前解决方案中,我尝试使用 python 32 位并从具有管理员权限但同样错误的 cmd 调用它!

当我尝试使用 matlab 时,它会显示:

s = serial('COM30')

   Serial Port Object : Serial-COM30

   Communication Settings 
      Port:               COM30
      BaudRate:           9600
      Terminator:         'LF'

   Communication State 
      Status:             closed
      RecordStatus:       off

   Read/Write State  
      TransferStatus:     idle
      BytesAvailable:     0
      ValuesReceived:     0
      ValuesSent:         0

【问题讨论】:

  • 感谢编辑!

标签: python python-2.7 port tcomport


【解决方案1】:
import serial
import time
ser=serial.Serial(port='COM30',baudrate=9600)
ser.open()
while True:
    print "try"
    time.sleep(10)
    s=ser.read(100) #reading up to 100 bytes
    print s
ser.close()

你可以试试这个吗?

【讨论】:

【解决方案2】:

看来你还没有打开串口

ser=serial.Serial(port='COM30',baudrate=9600)
ser.open()

还有。如果去掉循环会发生什么?

【讨论】:

  • 根据this answer,这不是必需的。我想知道是否存在一个问题,即每个循环在上一个循环之后还没有关闭时会尝试打开端口。
  • @elParaguayo 好吧,我不知道。
  • @AbhishekBhatia 串行连接可能很麻烦。在我看来,您尝试多次打开端口,您可能不得不终止与 com-port 通信的进程。我想重启windows最简单。您也可以尝试 'ser.flushInput()' 和 'ser.flushOutput()'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多