-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.txt
154 lines (117 loc) · 4.82 KB
/
README.txt
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
cl-libserialport - a Common Lisp interface to the libserialport C library.
libserialport is a "is a minimal, cross-platform shared library
written in C that is intended to take care of the OS-specific details
when writing software that uses serial ports."
See https://sigrok.org/wiki/Libserialport
It is nice because it is easy to use, cross-platform, and provides
straightforward access to traditional, bluetooth, and USB serial
devices.
Depends on: BABEL, CFFI, BORDEAUX-THREADS
This Lisp package provides an interface to all exported functions
of libserialport.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DOCUMENTATION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
All functions have documentation strings. Qualities are described
by keywords in libserialport-ffi.lisp, taken from original C enums.
;; Lisp object, containing a pointer to the low level
;; serial port object
SERIAL-PORT
;; Lisp object describing a serial port, created by LIST-SERIAL-PORTS
;; and BUILD-SERIAL-PORT-DESCRIPTION
SERIAL-PORT-DESCRIPTION
;; A hash with location eg key="/usr/dev/tty.serial" and val=SERIAL-PORT
;; that keeps track of open serial ports - because of this
;; serial ports don't get garbage collected
*SERIAL-PORT-HASH*
;; list serial ports on a computer, as SERIAL-PORT-DESCRIPTION objects
(LIST-SERIAL-PORTS)
;; create a SERIAL-PORT-DESCRIPTION from a SERIAL-PORT
(BUILD-SERIAL-PORT-DESCRIPTION SERIAL-PORT)
;; shut down open serial port - this must be done to deallocate foreign
;; memory consisting of the port, and a buffer
(SHUTDOWN-SERIAL-PORT SERIAL-PORT)
;; shut down all open ports in *SERIAL-PORT-HASH*
(SHUTDOWN-ALL-SERIAL-PORTS)
;; open a named serial port, with various qualities given by keywords.
(OPEN-SERIAL-PORT "/dev/tty.serial"
&KEY
:MODE :SP-MODE-READ-WRITE (etc)
:BAUD XXXX
:BITS 1
:PARITY :SP-PARITY-NONE (etc)
:STOPBITS 1
:XONXOFF :SP-XONXOFF-DISABLED
:FLOWCONTROL
:RTS rts :CTS cts :DTR dtr :DSR dsr);; see keywords in libserialport-ffi.lisp
r
(SET-SERIAL-PORT-QUALITIES :SERIAL-PORT :BAUD ...) - set a serial port's
qualities
;; Write DATA (an octet, octet-vector, or string)
;; Returns the number of octets written.
(SERIAL-WRITE-DATA SERIAL-PORT DATA
:ENCODING :LATIN-1 ;; Babel encoding for ougoing octets, if DATA is a string
:BLOCKING NIL ;; use blocking IO with timeout
:TIMEOUT 0 ;; milliseconds, if BLOCKING is true; 0 means forever
:LINE-END NIL) ;; or :cr, :lf, :crlf if we want to end this as a line
;; Read a vector of octets of length COUNT, or a single
;; bare octet if COUNT=-1
;; Returns (VALUES OCTETS NUM-OCTETS-READ)
(SERIAL-READ-OCTETS SERIAL-PORT COUNT
:BLOCKING NIL
:TIMEOUT 1000
:OCTET-BUF NIL ;; optional (unsigned-byte 8) vector with fill-pointer
:APPEND NIL) ;; append at fill pointer if true
;; read one octet, or NIL if timeout
(SERIAL-READ-OCTET SERIAL-PORT
:BLOCKING NIL
:TIMEOUT 1000)
;; read one char between 0-255
(SERIAL-READ-CHAR8 SERIAL-PORT
:BLOCKING NIL
:TIMEOUT 1000)
;; Read octets until it hits FINAL-OCTET, which is not returned. The
;; character readling loop checks for character ready, and uses a
;; short internal SLEEP if not, while checking for elapsed time, so
;; the timeout is imposed at the library levelif BLOCKING,
;; and always at the Lisp level.
(SERIAL-READ-OCTETS-UNTIL SERIAL-PORT FINAL-OCTAT
:BLOCKING NIL
:TIMEOUT 1000
:MAX-LENGTH 65536
:OCTET-BUF NIL) ;; optional (unsigned-byte 8) vector with fill-pointer
;; read a line (string), converting octets to chars using Babel
(SERIAL-READ-LINE SERIAL-PORT
:BLOCKING NIL
:TIMEOUT 1000
:MAX-LENGTH 65536 ;; octets, not chars
:LINE-TERMINATION-CHAR #\Linefeed
:IGNORE-FINAL-CARRIAGE-RETURN T ;; ignore #\cr before #\lf
:ENCODING :LATIN-1) ;; Babel encoding
;; return number of bytes waiting on serial port
(SERIAL-INPUT-WAITING SERIAL-PORT)
(SERIAL-OUTPUT-WAITING SERIAL-PORT)
;; flush buffers, dumping their data
(SERIAL-FLUSH-BUFFER SERIAL-PORT
:FLUSH-INPUT-BUFFER T
:FLUSH-OUTPUT-BUFFER T)
;; wait until buffers drain - no timeout possible
(SERIAL-DRAIN SERIAL-PORT)
;; lisp object representing an event-set, an obect attached to one
;; or more SERIAL-PORTS, waiting for an event (select mechanism)
EVENT-SET
;; make a new event set
(CREATE-EVENT-SET)
;; delete and unallocate an event set
(DESTROY-EVENT-SET EVENT-SET)
;; add a serial port to an event set, so the event set can wait
;; on it
(ADD-SERIAL-PORT-TO-EVENT-SET SERIAL-PORT EVENT-SET
:RX-READY T ;; wait for incoming data
:TX-READY T ;; wait for ready to write
:ERROR T) ;; wait for error
;; wait on an event set, returning after timeout, or after one
;; of the connect serial ports has a pending event. Then
;; SERIAL-INPUT-WAITING, SERIAL-OUTPUT-WAITING can be applied to
;; the SERIAL-PORTS
(EVENT-WAIT EVENT-SET :TIMEOUT TIMEOUT)