API
 
Loading...
Searching...
No Matches
example.c
Go to the documentation of this file.
1/*
2 * This file is intended to give examples of typical operations one might
3 * want to perform using the Zaber Thin API in C. The examples may need to
4 * be adapted slightly to work with your program.
5 *
6 * Here the example assumes a single device is connected to "/dev/ttyUSB0",
7 * using the ASCII protocol at the default baud rate (115200). It also assumes
8 * it is being compiled and run on a machine using glibc, eg. Linux.
9 */
10
11/* this #define is required for nanosleep: see feature_test_macros(7) */
12#define _POSIX_C_SOURCE 199309L
13#include <time.h>
14#include <stdio.h>
15#include <string.h>
16
17#include "za_serial.h"
18
19void poll_until_idle(z_port port)
20{
21 char reply[256] = { 0 };
22 struct za_reply decoded_reply;
23 const struct timespec ts = { 0, 100000000 }; /* 100mil nanosec = 100ms */
24
25 /* We use za_decode() to decode this string into more manageable parts,
26 * sorting them into the fields of a za_reply struct, then we test
27 * the device_status field. Possible values for device_status are "IDLE"
28 * and "BUSY". */
29 for(;;)
30 {
31 za_send(port, "/\n");
32 za_receive(port, reply, sizeof(reply));
33 za_decode(&decoded_reply, reply);
34
35 if(strncmp(decoded_reply.device_status, "BUSY", 4) == 0)
36 {
37 nanosleep(&ts, NULL); /* If we're busy, wait then try again */
38 }
39 else
40 {
41 break;
42 }
43 }
44}
45
46/* We take some shortcuts here in main() for the sake of demonstration and code
47 * clarity, namely not checking every function's return value. Special care has
48 * been taken in this API to provide meaningful return values, and we encourage
49 * you to check every one of them in your final production-quality code! */
50int main()
51{
52 z_port port;
53 char reply[256] = { 0 };
54 char *device_name = "/dev/ttyUSB0";
55
56 if (za_connect(&port, device_name) != Z_SUCCESS)
57 {
58 printf("Could not connect to device %s.\n", device_name);
59 return -1;
60 }
61
62 za_send(port, "/home\n");
63 za_receive(port, reply, sizeof(reply));
64 if (reply[0] == '\0')
65 {
66 printf("Read no reply from device %s. "
67 "It may be running at a different baud rate "
68 "and/or in the binary protocol.\n", device_name);
69 return -1;
70 }
71
72 poll_until_idle(port);
73
74 /* NB: The position 100,000 microsteps may be beyond the range of travel
75 * of your device. This example was written for devices with a long
76 * travel range, such as the A-LSQ450D. Adjust this number as necessary. */
77 za_send(port, "/move abs 100000\n");
78 za_receive(port, reply, sizeof(reply));
79
80 poll_until_idle(port);
81
82 za_disconnect(port);
83
84 return 0;
85}
86
void poll_until_idle(z_port port)
Definition example.c:19
int main()
Definition example.c:50
@ Z_SUCCESS
Definition z_common.h:65
int za_connect(z_port *port, const char *port_name)
Definition za_serial.c:53
int za_receive(z_port port, char *destination, int length)
Definition za_serial.c:151
int za_decode(struct za_reply *destination, const char *reply, size_t sMaxSz)
Definition za_serial.c:539
int za_send(z_port port, const char *command, size_t sMaxSz)
Definition za_serial.c:112
int za_disconnect(z_port port)
Definition za_serial.c:99
Provides a set of functions for interacting with Zaber devices in the ASCII protocol.
char device_status[5]
Definition za_serial.h:44