1、我们将使用XML来保存配置文件,我们将存储串口的设置,python的xml操作库有很多,我们直接使用官方库xml.etree.ElementTree
2、我们在pythonwork新建一个xmlreadandwrite.py,我们希望在程序中实现,启动过程中如果读到设置的xml文件,就自动配置串口,那我们的库需要实现两个功能:
1)传入配置的字典,生成xml保存在本地;
2)传入配置文件的路径,返回配置的串口信息。
由此来设计我们xml读写函数。
Xml的格式,先定义
我们定义的根元素为com,其它为set的内容,结合文档,代码写成如下:
def WriteXml(sl):
root=ET.Element("com")
for e in sl.keys():
l=ET.SubElement(root,"set")
l.attrib={'name':e,'value':sl[e]}
tree = ET.ElementTree(root)
tree.write("setmsg.xml")
我们输入sl的字典,格式与串口设置界面传递来的信号内容一致:
{'com': 'COM1', 'bsp': '115200', 'd': '8', 'p': 'NONE', 's': '1'}
我们来调用下代码:
sl={'com': 'COM1', 'bsp': '115200', 'd': '8', 'p': 'NONE', 's': '1'}
WriteXml(sl)
运行一下,在根目录生成了一个setmsg.xml的文档。
![9d408a568dd3833dabab37792189f287.png](https://i-blog.csdnimg.cn/blog_migrate/6b023744ff74c289b1e87d23b9d2c6d9.jpeg)
生成的xml文件:
![eece7789a3f8290714da75a12eb3c762.png](https://i-blog.csdnimg.cn/blog_migrate/63b16319cc4ae760b98672212af5df22.jpeg)
打开xml文件看下格式:
![69036526cbf77852419d5a1072ea27c3.png](https://i-blog.csdnimg.cn/blog_migrate/bbca1b77b721690888165124b9e9ca3b.jpeg)
与我们的设想一致,接着来读取xml文件,将读取的xml,以字典的形式返回,代码如下:
def ReadXml(spath):
root=ET.parse(spath)
p=root.findall('.')
xmllist={}
for oneper in p:
for child in oneper.getchildren():
xmllist[child.attrib['name']]=child.attrib['value']
return xmllist
输入调用代码:
spath="setmsg.xml"
print(ReadXml(spath))
运行一下:
![6bbfdcf7b7d961ce101be73dcbd73761.png](https://i-blog.csdnimg.cn/blog_migrate/a15141e1ae0aa79de6143a5b95add564.jpeg)
这样我们就实现了xml的读写。
最终代码如下:
![52c3c2dda948ad5a49ed875ccdd3c86d.png](https://i-blog.csdnimg.cn/blog_migrate/057c7f9b0141d0182aec56f675f1ebeb.jpeg)
3、切回uartform.py,在文件头引入:from xmlreadandwrite import WriteXml,ReadXml
在initdata增加:
try:
self.sl=ReadXml('setmsg.xml')
self.callcw(self.sl)
except Exception as e:
self.ShowMsg(str(e))
运行一下代码:
![d230034d316baadf8401540d4cf9b451.png](https://i-blog.csdnimg.cn/blog_migrate/32bd4e8063c774b2ffd69f35abb0e19b.jpeg)
这样我们就实现了程序启动自动设置串口并打开。
接着我们来实现,将配置更新到xml文件中,在callcw增加一个参数默认b=0,我们在callcw增加代码:
if b==0:
WriteXml(msg)
完整代码如下:
![9a565b382f563a5874288010fa38e72c.png](https://i-blog.csdnimg.cn/blog_migrate/6889b0357f15aed7b71682a916f036cc.jpeg)
将最开始的读取XML的代码更改为:self.callcw(self.sl,b=1)
运行一下代码:
![c46bca2c12357bdd19d9786ff0732687.png](https://i-blog.csdnimg.cn/blog_migrate/2e9575ad85f25261ccfb0367606e2cc9.jpeg)
改变一下波特率为9600:
![857b4b206cac59e25d466a63d9bb4e2f.png](https://i-blog.csdnimg.cn/blog_migrate/cf7d5497a319ce0f199adbe9c00a8694.jpeg)
点击确定:
![10a265c94c38e957acaf4e202264ff52.png](https://i-blog.csdnimg.cn/blog_migrate/152d9b957da879532e1ec4d0b6f77c95.jpeg)
查看xml文件:
![2f21812807a1e7cb1d02f20441d66ca3.png](https://i-blog.csdnimg.cn/blog_migrate/ce08da620f1a84a0a012f4e41eb0fbde.jpeg)
已经做了相应的变化,我们在读取配置文件的时候顺便把配置载入串口设置界面中,增加函数:
def set_com(self,msg):
try:
com=msg['com']
bsp=msg['bsp']
d=msg['d']
s=msg['s']
p=msg['p']
self.new.cb_com.setCurrentText(com)
bsp=self.new.cb_bsp.setCurrentText(bsp)
d=self.new.cb_data.setCurrentText(d)
p=self.new.cb_p.setCurrentText(s)
s=self.new.cb_stop.setCurrentText(p)
except Exception as e:
print(e)
在uartform.py的showcw中添加代码如下:
![a7e0daca987a2695f82a34bd86f25f2f.png](https://i-blog.csdnimg.cn/blog_migrate/b8dfb64e7ac4dccb4a613efac2cc3002.jpeg)
运行一下:
![e243817265b475a1f87129eac9964a45.png](https://i-blog.csdnimg.cn/blog_migrate/e03350f6243ad050c8cb29158b9750f0.jpeg)
点开设置串口:
![1e0a1d1a91078cce8e59e56319b3870a.png](https://i-blog.csdnimg.cn/blog_migrate/9cd8da6b501a4be89b090c851ddf5775.jpeg)
好的,我们本节课完成保存设置与读取设置,下节课将完成log读写的功能。