AttributeError: 'function' object has no attribute 'Serial' error when used in a threaded function

AttributeError: 'function' object has no attribute 'Serial' error when used in a threaded function

我正在尝试线程化一个打开串行 com 并发送消息然后等待重播的函数。在线程函数之外,串行工作但在其中出现此错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "MFC_Calv3_T.py", line 37, in serial
    ser = serial.Serial('/dev/ttyACM0', baudrate = 9600) # setup com to serial
AttributeError: 'function' object has no attribute 'Serial'

函数如下:

def serial():
 ser = serial.Serial('/dev/ttyACM0', baudrate = 9600) # setup com to serial
 ser.open()
 ser.write(str.encode('$GET DQ DC\r'))
 startTime = time.time()
 data = ser.readline()
 endTime = time.time()
 ser.close
 parts = data.split(",")
 Writer(startTime, endTime, parts)

我的导入是:

import serial 
from serial import Serial
import threading
from threading import Thread

我错过了什么?

模块

的名称相同 serial
import serial

和函数

def serial():

所以 function 替换了 module,这就产生了问题。

将函数重命名为 ie。 def get_serial() 它应该可以解决问题。