QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SerialSettings.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7
8ColumnLayout {
9 property var linkManager: QGroundControl.linkManager
10
11 spacing: _rowSpacing
12
13 function saveSettings() {
14 if (baudCombo.isCustomBaud) {
15 var baud = parseInt(customBaudField.text)
16 if (baud > 0) {
17 subEditConfig.baud = baud
18 }
19 }
20 }
21
22 GridLayout {
23 columns: 2
24 rowSpacing: _rowSpacing
25 columnSpacing: _colSpacing
26
27 QGCLabel { text: qsTr("Serial Port") }
28 QGCComboBox {
29 id: commPortCombo
30 Layout.preferredWidth: _secondColumnWidth
31 objectName: "serialPortCombo"
32 enabled: linkManager.serialPorts.length > 0
33 textRole: "label"
34 valueRole: "path"
35
36 function refreshPorts() {
37 const paths = linkManager.serialPorts
38 const labels = linkManager.serialPortStrings
39 const entries = []
40 let selectedPath = subEditConfig.portName
41 if (!selectedPath && paths.length > 0) {
42 selectedPath = paths[0]
43 subEditConfig.portName = selectedPath
44 }
45 for (let i = 0; i < paths.length; ++i) {
46 entries.push({ label: labels[i] || paths[i], path: paths[i] })
47 }
48 let selectedIndex = paths.indexOf(selectedPath)
49 if (selectedIndex < 0 && selectedPath) {
50 entries.push({ label: selectedPath, path: selectedPath })
51 selectedIndex = entries.length - 1
52 }
53 if (entries.length === 0) {
54 entries.push({ label: qsTr("None Available"), path: "" })
55 selectedIndex = 0
56 }
57 model = entries
58 currentIndex = selectedIndex
59 }
60
61 onActivated: (index) => {
62 if (index >= 0 && index < model.length && model[index].path) {
63 subEditConfig.portName = model[index].path
64 }
65 }
66
67 Component.onCompleted: refreshPorts()
68
69 Connections {
70 target: linkManager
71 function onCommPortsChanged() { commPortCombo.refreshPorts() }
72 function onCommPortStringsChanged() { commPortCombo.refreshPorts() }
73 }
74 Connections {
75 target: subEditConfig
76 function onPortNameChanged() { commPortCombo.refreshPorts() }
77 }
78 }
79
80 QGCLabel { text: qsTr("Baud Rate") }
81 QGCComboBox {
82 id: baudCombo
83 Layout.preferredWidth: _secondColumnWidth
84
85 readonly property string _customLabel: qsTr("Custom")
86 readonly property bool isCustomBaud: currentText === _customLabel
87
88 onActivated: (index) => {
89 if (index !== -1 && !isCustomBaud) {
90 subEditConfig.baud = parseInt(currentText)
91 }
92 }
93
94 Component.onCompleted: {
95 var rates = linkManager.serialBaudRates.slice()
96 rates.push(_customLabel)
97 model = rates
98
99 var baud = subEditConfig ? subEditConfig.baud.toString() : "57600"
100 var index = baudCombo.find(baud)
101 if (index === -1) {
102 baudCombo.currentIndex = baudCombo.count - 1
103 customBaudField.text = baud
104 } else {
105 baudCombo.currentIndex = index
106 }
107 }
108 }
109
110 QGCLabel {
111 text: qsTr("Custom Baud Rate")
112 visible: baudCombo.isCustomBaud
113 }
114 QGCTextField {
115 id: customBaudField
116 Layout.preferredWidth: _secondColumnWidth
117 visible: baudCombo.isCustomBaud
118 numericValuesOnly: true
119 validator: IntValidator { bottom: 1 }
120 onEditingFinished: {
121 if (!baudCombo.isCustomBaud) return
122 var baud = parseInt(text)
123 if (baud > 0) {
124 subEditConfig.baud = baud
125 }
126 }
127 }
128 }
129
130 QGCCheckBox {
131 id: advancedSettings
132 text: qsTr("Advanced Settings")
133 checked: false
134 }
135
136 GridLayout {
137 columns: 2
138 rowSpacing: _rowSpacing
139 columnSpacing: _colSpacing
140 visible: advancedSettings.checked
141
142 QGCCheckBox {
143 Layout.columnSpan: 2
144 text: qsTr("Enable Flow Control")
145 checked: subEditConfig.flowControl !== 0
146 onCheckedChanged: subEditConfig.flowControl = checked ? 1 : 0
147 }
148
149 QGCCheckBox {
150 Layout.columnSpan: 2
151 text: qsTr("Force DTR Low")
152 checked: subEditConfig ? subEditConfig.dtrForceLow : false
153 onCheckedChanged: { if (subEditConfig) subEditConfig.dtrForceLow = checked }
154 }
155
156 QGCLabel { text: qsTr("Parity") }
157 QGCComboBox {
158 Layout.preferredWidth: _secondColumnWidth
159 model: [qsTr("None"), qsTr("Even"), qsTr("Odd")]
160
161 onActivated: (index) => {
162 // Hard coded values from qserialport.h
163 switch (index) {
164 case 0:
165 subEditConfig.parity = 0
166 break
167 case 1:
168 subEditConfig.parity = 2
169 break
170 case 2:
171 subEditConfig.parity = 3
172 break
173 }
174 }
175
176 Component.onCompleted: {
177 switch (subEditConfig.parity) {
178 case 0:
179 currentIndex = 0
180 break
181 case 2:
182 currentIndex = 1
183 break
184 case 3:
185 currentIndex = 2
186 break
187 default:
188 console.warn("Unknown parity", subEditConfig.parity)
189 break
190 }
191 }
192 }
193
194 QGCLabel { text: qsTr("Data Bits") }
195 QGCComboBox {
196 Layout.preferredWidth: _secondColumnWidth
197 model: [ "5", "6", "7", "8" ]
198 currentIndex: Math.max(Math.min(subEditConfig.dataBits - 5, 3), 0)
199 onActivated: (index) => { subEditConfig.dataBits = index + 5 }
200 }
201
202 QGCLabel { text: qsTr("Stop Bits") }
203 QGCComboBox {
204 Layout.preferredWidth: _secondColumnWidth
205 model: [ "1", "2" ]
206 currentIndex: Math.max(Math.min(subEditConfig.stopBits - 1, 1), 0)
207 onActivated: (index) => { subEditConfig.stopBits = index + 1 }
208 }
209 }
210}