forked from JuliaIO/LibSerialPort.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
high-level-api.jl
414 lines (330 loc) · 11.7 KB
/
high-level-api.jl
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
type SerialPort <: IO
ref::Port
eof::Bool
end
"""
`sp = SerialPort(portname::AbstractString)`
Constructor for the `SerialPort` object.
"""
SerialPort(portname::AbstractString) = SerialPort(sp_get_port_by_name(portname), false)
"""
`set_speed(sp::SerialPort,bps::Integer)`
Set connection speed of `sp` in bits per second. The library will return an
error if bps is not a valid/supported value.
"""
function set_speed(sp::SerialPort, bps::Integer)
sp_set_baudrate(sp.ref, bps)
return nothing
end
"""
`set_frame(sp::SerialPort [, ndatabits::Integer, pariry::SPParity, nstopbits::Integer])`
Configure packet framing. Defaults to the most common "8N1" scheme. See
https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter#Data_framing
for more details.
`ndatabits` is the number of data bits which is `8` in the common "8N1" sceme.
The `parity` is set to none in the "8N1" sceme and can take the values:
`SP_PARITY_NONE`, `SP_PARITY_ODD`, `SP_PARITY_EVEN`, `SP_PARITY_MARK` and
`SP_PARITY_SPACE`.
`nstopbits` is the number of stop bits, which is `1` by default.
"""
function set_frame(sp::SerialPort;
ndatabits::Integer=8,
parity::SPParity=SP_PARITY_NONE,
nstopbits::Integer=1)
sp_set_bits(sp.ref, ndatabits)
sp_set_parity(sp.ref, parity)
sp_set_stopbits(sp.ref, nstopbits)
return nothing
end
"""
`set_flow_control(sp::SerialPort [,rts::SPrts, cts::SPcts, dtr::SPdtr, dst::SPdsr, xonxoff::SPXonXoff])`
Configure flow control settings. Many systems don't support all options.
If an unsupported option is requested, the library will return SP_ERR_SUPP.
`rts` can take the values: `SP_RTS_OFF`, `SP_RTS_ON` and `SP_RTS_FLOW_CONTROL`
and defaults to `SP_RTS_OFF`.
`cts` can take the values: `SP_CTS_IGNORE` and `SP_CTS_FLOW_CONTROL`. Its
default is `SP_CTS_IGNORE`.
`dtr` can take the values: `SP_DTR_OFF`, `SP_DTR_ON`, and `SP_DTR_FLOW_CONTROL`
and defaults to `SP_DTR_OFF`.
`dsr` can take the values: `SP_DSR_IGNORE` and `SP_DSR_FLOW_CONTROL`. Its
default is SP_DSR_IGNORE`.
`xonxoff` can take values: `SP_XONXOFF_DISABLED`, `SP_XONXOFF_IN`,
`SP_XONXOFF_OUT`, and `SP_XONXOFF_INOUT` and defaults to `SP_XONXOFF_DISABLED`.
"""
function set_flow_control(sp::SerialPort;
rts::SPrts=SP_RTS_OFF,
cts::SPcts=SP_CTS_IGNORE,
dtr::SPdtr=SP_DTR_OFF,
dsr::SPdsr=SP_DSR_IGNORE,
xonxoff::SPXonXoff=SP_XONXOFF_DISABLED)
# CTS and RTS must be enabled or disabled as a pair
sp_set_rts(sp.ref, rts)
sp_set_cts(sp.ref, cts)
sp_set_dtr(sp.ref, dtr)
sp_set_dsr(sp.ref, dsr)
sp_set_xon_xoff(sp.ref, xonxoff)
return nothing
end
"""
`listports([nports_guess::Integer])`
Print a list of currently visible ports, along with some basic info.
`nports_guess` provides the number of ports guessed. Its default is `64`.
"""
function list_ports(;nports_guess::Integer=64)
ports = sp_list_ports()
for port in unsafe_wrap(Array, ports, nports_guess, false)
port == C_NULL && return
println(sp_get_port_name(port))
println("\tDescription:\t", sp_get_port_description(port))
println("\tTransport type:\t", sp_get_port_transport(port))
end
sp_free_port_list(ports)
return nothing
end
"""
`print_port_metadata(sp::SerialPort [,show_config::Bool])
Print info found for this port.
Note: port should be open to obtain a valid FD/handle before accessing fields.
`show_config` is `true` by default and prints out the current port settings.
"""
function print_port_metadata(sp::SerialPort; show_config::Bool=true)
print_port_metadata(sp.ref, show_config=show_config)
return nothing
end
function print_port_metadata(port::LibSerialPort.Port; show_config::Bool=true)
println("\nPort name:\t", sp_get_port_name(port))
println("Manufacturer:\t", sp_get_port_usb_manufacturer(port))
println("Product:\t", sp_get_port_usb_product(port))
println("USB serial number:\t", sp_get_port_usb_serial(port))
println("Bluetooth address:\t", sp_get_port_bluetooth_address(port))
println("File descriptor:\t", sp_get_port_handle(port))
bus, addr = sp_get_port_usb_bus_address(port)
if bus != -1
println("USB bus #:\t", bus)
println("Address on bus:\t", addr)
end
vid, pid = sp_get_port_usb_vid_pid(port)
if vid != -1
println("Vendor ID:\t", vid)
println("Product ID:\t", pid)
end
if show_config
print_port_settings(port)
end
return nothing
end
"""
`print_port_settings(sp::SerialPort)`
Print port settings for `sp`.
"""
print_port_settings(sp::SerialPort) = print_port_settings(sp.ref)
function print_port_settings(port::LibSerialPort.Port)
println("Configuration for ", sp_get_port_name(port), ":")
config = sp_get_config(port)
print_port_settings(config)
sp_free_config(config)
end
function print_port_settings(config::LibSerialPort.Config)
println("\tbaudrate\t", sp_get_config_baudrate(config))
println("\tbits\t", sp_get_config_bits(config))
println("\tparity\t", sp_get_config_parity(config))
println("\tstopbits\t", sp_get_config_stopbits(config))
println("\tRTS\t", sp_get_config_rts(config))
println("\tCTS\t", sp_get_config_cts(config))
println("\tDTR\t", sp_get_config_dtr(config))
println("\tDSR\t", sp_get_config_dsr(config))
println("\tXonXoff\t", sp_get_config_xon_xoff(config))
println("")
end
"""
`open(sp::SerialPort [, mode::SPMode])`
Open the serial port `sp`.
`mode` can take the values: `SP_MODE_READ`, `SP_MODE_WRITE`, and
`SP_MODE_READ_WRITE`
"""
function Base.open(sp::SerialPort; mode::SPMode=SP_MODE_READ_WRITE)
sp_open(sp.ref, mode)
return sp
end
"""
`open(portname::AbstractString,baudrate::Integer [,mode::SPMode,
ndatabits::Integer,parity::SPParity,nstopbits::Integer])`
construct, configure and open a `SerialPort` object.
For details on posssible settings see `?set_flow_control` and `?set_frame`.
"""
function Base.open(portname::AbstractString,
bps::Integer;
mode::SPMode=SP_MODE_READ_WRITE,
ndatabits::Integer=8,
parity::SPParity=SP_PARITY_NONE,
nstopbits::Integer=1)
sp = SerialPort(sp_get_port_by_name(portname), false)
sp_open(sp.ref, mode)
set_speed(sp, bps)
set_frame(sp, ndatabits=ndatabits, parity=parity, nstopbits=nstopbits)
return sp
end
"""
`open_serial_port(port_address::AbstractString, baudrate::Integer)`
Create and configure a SerialPort object with the standard 8N1 settings
and specified `baudrate`. Example: `open_serial_port("/dev/ttyACM0", 115200)`
"""
function open_serial_port(port_address::AbstractString, baudrate::Integer)
sp = SerialPort(port_address)
open(sp)
set_speed(sp, baudrate)
set_frame(sp, ndatabits=8, parity=SP_PARITY_NONE, nstopbits=1)
return sp
end
"""
close(sp::SerialPort [, delete::Bool])
Close the serial port `sp`. The optional `delete` keyword argument triggers
a call to `sp_free_port` in the C library if set to `true` (default = false).
"""
function Base.close(sp::SerialPort; delete::Bool=false)
# Flush first, as is done in other close() methods in Base
sp_flush(sp.ref, SP_BUF_BOTH)
sp_close(sp.ref)
if delete
sp_free_port(sp.ref)
end
return sp
end
"""
`flush(sp::SerialPort [, buffer::SPBuffer])`
Flush `buffer` of serial port `sp`.
`buffer` can take the values: `SP_BUF_INPUT`, `SP_BUF_OUTPUT`, and
`SP_BUF_BOTH`.
"""
function Base.flush(sp::SerialPort; buffer::SPBuffer=SP_BUF_BOTH)
sp_flush(sp.ref, buffer)
end
"""
`write(sp::SerialPort, data::String)`
`write(sp::SerialPort, data::Array{Char})`
`write(sp::SerialPort, data::Array{UInt8})`
Write sequence of Bytes to `sp`.
"""
function Base.write(sp::SerialPort, data::Array{UInt8})
sp_nonblocking_write(sp.ref, data)
return sp_drain(sp.ref)
end
function Base.write(sp::SerialPort, data::Array{Char})
sp_nonblocking_write(sp.ref, data)
return sp_drain(sp.ref)
end
function Base.write(sp::SerialPort, data::String)
sp_nonblocking_write(sp.ref, convert(Array{UInt8},data))
return sp_drain(sp.ref)
end
"""
`write(sp::SerialPort, data::UInt8)`
`write(sp::SerialPort, data::Char)`
Write single Byte to `sp`
"""
Base.write(sp::SerialPort, data::Char) = write(sp, [data])
Base.write(sp::SerialPort, data::UInt8) = write(sp, [data])
"""
`write(sp::SerialPort, i::Integer)`
Write string representation of `i` to `sp`.
"""
Base.write(sp::SerialPort, i::Integer) = Base.write(sp, "$i")
"""
`write(sp::SerialPort, f::AbstractFloat)`
`write(sp::SerialPort, f::AbstractFloat, format::AbstractString)`
Write formatted string representation of `f` to `sp`. By default the string is
formated using `format="%.3f"`. For details on the format consult the
documentation of the C library function `sprintf`.
"""
Base.write(sp::SerialPort, f::AbstractFloat, format::AbstractString="%.3f") = Base.write(sp, eval(:@sprintf($format, $f)))
"""
`eof(sp::SerialPort)`
Return EOF state (`true` or `false`) of `sp`.
"""
Base.eof(sp::SerialPort) = sp.eof
"""
`seteof(sp::SerialPort, state::Bool)`
Set EOF of `sp` to `state`
"""
function seteof(sp::SerialPort, state::Bool)
sp.eof = state
return nothing
end
"""
`seteof(sp::SerialPort, state::Bool)`
Reset EOF of `sp` to `false`
"""
reseteof(sp::SerialPort) = seteof(sp, false)
"""
`read(sp::SerialPort, T::Type{UInt8})`
`read(sp::SerialPort, T::Type{Char})`
Read a single Byte from the specified port and return it represented as `T`.
`T` might be either `Char or `UInt8`. if no Byte is availible in the port
buffer return zero.
"""
function Base.read(sp::SerialPort, readType::Type{Char})
nbytes_read, bytes = sp_nonblocking_read(sp.ref, 1)
return (nbytes_read == 1) ? convert(readType,bytes[1]) : readType(0)
end
function Base.read(sp::SerialPort, readType::Type{UInt8})
nbytes_read, bytes = sp_nonblocking_read(sp.ref, 1)
return (nbytes_read == 1) ? convert(readType,bytes[1]) : readType(0)
end
"""
`readuntil(sp::SerialPort,delim::Union{Char,AbstractString,Vector{Char}},timeout_ms::Integer)`
Read until the specified delimiting byte (e.g. `'\\n'`) is encountered, or until
timeout_ms has elapsed, whichever comes first.
"""
function Base.readuntil(sp::SerialPort, delim::Char, timeout_ms::Real)
return readuntil(sp,[delim],timeout_ms)
end
function Base.readuntil(sp::SerialPort, delim::AbstractString, timeout_ms::Real)
return readuntil(sp,convert(Vector{Char},delim),timeout_ms)
end
function Base.readuntil(sp::SerialPort, delim::Vector{Char}, timeout_ms::Real)
start_time = time_ns()
out = IOBuffer()
lastchars = Char[0 for i=1:length(delim)]
while !eof(sp)
if (time_ns() - start_time)/1e6 > timeout_ms
break
end
if nb_available(sp) > 0
c = read(sp, Char)
write(out, c)
lastchars = circshift(lastchars,-1)
lastchars[end] = c
if lastchars == delim
break
end
end
end
return String(take!(out))
end
"""
`Base.nb_available(sp::SerialPort)`
Gets the number of bytes waiting in the input buffer.
"""
Base.nb_available(sp::SerialPort) = Int(sp_input_waiting(sp.ref))
"""
`readbytes!(sp::SerialPort,nbytes::Integer)`
Read `nbytes` from the specified serial port `sp`, without blocking. Returns
a `UInt8` `Array`.
"""
function Base.readbytes!(sp::SerialPort, nbytes::Integer)
nbytes_read, bytes = sp_nonblocking_read(sp.ref, nbytes)
return bytes
end
"""
`readstring(sp::SerialPort)`
Read everything from the specified serial ports `sp` input buffer, one byte at
a time, until it is empty. Returns a `Char` array.
"""
function Base.readstring(sp::SerialPort)
result = Char[]
while Int(nb_available(sp)) > 0
byte = readbytes!(sp, 1)[1]
push!(result, byte)
end
return join(result)
end