API
zb_serial.h
Go to the documentation of this file.
1 /**
2  * \file zb_serial.h
3  * \author Eric Dand
4  * \version 1.0
5  * \date 28 November 2014
6  * \copyright Apache Software License Version 2.0
7  *
8  * \brief Provides a set of functions for interacting with Zaber devices in
9  * the binary protocol.
10  *
11  * Before using this library, it is recommended to read about the message
12  * format of the binary protocol. The manual can be found at the link below,
13  * as of November 2014:
14  *
15  * http://www.zaber.com/wiki/Manuals/Binary_Protocol_Manual#Message_Format
16  *
17  * Note especially that binary replies are sent when a command has *finished*
18  * as opposed to when the command was received. It is up to you to wait for
19  * appropriate responses to commands.
20  */
21 
22 #if !defined(ZB_SERIAL_H)
23 #define ZB_SERIAL_H
24 
25 #if defined(__cplusplus)
26 extern "C"{
27 #endif /* if defined(__cplusplus) */
28 
29 #include "z_common.h"
30 
31 /** Connect to a serial port specified by \a port_name.
32  *
33  * Configures the port to the binary protocol defaults (9600 baud, 8N1).
34  * It is not recommended to use a device in binary at any other baud rate.
35  *
36  * On Linux the port name will likely be something like "/dev/ttyUSB0",
37  * on Windows "COM1", and on OS X and BSD systems "/dev/cu.usbserial-A4017CQX".
38  * It is important that OS X/BSD systems use the "callout" (cu.* or cua.*) port
39  * as opposed to the "dial in" ports with names starting with tty.*,
40  * which will not work with Zaber devices.
41  *
42  * If you are re-using a `z_port`, make sure to use zb_disconnect() to
43  * disconnect the old port before overwriting it with a new one.
44  *
45  * \param[out] port a pointer to a `z_port` to be written-over with the
46  * newly-connected port.
47  * \param[in] port_name a string containing the name of the port to be opened.
48  *
49  * \return Z_SUCCESS on success, Z_ERROR_NULL_PARAMETER if \a port or
50  * \a port_name is NULL, or Z_ERROR_SYSTEM_ERROR in case of system error.
51  */
52 int zb_connect(z_port *port, const char *port_name);
53 
54 /** Gracefully closes a connection.
55  *
56  * \param[in] port the port to be disconnected.
57  *
58  * \return Z_SUCCESS on success, Z_ERROR_SYSTEM_ERROR on failure.
59  */
60 int zb_disconnect(z_port port);
61 
62 /** Sends a command to a serial port.
63  *
64  * It is recommended that before sending any commands, you read the Binary
65  * Protocol Manual to understand how these commands should be formatted
66  * (least-significant byte first). See zb_encode() for help encoding commands.
67  *
68  * Note that this function does not typically crash if \a command is not six
69  * (or greater) bytes long: it will simply write the first six bytes found at
70  * the address \a command, whatever they may be. It is up to you to make sure
71  * that \a command is long enough.
72  *
73  * \param[in] port the port to which to send the command.
74  * \param[in] command a string of bytes containing the command to be sent.
75  *
76  * \return the number of bytes written (will always be 6 if successful), or
77  * Z_ERROR_SYSTEM_ERROR on failure.
78  */
79 int zb_send(z_port port, const uint8_t *command);
80 
81 /** Receives a message from the serial port.
82  *
83  * Blocks while reading until 6 bytes have been read, or its timeout has
84  * elapsed. The default timeout is defined by #READ_TIMEOUT, and can also be
85  * changed programmatically by zb_set_timeout().
86  *
87  * The reply received will follow the message format specified
88  * in Zaber's Binary Protocol Manual. This means that the final 4 bytes
89  * received will be ordered from least to most significant. It is recommended
90  * to use zb_decode() to get the last 4 bytes of data as a convenient,
91  * well-formed 32-bit integer that matches your system.
92  *
93  * Note that this function will simply read the first six bytes on the input
94  * buffer, whatever they may be. If you have sent many commands without
95  * receiving their corresponding replies, sorting them all out may be a real
96  * headache. Note also that this buffer is finite, and allowing it to
97  * fill up will result in undefined behaviour. See zb_drain() for a solution
98  * to this problem.
99  *
100  * \param[in] port the port to receive a message from.
101  * \param[out] destination a pointer to which to write the reply read. Must be
102  * at least 6 bytes long.
103  *
104  * \return the number of bytes received (will always be 6 if successful), or
105  * Z_ERROR_SYSTEM_ERROR on failure.
106  */
107 int zb_receive(z_port port, uint8_t *destination);
108 
109 /** Encodes a command according to Zaber's Binary Protocol Manual.
110  *
111  * This function performs the necessary transposition of data bytes for
112  * transmission to Zaber devices using the binary protocol.
113  *
114  * This function does not support the encoding of Message IDs.
115  *
116  * \param[out] destination a pointer to an array to which to write the encoded
117  * command. Assumed to be at least 6 bytes long.
118  * \param[in] device_number the number of the device to which to send the
119  * command.
120  * \param[in] command_number the number of the command to be sent.
121  * \param[in] data the data to be sent along with the command. The contents of
122  * this field depend on the command number being sent. See the Binary Protocol
123  * Manual for info on specific commands.
124  *
125  * \return Z_SUCCESS on success, or Z_ERROR_NULL_PARAMETER if \a destination
126  * is NULL.
127  */
128 int zb_encode(uint8_t *destination, uint8_t device_number,
129  uint8_t command_number, int32_t data);
130 
131 /** Decodes the last 4 bytes of a 6-byte (complete) reply.
132  *
133  * This function wraps the last four bytes of the reply into a 32-bit integer
134  * matching the architecture of the compilation target (typically the machine
135  * which compiled the code, unless otherwise specified).
136  *
137  * This function only writes the "data" portion of a reply (the last four
138  * bytes) into \a destination. You will still need to manually read the first
139  * two bytes of your reply to get the device and command IDs.
140  *
141  * This function does not support the decoding of Message IDs. If your device
142  * does have Message IDs enabled, the data decoded by this function will be
143  * incorrect and should be considered undefined.
144  *
145  * \param[out] destination a pointer to a 32-bit integer to which to write the
146  * reply's data.
147  * \param[in] reply a pointer to an array containing a 6-byte reply message.
148  *
149  * \return Z_SUCCESS on success, or Z_ERROR_NULL_PARAMETER if \a destination
150  * or \a reply is NULL.
151  */
152 int zb_decode(int32_t *destination, const uint8_t *reply);
153 
154 /** Flushes all input received but not yet read, and attempts to drain all
155  * input that would be incoming.
156  *
157  * This function is intended to be used when many commands have been sent
158  * without reading any responses, and there is a need to read a response from
159  * a certain command. In other words, call this function before sending a
160  * command whose response you would like to read if you have not been
161  * consistently reading the responses to previous commands.
162  *
163  * This function will always take at least 100ms to complete, as it tries to
164  * read input until it is certain no more is arriving by waiting 100ms before
165  * deciding there is no more input incoming. Do not use it for
166  * time-sensitive operations, or in any kind of "chatty" setup, eg. multiple
167  * devices daisy-chained together with move tracking enabled. In such a setup,
168  * the only reliable way to retrieve any reply data is to always follow
169  * calls to zb_send() with calls to zb_receive().
170  *
171  * \param[in] port the port to drain.
172  *
173  * \return Z_SUCCESS on success, or Z_ERROR_SYSTEM_ERROR on failure.
174  */
175 int zb_drain(z_port port);
176 
177 /** Change the duration zb_receive() will wait before timing out and returning
178  * without having read anything.
179  *
180  * A Zaber device using the binary protocol will only respond once a command
181  * has been completed. Though we recommend users implement their own "wait"
182  * behaviour for commands they expect to take a long time, this function can
183  * be used to wait for such commands to complete before continuing execution.
184  *
185  * A value of 0 for milliseconds will block indefinitely until a reply is
186  * received. Use with caution.
187  *
188  * On *NIX systems, the value of milliseconds will be rounded up to the
189  * nearest tenth of a second (0.1s). This function returns the new timeout
190  * value to aid with compatibility between Windows and *NIX.
191  *
192  * \param[in] port the port whose timeout shall be changed.
193  * \param[in] milliseconds the duration of the new timeout, in milliseconds.
194  *
195  * \return the new timeout value on success, Z_ERROR_SYSTEM_ERROR on failure.
196  */
197 int zb_set_timeout(z_port port, int milliseconds);
198 
199 /** Sets whether errors and extra info are reported to stderr.
200  *
201  * Set \a value to 0 to disable all output. Additionally, you can compile this
202  * library with the macro NDEBUG defined, which will disable all output and
203  * skip checks to "verbose" altogether in the compiled code.
204  *
205  * \param[in] value whether (1) or not (0) the program should output
206  * error messages and info to stderr.
207  */
208 void zb_set_verbose(int value);
209 
210 #if defined(__cplusplus)
211 }
212 #endif /* if defined(__cplusplus) */
213 
214 #endif /* if !defined(ZB_SERIAL_H) */
215 
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4001
Defines a few things that all of the serial API has in common.
int zb_disconnect(z_port port)
int zb_send(z_port port, const uint8_t *command)
int zb_decode(int32_t *destination, const uint8_t *reply)
Definition: zb_serial.c:48
int zb_encode(uint8_t *destination, uint8_t device_number, uint8_t command_number, int32_t data)
Definition: zb_serial.c:25
void zb_set_verbose(int value)
Definition: zb_serial.c:20
int zb_receive(z_port port, uint8_t *destination)
int zb_drain(z_port port)
int zb_set_timeout(z_port port, int milliseconds)
int zb_connect(z_port *port, const char *port_name)