API
za_serial.h
Go to the documentation of this file.
1 /**
2  * \file za_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 ASCII protocol.
10  */
11 
12 #if !defined(ZA_SERIAL_H)
13 #define ZA_SERIAL_H
14 
15 #if defined(__cplusplus)
16 extern "C"{
17 #endif /* if defined(__cplusplus) */
18 
19 #include "z_common.h"
20 
21 /** Provides programmatic access to reply data.
22  *
23  * This struct is provided as a convenience to allow for easier interaction
24  * with ASCII replies. The names of the fields are taken straight from the
25  * ASCII protocol manual.
26  *
27  * Note that the response data may contain more than one piece of data:
28  * because many replies will return multiple pieces of data, the response_data
29  * field will simply contain the end of the message, without the newline
30  * terminators. (ie. "\r\n")
31  */
32 struct za_reply {
33  /** The message type will always be '@' for replies.
34  * Info will have the type '#', and alerts '!'. */
35  char message_type;
36  /** The address of the device, an integer between 1 and 99. */
37  int device_address;
38  /** The axis number: 0-9, where 0 refers to the whole device,
39  * and 1-9 refer only to that specific axis. */
40  int axis_number;
41  /** Whether a command was accepted or rejected: either "OK" or "RJ". */
42  char reply_flags[3];
43  /** Either "BUSY" when the axis is moving, or "IDLE" when it isn't. */
44  char device_status[5];
45  /** The highest priority warning for the device,
46  * or -- under normal conditions. */
47  char warning_flags[3];
48  /** The response for the command executed. See the protocol manual entry
49  * for your command of choice to know what to expect here. */
50  char response_data[128];
51 };
52 
53 /** Connect to a serial port specified by \a port_name.
54  *
55  * Configures the port to the ASCII protocol defaults (115200 baud, 8N1).
56  * If you have set your device to run at a different baud rate, use
57  * za_setbaud() to change it after connecting using this function.
58  *
59  * On Linux the port name will likely be something like "/dev/ttyUSB0",
60  * on Windows "COM1", and on OS X and BSD systems "/dev/cu.usbserial-A4017CQX".
61  * It is important that OS X/BSD systems use the "callout" (cu.* or cua.*) port
62  * as opposed to the "dial in" ports with names starting with tty.*,
63  * which will not work with Zaber devices.
64  *
65  * If you are re-using a `z_port`, make sure to use za_disconnect() to
66  * disconnect the old port before overwriting it with a new one.
67  *
68  * \param[out] port a pointer to a `z_port` to be written-over with the
69  * newly-connected port.
70  * \param[in] port_name a string containing the name of the port to be opened.
71  *
72  * \return Z_SUCCESS on success, Z_ERROR_NULL_PARAMETER if \a port or
73  * \a port_name is NULL, or Z_ERROR_SYSTEM_ERROR in case of system error.
74  */
75 int za_connect(z_port *port, const char *port_name);
76 
77 /** Gracefully closes a connection.
78  *
79  * \param[in] port the port to be disconnected.
80  *
81  * \return Z_SUCCESS on success, Z_ERROR_SYSTEM_ERROR in case of system error.
82  */
83 int za_disconnect(z_port port);
84 
85 /** Sends a command to a serial port.
86  *
87  * Automatically adds a '/' to begin the command and a '\\n' to end it if
88  * these characters are omitted from \a command.
89  *
90  * \param[in] port the port to which to send the command.
91  * \param[in] command a string containing the command to be sent.
92  *
93  * \return the number of bytes written, Z_ERROR_NULL_PARAMETER if \a command is
94  * NULL, or Z_ERROR_SYSTEM_ERROR in case of system error.
95  */
96 int za_send(z_port port, const char *command);
97 
98 /** Reads a message from the serial port.
99  *
100  * Blocks while reading until it encounters a newline character or its
101  * timeout has elapsed.
102  *
103  * Note: It is recommended that your \a destination buffer be 256B long.
104  * This is long enough to hold any reply from a Zaber device using the
105  * ASCII protocol.
106  *
107  * Note that this function will simply read the first message on the input
108  * buffer, whatever it may be. If you have sent many commands without
109  * receiving their corresponding replies, sorting them all out may be a real
110  * headache. Note also that the input buffer is finite, and allowing it to
111  * fill up will result in undefined behaviour. Try to receive responses to
112  * every command you send, or use za_drain() when necessary.
113  *
114  * Passing NULL for \a destination and 0 for \a length will read a single
115  * reply, discarding it as it is read. This is useful for keeping your
116  * commands and replies straight without using zb_drain() when you don't care
117  * about the contents of most of the replies.
118  *
119  * \param[in] port the port to receive a message from.
120  * \param[out] destination a pointer to which to write the reply read.
121  * \param[in] length the length of the buffer pointed to by \a destination.
122  *
123  * \return the number of bytes read, Z_ERROR_SYSTEM_ERROR in case of system
124  * error, or Z_ERROR_BUFFER_TOO_SMALL if \a length is insufficient to store
125  * the reply.
126  */
127 int za_receive(z_port port, char *destination, int length);
128 
129 /** Build a #za_reply struct from a string pointed-to by reply.
130  *
131  * The #za_reply struct can then be used to gain easier access to the parts
132  * of an ASCII reply.
133  *
134  * \param[out] destination a pointer to a za_reply struct to be populated
135  * with the data found in \a reply.
136  * \param[in] reply a pointer to a string containing a full reply from a Zaber
137  * device, as specified by the ASCII protocol manual.
138  *
139  * \return Z_SUCCESS on success, Z_ERROR_NULL_PARAMETER if \a destination or
140  * \a reply is NULL, or Z_ERROR_COULD_NOT_DECODE if the reply is malformed.
141  */
142 int za_decode(struct za_reply *destination, char *reply);
143 
144 /** Changes the baud rate of both input and output.
145  *
146  * This function is unlikely to be necessary for typical use, as za_connect()
147  * already sets the baud rate to 115200, the recommended rate for the
148  * ASCII protocol.
149  *
150  * Note: za_setbaud() flushes both input and output buffers to ensure a
151  * "clean slate" after the baud rate has been changed.
152  *
153  * Also note that this sets the baud rate at which the program tries to talk
154  * to the device. It does not change the baud rate of the device itself. See
155  * the ASCII Protocol Manual for info on how to change the device's baud rate.
156  *
157  * Valid baid rates are 9600, 19200, 38400, 57600, and 115200.
158  *
159  * \param[in] port the port to change.
160  * \param[in] baud the new desired baud rate.
161  *
162  * \return Z_SUCCESS on success, Z_ERROR_INVALID_BAUDRATE if the baud rate is not one
163  * specified above, or Z_ERROR_SYSTEM_ERROR in case of system error.
164  */
165 int za_setbaud(z_port port, int baud);
166 
167 /** Flushes all input received but not yet read, and attempts to drain all
168  * input that would be incoming.
169  *
170  * This function is intended to be used when many commands have been sent
171  * without reading any responses, and there is a need to read a response from
172  * a certain command. In other words, call this function before sending a
173  * command whose response you would like to read if you have not been
174  * consistently reading the responses to previous commands.
175  *
176  * This function will always take at least 100ms to complete, as it tries to
177  * read input until it is certain no more is arriving by waiting 100ms before
178  * deciding there is no more input incoming. Do not use it for
179  * time-sensitive operations, or in any kind of "chatty" setup, eg. multiple
180  * devices daisy-chained together with move tracking enabled. In such a setup,
181  * the only reliable way to retrieve reply data is to always follow
182  * calls to za_send() with calls to za_receive().
183  *
184  * \param[in] port the port to drain.
185  *
186  * \return Z_SUCCESS on success, or Z_ERROR_SYSTEM_ERROR in case of system error.
187  */
188 int za_drain(z_port port);
189 
190 /** Sets whether errors and extra info are reported to `stderr`.
191  *
192  * Set \a value to 0 to disable all output. Additionally, you can compile this
193  * library with the macro NDEBUG defined, which will disable all output and
194  * skip checks to "verbose" altogether in the compiled code.
195  *
196  * \param[in] value whether or not the program should output error messages
197  * and info to `stderr`.
198  */
199 void za_set_verbose(int value);
200 
201 #if defined(__cplusplus)
202 }
203 #endif /* if defined(__cplusplus) */
204 
205 #endif /* if !defined(ZA_SERIAL_H) */
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4001
int za_decode(struct za_reply *destination, const char *reply)
Definition: za_serial.c:527
int za_connect(z_port *port, const char *port_name)
Definition: za_serial.c:53
int za_setbaud(z_port port, int baud)
Definition: za_serial.c:197
void za_set_verbose(int value)
Definition: za_serial.c:18
int za_receive(z_port port, char *destination, int length)
Definition: za_serial.c:148
int za_send(z_port port, const char *command)
Definition: za_serial.c:112
int za_drain(z_port port)
Definition: za_serial.c:232
int za_disconnect(z_port port)
Definition: za_serial.c:99
Defines a few things that all of the serial API has in common.
char device_status[5]
Definition: za_serial.h:44
char message_type
Definition: za_serial.h:35
int device_address
Definition: za_serial.h:37
char reply_flags[3]
Definition: za_serial.h:42
char warning_flags[3]
Definition: za_serial.h:47
int axis_number
Definition: za_serial.h:40
char response_data[128]
Definition: za_serial.h:50