首页 > 解决方案 > 莳萝是否存储串口属性?

问题描述

我正在设置与 Arduino 的通信,该 Arduino 又与几个数模控制器进行通信(有关更多详细信息,请访问 www.opendacs.com)。我在 python 包装器中执行此操作,以便能够更好地将其与我团队的其他代码库集成。

对于我的特定应用程序,即使当前 ipython 会话在没有关闭串口的情况下突然关闭,串口也需要可访问(当前使用 pyserial)。

我以为我会使用 Dill 序列化端口对象,然后重新访问它,但它似乎没有保留某些属性。

import pickle
import dill
import time
import serial

global serial_port

serial_port = serial.Serial(
    'COM4',
    baudrate=115200,
    timeout = 0.5
)
global ser
ser = serial_port
print("Serial before storage: ")
print(ser)
#store serial port 
port_file = open('port_file.txt','w')
dill.dump(ser,port_file)
port_file.close()
ser = None
port_file = open("port_file.txt",'r')
ser = dill.load(port_file)
port_file.close()
print("Serial after storage: ")
print(ser)

我希望这两个打印消息是相同的,但是第二个会引发此错误:

Serial before storage: 
Serial<id=0x99f40f0, open=True>(port='COM4', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0.5, xonxoff=False, rtscts=False, dsrdtr=False)
Serial after storage: 


Traceback (most recent call last):

  File "<ipython-input-33-a9117ca1b8e3>", line 1, in <module>
    runfile('C:/Users/rkauf/Google Drive/Grad School/Projects/4 - Current Sources/Pickle_Testing.py', wdir='C:/Users/rkauf/Google Drive/Grad School/Projects/4 - Current Sources')

  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 95, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/rkauf/Google Drive/Grad School/Projects/4 - Current Sources/Pickle_Testing.py", line 60, in <module>
    print(ser)

  File "C:\ProgramData\Anaconda2\lib\site-packages\serial\serialutil.py", line 529, in __repr__
    name=self.__class__.__name__, id=id(self), p=self)

AttributeError: 'Serial' object has no attribute 'is_open'

标签: pythonpyserialdill

解决方案


推荐阅读