]> sigrok.org Git - libserialport.git/blame - README
Remove outdated inline documentation, README is now authoritative.
[libserialport.git] / README
CommitLineData
0a16d4de
ML
1----------------------------------------------------------------
2libserialport: cross-platform library for accessing serial ports
3----------------------------------------------------------------
4
5libserialport is a minimal library written in C that is intended to take care
6of the OS-specific details when writing software that uses serial ports.
7
8By writing your serial code to use libserialport, you enable it to work
9transparently on any platform supported by the library.
10
11The operations that are supported are:
12
13- Port enumeration (obtaining a list of serial ports on the system).
14- Opening and closing ports.
15- Setting port parameters (baud rate, parity, etc).
16- Reading, writing and flushing data.
17- Obtaining error information.
18
19libserialport is an open source project released under the LGPL3+ license.
20
21Status
22======
23
24The library should build and work on any Windows or Unix-based system. If it
25does not, please submit a bug.
26
27Enumeration is currently only implemented on Windows, Mac OS X and Linux. On
28other systems enumeration will return no results, but ports can still be opened
29by name and then used.
30
31If you know how to enumerate available ports on another OS, please submit a bug
32with this information, or better still a patch implementing it.
33
34Future
35======
36
37Future versions will add additional API calls for obtaining metadata about a
38port, e.g. for USB devices the USB VID and PID of the underlying device.
39
40Dependencies
41============
42
43On Linux, libudev is required. On other systems no other libraries are required.
44
45The libudev dependency could be eliminated in favour of direct sysfs queries at
46the cost of some brevity. This is not currently a priority but if you feel like
47doing this feel free to submit a patch.
48
49Building
50========
51
52The package uses a GNU style build system and requires a Unix style shell.
53On Windows it can be built with the MinGW toolchain and MSYS environment.
54
55Run "./autogen.sh" to generate the build system, "./configure" to setup, then
56"make" to build the library and "make install" to install it.
57
58API
59===
60
61The API is simple, and designed to be a minimal wrapper around the serial port
62support in each OS.
63
d54e9004
ML
64Most functions take a pointer to a struct sp_port, which represents an serial
65port. This structure is obtained from the array returned by sp_list_ports().
0a16d4de 66
e9a2f9c9 67All functions can return only three possible error values. SP_ERR_ARG indicates
0a16d4de 68the function was called with invalid arguments. SP_ERR_FAIL indicates that the
e9a2f9c9 69OS reported a failure. SP_ERR_MEM indicates that a memory allocation failed.
01619328 70All of these error values are negative.
0a16d4de
ML
71
72When SP_ERR_FAIL is returned, an error code or string description of the error
73can be obtained by calling sp_last_error_code() or sp_last_error_message(). The
74error code or message is that provided by the OS; libserialport does not define
75any error codes or messages of its own.
76
77Functions calls that succeed return SP_OK, which is equal to zero, or where
78otherwise documented a positive value.
79
80The available functions are as follows:
81
82Enumeration
83-----------
84
01619328 85int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
0a16d4de 86
01619328
ML
87 Obtains a pointer to a new sp_port structure representing the named port. The
88 user should allocate a variable of type "struct sp_port *" and pass a pointer
89 to this to receive the result.
b9a462bb
ML
90
91 The result should be freed after use by calling sp_free_port().
01619328 92
b9a462bb
ML
93 Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
94 failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
95 is returned, the variable pointed to by port_ptr will be set to NULL.
96 Otherwise, it will be set to point to the newly allocated port.
01619328
ML
97
98void sp_free_port(struct sp_port *port);
99
100 Frees a port structure obtained from sp_get_port_by_name().
101
102int sp_list_ports(struct sp_port ***list_ptr);
103
104 Lists the serial ports available on the system. The result obtained is an
105 array of pointers to sp_port structures, terminated by a NULL. The user should
106 allocate a variable of type "struct sp_port **" and pass a pointer to this to
107 receive the result.
108
109 The result should be freed after use by calling sp_free_port_list().
110
b9a462bb
ML
111 Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
112 failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
113 is returned, the variable pointed to by list_ptr will be set to NULL.
114 Otherwise, it will be set to point to the newly allocated array.
0a16d4de 115
d54e9004 116void sp_free_port_list(struct sp_port **list);
0a16d4de 117
01619328 118 Frees a port list obtained from sp_list_ports().
0a16d4de
ML
119
120Opening and closing ports
121-------------------------
122
d54e9004 123int sp_open(struct sp_port *port, int flags);
0a16d4de
ML
124
125 Opens the specified serial port.
126
127 Parameters:
128
d54e9004 129 port: Pointer to port structure.
0a16d4de
ML
130 flags: Flags to use when opening the serial port. Possible
131 flags are: SP_MODE_RDWR, SP_MODE_RDONLY, and SP_MODE_NONBLOCK.
132
133 Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
01619328 134 if an invalid port is passed.
0a16d4de
ML
135
136int sp_close(struct sp_port *port);
137
138 Closes the specified serial port.
139
140 Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
141 if an invalid port is passed.
142
143Setting port parameters
144-----------------------
145
146int sp_set_params(struct sp_port *port, int baudrate,
147 int bits, int parity, int stopbits,
148 int flowcontrol, int rts, int dtr);
149
150 Sets serial parameters for the specified serial port.
151
152 Parameters:
153
154 port: Pointer to port structure.
155 baudrate: Baud rate to set.
156 bits: Number of data bits to use.
157 parity: Parity setting to use
158 (SP_PARITY_NONE, SP_PARITY_EVEN or SP_PARITY_ODD)
159 stopbits: Number of stop bits to use (1 or 2).
160 flowcontrol: Flow control setting to use
161 (SP_FLOW_NONE, SP_FLOW_HARDWARE or SP_FLOW_SOFTWARE)
162
163 Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
164 for invalid arguments.
165
166Reading, writing and flushing data
167----------------------------------
168
169int sp_read(struct sp_port *port, const void *buf, size_t count)
170
25ab82f6
ML
171 Reads bytes from the specified serial port. Note that this function may
172 return after reading less than the specified number of bytes; it is the
173 user's responsibility to iterate as necessary in this case.
0a16d4de
ML
174
175 Parameters:
176
177 port: Pointer to port structure.
178 buf: Buffer in which to store the bytes read.
25ab82f6 179 count: Maximum number of bytes to read.
0a16d4de
ML
180
181 Returns: The number of bytes read, SP_ERR_FAIL on failure,
182 or SP_ERR_ARG for invalid arguments.
183
184int sp_write(struct sp_port *port, const void *buf, size_t count)
185
25ab82f6
ML
186 Write bytes to the specified serial port. Note that this function may
187 return after writing less than the specified number of bytes; it is the
188 user's responsibility to iterate as necessary in this case.
0a16d4de
ML
189
190 Parameters:
191
192 port: Pointer to port structure.
193 buf: Buffer containing the bytes to write.
25ab82f6 194 count: Maximum number of bytes to write.
0a16d4de
ML
195
196 Returns: The number of bytes written, SP_ERR_FAIL on failure,
197 or SP_ERR_ARG for invalid arguments.
198
199int sp_flush(struct sp_port *port);
200
201 Flushes serial port buffers.
202
203 Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
204 if an invalid port is passed.
205
206Error handling
207--------------
208
209int sp_last_error_code();
210
211 Gets the error code for a failed operation.
212
213 In order to obtain the correct result, this function should be called
214 straight after the failure, before executing any other system operations.
215
216 Returns: The system's numeric code for the error that caused the last
217 operation to fail.
218
219char *sp_last_error_message();
220
221 Gets the error message for failed operation.
222
223 In order to obtain the correct result, this function should be called
224 straight after the failure, before executing other system operations.
225
226 Returns: The system's message for the error that caused the last
227 operation to fail. This string may be allocated by the function,
228 and should be freed after use by calling sp_free_error_message.
229
230void sp_free_error_message(char *message);
231
232 Frees the error message returned by sp_last_error_message().