-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNAVFoundation.ComSpec.axi
More file actions
304 lines (271 loc) · 9.66 KB
/
NAVFoundation.ComSpec.axi
File metadata and controls
304 lines (271 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
PROGRAM_NAME='NAVFoundation.ComSpec'
/*
_ _ _ ___ __
| \ | | ___ _ __ __ _ __ _| |_ ___ / \ \ / /
| \| |/ _ \| '__/ _` |/ _` | __/ _ \ / _ \ \ / /
| |\ | (_) | | | (_| | (_| | || __// ___ \ V /
|_| \_|\___/|_| \__, |\__,_|\__\___/_/ \_\_/
|___/
MIT License
Copyright (c) 2010-2026 Norgate AV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#IF_NOT_DEFINED __NAV_FOUNDATION_COMSPEC__
#DEFINE __NAV_FOUNDATION_COMSPEC__ 'NAVFoundation.ComSpec'
#include 'NAVFoundation.ComSpec.h.axi'
#include 'NAVFoundation.ErrorLogUtils.axi'
/**
* @function NAVComSpecInit
* @public
* @description Initializes a _NAVComSpec structure with common default values.
* Default settings: 9600 baud, 8 data bits, 1 stop bit, no parity, RS-232 mode,
* no character delay, hardware/software handshaking disabled, 9-bit mode disabled.
*
* @param {_NAVComSpec} spec - Reference to the _NAVComSpec structure to initialize
*
* @returns {void}
*
* @example
* stack_var _NAVComSpec comSpec
* NAVComSpecInit(comSpec)
* comSpec.Baud = 115200
* NAVComSpecApply(dvDevice, comSpec)
*/
define_function NAVComSpecInit(_NAVComSpec spec) {
spec.Baud = 9600
spec.DataBits = 8
spec.StopBits = 1
spec.Parity = 'N'
spec.Rs485 = false
spec.Rs422 = false
spec.CharDelay = 0
spec.CharDelayMs = 0
spec.HardFlowControl = false
spec.SoftFlowControl = false
spec.B9Mode = false
}
/**
* @function NAVComSpecApply
* @public
* @description Applies serial port communication settings to a device based on a _NAVComSpec structure.
* Sends appropriate commands to configure baud rate, data bits, parity, stop bits, RS-422/485 mode,
* character delay, hardware/software handshaking, and 9-bit mode.
*
* @param {dev} device - Target device to configure
* @param {_NAVComSpec} spec - Structure containing the serial port settings to apply
*
* @returns {char} true if settings were applied successfully, false if validation failed
*
* @example
* stack_var _NAVComSpec comSpec
* NAVComSpecInit(comSpec)
* comSpec.Baud = 115200
* comSpec.HardFlowControl = true
* if (NAVComSpecApply(dvSerialPort, comSpec)) {
* // Settings applied successfully
* }
*/
define_function char NAVComSpecApply(dev device, _NAVComSpec spec) {
stack_var char mode[15]
// Can't apply serial setting to a socket
if (device.number == 0) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
"'Cannot apply serial settings to socket device: ', NAVDeviceToString(device)")
return false
}
// Ensure device is online before attempting to send commands
if (!NAVDeviceIsOnline(device)) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
"'Attempt to apply serial settings to offline device: ', NAVDeviceToString(device)")
return false
}
// Validate baud rate
switch (spec.Baud) {
case 150:
case 300:
case 600:
case 1200:
case 2400:
case 4800:
case 9600:
case 19200:
case 38400:
case 57600:
case 76800:
case 115200: {
break
// Valid baud rates
}
default: {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
"'Invalid baud rate: ', itoa(spec.Baud)")
return false
}
}
// Validate 9-bit mode constraints
// When B9Mode is enabled, force to the ONLY valid combination: N,9,1
if (spec.B9Mode) {
spec.Parity = 'N'
spec.DataBits = 9
spec.StopBits = 1
}
// Validate parity
switch (spec.Parity) {
case 'N': // None
case 'E': // Even
case 'O': // Odd
case 'M': // Mark
case 'S': { // Space
break
// Valid parity options
}
default: {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
"'Invalid parity: ', spec.Parity")
return false
}
}
// Validate data bits (8 is standard, 9 only valid with B9Mode)
switch (spec.Databits) {
case 8: {
break
// Valid data bits
}
case 9: {
if (!spec.B9Mode) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
'9 data bits requires B9Mode to be enabled')
return false
}
break
// Only valid if B9Mode enabled
}
default: {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
"'Invalid data bits: ', itoa(spec.DataBits)")
return false
}
}
// Validate stop bits
switch (spec.StopBits) {
case 1:
case 2: {
break
// Valid stop bits
}
default: {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
"'Invalid stop bits: ', itoa(spec.StopBits)")
return false
}
}
// Validate RS485 and RS422 are NOT both set
if (spec.Rs485 && spec.Rs422) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
'Both Rs485 and Rs422 cannot be enabled simultaneously')
return false
}
// Validate character delay settings - cannot use both CHARD and CHARDM simultaneously
if (spec.CharDelay > 0 && spec.CharDelayMs > 0) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_COMSPEC__,
'NAVComSpecApply',
'Cannot set both CharDelay and CharDelayMs')
return false
}
// Set port mode
// This will be ignored for regular RS232 ports,
// but is required for IR ports when using one-way RS232 mode
NAVCommand(device, 'SET MODE DATA')
// Ensure RX mode is on
NAVCommand(device, 'RXON')
// Configure 9-bit mode BEFORE setting baud parameters
// B9MON overrides the port to 9-bit mode
if (spec.B9Mode) {
NAVCommand(device, 'B9MON')
}
else {
NAVCommand(device, 'B9MOFF')
}
// Build RS-422/485 mode string
select {
active (spec.Rs485): {
mode = '485 ENABLE'
}
active (spec.Rs422): {
mode = '422 ENABLE'
}
active (true): {
// Neither 422 nor 485 enabled = RS-232 mode
mode = '422/485 DISABLE'
}
}
// Send SET BAUD command
NAVCommand(device, "
'SET BAUD ', itoa(spec.Baud), ',',
spec.Parity, ',',
itoa(spec.DataBits), ',',
itoa(spec.StopBits), ' ',
mode")
// Configure character delay
// If both are zero, send both commands to ensure zero delay
// Otherwise, send only the non-zero command
select {
active (spec.CharDelay == 0 && spec.CharDelayMs == 0): {
NAVCommand(device, 'CHARD-0')
NAVCommand(device, 'CHARDM-0')
}
active (spec.CharDelayMs > 0): {
NAVCommand(device, "'CHARDM-', itoa(spec.CharDelayMs)")
}
active (spec.CharDelay > 0): {
NAVCommand(device, "'CHARD-', itoa(spec.CharDelay)")
}
}
// Configure hardware handshaking
if (spec.HardFlowControl) {
NAVCommand(device, 'HSON')
}
else {
NAVCommand(device, 'HSOFF')
}
// Configure software handshaking
if (spec.SoftFlowControl) {
NAVCommand(device, 'XON')
}
else {
NAVCommand(device, 'XOFF')
}
return true
}
#END_IF // __NAV_FOUNDATION_COMSPEC__