74 lines
1.6 KiB
Python
74 lines
1.6 KiB
Python
|
|
#!/opt/homebrew/bin/python3
|
||
|
|
# -*- coding:utf-8 -*-
|
||
|
|
|
||
|
|
import sys
|
||
|
|
import time
|
||
|
|
|
||
|
|
from PyQt6 import *
|
||
|
|
from PyQt6.QtCore import *
|
||
|
|
|
||
|
|
from config import config
|
||
|
|
from logs import log
|
||
|
|
|
||
|
|
from interfaceSession.interfaceManager import interfaceManager
|
||
|
|
|
||
|
|
log.setConfig(config.toDict()['log'])
|
||
|
|
|
||
|
|
def main():
|
||
|
|
|
||
|
|
print(dir(interfaceManager))
|
||
|
|
|
||
|
|
log.info("supported interface type:", interfaceManager.getSupportedType())
|
||
|
|
|
||
|
|
interfaceList = interfaceManager.getInfo('all')
|
||
|
|
|
||
|
|
log.info("all port info:", interfaceList)
|
||
|
|
|
||
|
|
id = interfaceManager.create("COM1_test", "serial", {
|
||
|
|
'port':'COM1',
|
||
|
|
'baudrate':'115200'
|
||
|
|
})
|
||
|
|
if not id:
|
||
|
|
log.error("failed create interface")
|
||
|
|
return
|
||
|
|
|
||
|
|
|
||
|
|
log.info("this port info:", interfaceManager.getInfo(id)[0])
|
||
|
|
|
||
|
|
if not interfaceManager.open(id):
|
||
|
|
log.error("failed to open session")
|
||
|
|
return
|
||
|
|
|
||
|
|
interfaceManager.update(id, 'COM1_' + time.strftime('%H:%M:%S', time.localtime()))
|
||
|
|
|
||
|
|
interfaceManager.declearUsing(id) #声明使用此接口
|
||
|
|
|
||
|
|
# interfaceManager.update(id, 'COM1_new') # 使用中的接口无法更新
|
||
|
|
|
||
|
|
session = interfaceManager.getSession(id)
|
||
|
|
|
||
|
|
if not session:
|
||
|
|
log.error("no session")
|
||
|
|
return
|
||
|
|
|
||
|
|
log.info("send cmd...")
|
||
|
|
|
||
|
|
session.lock() #锁定此接口
|
||
|
|
session.send(bytearray(b'aa bb cc'))
|
||
|
|
session.unlock() #解锁此接口
|
||
|
|
|
||
|
|
interfaceManager.declearUnusing(id) #声明不再使用此接口
|
||
|
|
|
||
|
|
log.info("close port")
|
||
|
|
if interfaceManager.close(id):
|
||
|
|
log.info("close the port OK")
|
||
|
|
else:
|
||
|
|
log.error("failed to close the port")
|
||
|
|
|
||
|
|
interfaceManager.delete(id) #删除此接口
|
||
|
|
|
||
|
|
log.info("start...")
|
||
|
|
main()
|
||
|
|
|
||
|
|
|