我正在建立与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'