Skip to content

Commit 9be5279

Browse files
committed
Rewrite: v2.0
1 parent ff5ba51 commit 9be5279

33 files changed

+11639
-4291
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#other
22
build/
3+
log/
34
Serial Tool LOG/
5+
.vscode/.*
6+
tests/
7+
.pytest*
48

59
# Byte-compiled / optimized / DLL files
610
__pycache__/

.vscode/settings.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
{
2-
"python.pythonPath": "C:\\Python3.7_64x\\python.exe"
2+
"python.pythonPath": "C:\\Python3.7_64x\\python.exe",
3+
"python.testing.pytestArgs": [
4+
"src/tests",
5+
],
6+
"python.testing.unittestEnabled": true,
7+
"python.testing.nosetestsEnabled": false,
8+
"python.testing.pytestEnabled": false,
9+
"python.testing.unittestArgs": [
10+
"-v",
11+
"-s",
12+
"tests",
13+
"-p",
14+
"test_*.py"
15+
]
316
}

.vscode/tasks.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"type": "process",
99
"command": "C:/Python3.7_64x/Lib/site-packages/pyqt5_tools/Qt/bin/designer.exe",
1010
"args": [
11-
"${workspaceFolder}/src/gui.ui"
11+
"${workspaceFolder}/src/gui/ui/gui.ui",
12+
"${workspaceFolder}/src/gui/ui/serialSetupDialog.ui"
1213
],
1314
"problemMatcher": []
1415
},

example_configuration.txt

-37
This file was deleted.

serialTool.code-workspace

+8
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@
55
}
66
],
77
"settings": {
8+
"files.exclude": {
9+
"**/.git": true,
10+
"**/.svn": true,
11+
"**/.hg": true,
12+
"**/CVS": true,
13+
"**/.DS_Store": true,
14+
"**/__pycache__": true
15+
}
816
}
917
}

src/cfgHandler.py

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""
2+
This file holds all configuration file save/load functions and handlers.
3+
"""
4+
import json
5+
import sys
6+
import time
7+
import threading
8+
9+
from defines import *
10+
import dataModel
11+
import serComm
12+
13+
__version__ = 2.0 # configuration file version (not main software version)
14+
15+
16+
class ConfigurationHandler:
17+
def __init__(self, data: dataModel.SerialToolSettings):
18+
"""
19+
This class initialize thread that constantly poll RX buffer and store receive data in a list.
20+
On after data readout, sigRxNotEmpty signal is emitted to notify parent that new data is available.
21+
"""
22+
self.dataModel: dataModel.SerialToolSettings = data
23+
24+
def saveConfiguration(self, filePath: str):
25+
"""
26+
Overwrite data with current settings in a json format.
27+
@param filePath: path where file should be created.
28+
"""
29+
wData = {}
30+
wData[CFG_TAG_FILE_VERSION] = __version__
31+
wData[CFG_TAG_SERIAL_CFG] = {}
32+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_PORT] = self.dataModel.serialSettings.port
33+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_BAUDRATE] = self.dataModel.serialSettings.baudrate
34+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_DATASIZE] = self.dataModel.serialSettings.dataSize
35+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_STOPBITS] = self.dataModel.serialSettings.stopbits
36+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_PARITY] = self.dataModel.serialSettings.parity
37+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_SWFLOWCONTROL] = self.dataModel.serialSettings.swFlowControl
38+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_HWFLOWCONTROL] = self.dataModel.serialSettings.hwFlowControl
39+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_READTIMEOUTMS] = self.dataModel.serialSettings.readTimeoutMs
40+
wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_WRITETIMEOUTMS] = self.dataModel.serialSettings.writeTimeoutMs
41+
wData[CFG_TAG_DATA_FIELDS] = {}
42+
for channelIndex, data in enumerate(self.dataModel.dataFields):
43+
wData[CFG_TAG_DATA_FIELDS][channelIndex] = data
44+
wData[CFG_TAG_NOTE_FIELDS] = {}
45+
for channelIndex, note in enumerate(self.dataModel.noteFields):
46+
wData[CFG_TAG_NOTE_FIELDS][channelIndex] = note
47+
wData[CFG_TAG_SEQ_FIELDS] = {}
48+
for channelIndex, sequence in enumerate(self.dataModel.seqFields):
49+
wData[CFG_TAG_SEQ_FIELDS][channelIndex] = sequence
50+
wData[CFG_TAG_RXLOG] = self.dataModel.displayReceivedData
51+
wData[CFG_TAG_TXLOG] = self.dataModel.displayTransmittedData
52+
wData[CFG_TAG_OUTPUT_REPRESENTATION] = self.dataModel.outputDataRepresentation
53+
wData[CFG_TAG_VERBOSE_DISPLAY] = self.dataModel.verboseDisplayMode
54+
55+
with open(filePath, 'w+') as fileHandler:
56+
json.dump(wData, fileHandler, indent=4)
57+
58+
def loadConfiguration(self, filePath: str):
59+
"""
60+
Read (load) given json file and set new configuration.
61+
@param filePath: path to load file from.
62+
"""
63+
with open(filePath, 'r') as fileHandler:
64+
wData = json.load(fileHandler)
65+
66+
if (CFG_TAG_FILE_VERSION not in wData) or (wData[CFG_TAG_FILE_VERSION] != __version__):
67+
errorMsg = f"Configuration file syntax has changed - unable to set configuration."
68+
errorMsg += f"\nCurrent version: {__version__}, config file version: {wData[CFG_TAG_FILE_VERSION]}"
69+
raise Exception(errorMsg)
70+
71+
try:
72+
# serial settings
73+
serialSettings = serComm.SerialCommSettings()
74+
serialSettings.port = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_PORT]
75+
serialSettings.baudrate = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_BAUDRATE]
76+
serialSettings.dataSize = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_DATASIZE]
77+
serialSettings.stopbits = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_STOPBITS]
78+
serialSettings.parity = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_PARITY]
79+
serialSettings.swFlowControl = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_SWFLOWCONTROL]
80+
serialSettings.hwFlowControl = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_HWFLOWCONTROL]
81+
serialSettings.readTimeoutMs = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_READTIMEOUTMS]
82+
serialSettings.writeTimeoutMs = wData[CFG_TAG_SERIAL_CFG][CFG_TAG_SERIAL_CFG_WRITETIMEOUTMS]
83+
self.dataModel.setSerialSettings(serialSettings)
84+
85+
for channel, data in wData[CFG_TAG_DATA_FIELDS].items():
86+
self.dataModel.setDataField(int(channel), data)
87+
88+
for channel, data in wData[CFG_TAG_NOTE_FIELDS].items():
89+
self.dataModel.setNoteField(int(channel), data)
90+
91+
for channel, data in wData[CFG_TAG_SEQ_FIELDS].items():
92+
self.dataModel.setSeqField(int(channel), data)
93+
94+
self.dataModel.setRxDisplayMode(wData[CFG_TAG_RXLOG])
95+
self.dataModel.setTxDisplayMode(wData[CFG_TAG_TXLOG])
96+
self.dataModel.setOutputRepresentationMode(wData[CFG_TAG_OUTPUT_REPRESENTATION])
97+
self.dataModel.setVerboseDisplayMode(wData[CFG_TAG_VERBOSE_DISPLAY])
98+
99+
except Exception as err:
100+
errorMsg = f"Unable to load configuration from a file: {filePath}"
101+
errorMsg += f"\nError:\n{err}"
102+
errorMsg += f"\nFile content:\n{wData}"
103+
raise Exception(errorMsg)
104+
105+
def createDefaultConfiguration(self):
106+
"""
107+
Set instance of data model with default values.
108+
Will emit signals to update GUI.
109+
"""
110+
self.dataModel.setSerialSettings(serComm.SerialCommSettings())
111+
for channel in range(NUM_OF_DATA_CHANNELS):
112+
self.dataModel.setDataField(channel, '')
113+
self.dataModel.setNoteField(channel, '')
114+
115+
for channel in range(NUM_OF_SEQ_CHANNELS):
116+
self.dataModel.setSeqField(channel, '')
117+
118+
self.dataModel.setRxDisplayMode(True)
119+
self.dataModel.setTxDisplayMode(True)
120+
self.dataModel.setOutputRepresentationMode(OutputRepresentation.STRING)
121+
self.dataModel.setVerboseDisplayMode(True)
122+
123+
124+
if __name__ == "__main__":
125+
# test
126+
filePath = r"D:\Google Drive\Strom\SerialTool\log\exampleConfiguration.json"
127+
dataModel = dataModel.SerialToolSettings()
128+
129+
cfgHandler = ConfigurationFilesHandler(dataModel)
130+
cfgHandler.saveConfiguration(filePath)
131+
cfgHandler.loadConfiguration(filePath)

0 commit comments

Comments
 (0)