Python pyserial对象只在全局运行?

时间:2016-07-29 10:56:43

标签: python arduino pyserial

我正在尝试使用串行通信从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'))

任何人都知道可能导致问题的原因是什么?

0 个答案:

没有答案