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