python - 莳萝是否存储串口属性?
问题描述
我正在设置与 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'
解决方案
推荐阅读
- php - 如何使用给定的 json 发出 PHP POST 请求
- python-3.x - 如何使用 YAML、tavern 和 pytest 使用基本身份验证测试 API
- android - 通过android中的adb shell命令行将json字符串发送到模拟器|设备
- javascript - Microsoft Edge - 无法为 KeyboardEvent 设置 keyCode(合成)
- c# - 选择所有类型的雇员,然后选择特定的雇员
- c# - 未找到与 RestoreAssemblyResources AssemblyName 元数据“Xamarin.Firebase.Messaging”匹配的参考
- c# - WPF 应用程序 - 无法更改 MouseDown 上的矩形属性
- css - 将 SAPUI5 按钮图标放在文本上(不是左侧)
- r - 我在梯度提升机(GBM)模型上设置种子,但我不断得到不同的预测
- python - 表中的 Django 选择选项不起作用