Python pyserial对象只在全局时工作?

2024-09-28 21:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用串行通信的python脚本与arduino通信。当我声明全局创建串行对象时,串行通信工作正常,如下所示:

ser = serial.Serial('COM4', 115200, timeout=0) #object to interface with serial, comnumber, baud rate, timeout

def configureArduino():

  try:

    #there are a few lines here to calculate TsMicro I have ommitted for ease of reading

    ser.write(TsMicro.encode('ascii'))
    ser.write('\n'.encode('ascii'))

但当我在我的函数中这样做的时候:

def configureArduino():

  ser = serial.Serial('COM4', 115200, timeout=0) #object to interface with serial, comnumber, baud rate, timeout

  try:

    #there are a few lines here to calculate TsMicro I have ommitted for ease of reading

    ser.write(TsMicro.encode('ascii'))
    ser.write('\n'.encode('ascii'))

有人知道是什么导致了这个问题吗?你知道吗


Tags: toobjectwithasciitimeoutserialinterfaceser