通过Python从串行端口读取Arduino传感器数据不起作用

时间:2019-05-10 21:27:53

标签: python arduino pyserial

尝试使用python读取来自arduino板的串行输出。无法弄清楚如何与“ pyserial”连接。按照我的代码和我得到的错误消息:

import serial
import time

i=0
ser = serial.Serial('COM7', baudrate = 9600, timeout= 2)

def getValues():
    arduinoData = ser.readline().decode('ascii')
    return(arduinoData)

while i<1000:
    print(getValues())
    time.sleep(5)
    i+=1


---------------------------------------------------------------------------
SerialException                           Traceback (most recent call last)
<ipython-input-27-d34b4056a66b> in <module>
      3 
      4 i=0
----> 5 ser = serial.Serial('COM7', baudrate = 9600, timeout= 2)
      6 
      7 def getValues():

c:\users\hp\appdata\local\programs\python\python37-32\lib\site-packages\serial\serialwin32.py in __init__(self, *args, **kwargs)
     29         self._overlapped_read = None
     30         self._overlapped_write = None
---> 31         super(Serial, self).__init__(*args, **kwargs)
     32 
     33     def open(self):

c:\users\hp\appdata\local\programs\python\python37-32\lib\site-packages\serial\serialutil.py in __init__(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)
    238 
    239         if port is not None:
--> 240             self.open()
    241 
    242     #  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

c:\users\hp\appdata\local\programs\python\python37-32\lib\site-packages\serial\serialwin32.py in open(self)
     60         if self._port_handle == win32.INVALID_HANDLE_VALUE:
     61             self._port_handle = None    # 'cause __del__ is called anyway
---> 62             raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
     63 
     64         try:

SerialException: could not open port 'COM7': PermissionError(13, 'Access is denied.', None, 5)

我的问题是我在做什么错,我该如何解决?

0 个答案:

没有答案