问题
I am setting up communication to an Arduino that is in turn communicating with a couple of digital-to-analog controllers (www.opendacs.com for more details). I am doing this in a python wrapper in order to be able to better integrate it with the rest of my team's codebase.
For my specific application, the serial port needs to be accessible (currently using pyserial) even if the current ipython session is closed out suddenly without shutting down the serial port.
I thought I would use Dill to serialize the port object and then just reaccess it, but it seems that it doesn't preserve some of the attributes.
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)
I would expect the two print messages to just be the same, however the second one throws this error:
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'
来源:https://stackoverflow.com/questions/56451610/does-dill-store-serial-port-attributes