![]() |
gclib 2.4.0
Galil Communications Library
|
Get unsolicited messages, interrupts, or data records, in blocking or callback mode. More...
Functions | |
| gclib_result | gclib_set_interrupts (gclib_handle h, gclib_interrupt_type interrupt_mask, gclib_axis_flags motion_complete_axes, gclib_digital_input_flags digital_inputs) |
| Configure which interrupts will be generated by the controller. | |
| gclib_result | gclib_set_data_records (gclib_handle h, size_t period_ms) |
| Configure controller data records. | |
| gclib_result | gclib_subscribe_messages (gclib_handle h, void(callback)(void *user_data, const char *message), void *user_data) |
| Subscribe to unsolicited messages. | |
| gclib_result | gclib_subscribe_interrupts (gclib_handle h, void(callback)(void *user_data, gclib_interrupt_t interrupt), void *user_data) |
| Subscribe to interrupts. | |
| gclib_result | gclib_subscribe_data_records (gclib_handle h, void(callback)(void *user_data, gclib_data_record_handle data_record), void *user_data) |
| Subscribe to data records. | |
| gclib_result | gclib_subscribe_progress (gclib_handle h, void(callback)(void *user_data, size_t current, size_t max), void *user_data) |
| Subscribe to progress for gclib_set_program(), gclib_set_array(), and gclib_set_firmware(). | |
| gclib_result | gclib_unsubscribe (gclib_handle h, void *callback) |
| Unsubscribe from messages, interrupts, data records, or progress. | |
| gclib_result | gclib_message (gclib_handle h, char *message, size_t len, int timeout) |
| Get a queued unsolicited message, or wait up to timeout ms for one to arrive. | |
| gclib_result | gclib_interrupt (gclib_handle h, gclib_interrupt_t *interrupt, int timeout) |
| Get a queued interrupt, or wait up to timeout ms for one to arrive. | |
| gclib_result | gclib_data_record (gclib_handle h, gclib_data_record_handle *data_record, int timeout) |
| Get a queued data record, or wait up to timeout ms for one to arrive. | |
Get unsolicited messages, interrupts, or data records, in blocking or callback mode.
Blocking mode is simpler to use, but has tradeoffs. If you want unsolicited data as soon as it arrives, you must block the thread to wait for it, which may not be ideal. If you want to keep the thread running, then you must periodically check for queued unsolicited data, which can add unwanted latency.
To subscribe in blocking mode, simply do not pass a callback or data pointer when you subscribe. These methods will begin queueing any unsolicited data sent by the controller.
Callback mode allows your thread to stay running while enabling immediate response to unsolicited data. When data arrives, your callback function will be invoked on a separate, dedicated thread. Due to this, callback mode can be more complicated to use if you are not familiar with thread synchronization.
To subscribe in callback mode, pass a callback function and an optional data pointer when you subscribe. When unsolicited data arrives, your callback will be invoked and passed the data pointer. Use this to allow the callback function access to any data it needs, and to allow data to persist between callbacks.
The user data pointer can be used to pass arbitrary data to the callback function. The following example uses a struct to provide the callback access to the gclib handle itself, a count to increment when messages are received, and an atomic bool to signal when the program is ready to exit. Note that the handle is safe to use concurrently, and the count doesn't need to be atomic (since it is only used in the callback thread). As soon as the tenth message is received, the program is stopped and the connection is closed from the callback method, and the main thread is signaled to exit.
| gclib_result gclib_set_interrupts | ( | gclib_handle | h, |
| gclib_interrupt_type | interrupt_mask, | ||
| gclib_axis_flags | motion_complete_axes, | ||
| gclib_digital_input_flags | digital_inputs ) |
Configure which interrupts will be generated by the controller.
By default, the controller will not generate any interrupts.
Controllers can be configured to send only an exact list of desired interrupts, to avoid unnecessary processing and network traffic.
Example usage:
See gclib_subscribe_interrupts() to enable receiving interrupts from the controller.
| h | Handle to an open connection. |
| interrupt_mask | Bitmask of desired interrupts. Use GCLIB_NO_INTERRUPTS (0) to disable interrupts. Note that User Interrupts cannot be disabled. |
| axis_mask | When GCLIB_MOTION_COMPLETE is in interrupt_mask, this axis mask controls which axes will trigger a GCLIB_MOTION_COMPLETE interrupt. interrupts. If 0, all axes will be selected. |
| digital_input_mask | When GCLIB_DIGITAL_INPUT_LOW is in interrupt_mask, this bitmask selects which digital inputs will trigger an interrupt. If 0, all digital inputs will be selected. |
| gclib_result gclib_set_data_records | ( | gclib_handle | h, |
| size_t | period_ms ) |
Configure controller data records.
By default, data records are not sent by the controller.
See gclib_subscribe_data_records() to enable receiving data records from the controller.
| h | Handle to an open connection. |
| period_ms | Period between data records. Pass 0 to disable data records. |
| gclib_result gclib_subscribe_messages | ( | gclib_handle | h, |
| void(callback)(void *user_data, const char *message) | , | ||
| void * | user_data ) |
Subscribe to unsolicited messages.
This method registers a callback to be run every time an unsolicited message is received from the controller.
When a callback is not provided, messages are instead queued for later retrieval by gclib_message().
On ethernet connections, a handle is opened for unsolicited data (if not already open) and the controller is configured to use it for messages.
| h | Handle to an open connection. |
| callback | Pass a callback function to get messages asynchronously, or NULL if you would like to use the gclib_message() blocking API. |
| user_data | An arbitrary pointer that will be passed to your callback function. Can be used to provide state that persists between callbacks. |
| gclib_result gclib_subscribe_interrupts | ( | gclib_handle | h, |
| void(callback)(void *user_data, gclib_interrupt_t interrupt) | , | ||
| void * | user_data ) |
Subscribe to interrupts.
This method registers a callback to be run every time an interrupt is received from the controller. See gclib_set_interrupts() to enable and configure controller interrupts.
When a callback is not provided, interrupts are instead queued for later retrieval by gclib_interrupt().
On ethernet connections, a handle is opened for unsolicited data (if not already open) and the controller is configured to use it for interrupts.
| h | Handle to an open connection. |
| callback | Pass a callback function to get interrupts asynchronously, or NULL if you would like to use the gclib_interrupt() blocking API. |
| user_data | An arbitrary pointer that will be passed to your callback function. Can be used to provide state that persists between callbacks. |
| gclib_result gclib_subscribe_data_records | ( | gclib_handle | h, |
| void(callback)(void *user_data, gclib_data_record_handle data_record) | , | ||
| void * | user_data ) |
Subscribe to data records.
This method registers a callback to be run every time a data record is received from the controller. See gclib_set_data_records() to enable data records and configure how often they will be generated by the controller.
When a callback is not provided, data records are instead queued for later retrieval by gclib_data_record().
On ethernet connections, a handle is opened for unsolicited data (if not already open) and the controller is configured to use it for data records.
| h | Handle to an open connection. |
| callback | Pass a callback function to get data records asynchronously, or NULL if you would like to use the gclib_data_record() blocking API. |
| user_data | An arbitrary pointer that will be passed to your callback function. Can be used to provide state that persists between callbacks. |
| gclib_result gclib_subscribe_progress | ( | gclib_handle | h, |
| void(callback)(void *user_data, size_t current, size_t max) | , | ||
| void * | user_data ) |
Subscribe to progress for gclib_set_program(), gclib_set_array(), and gclib_set_firmware().
| h | Handle to an open connection. |
| callback | A callback function that will get called anytime part of a large write is completed. |
| user_data | An arbitrary pointer that will be passed to your callback function. Can be used to provide state that persists between callbacks. |
| gclib_result gclib_unsubscribe | ( | gclib_handle | h, |
| void * | callback ) |
Unsubscribe from messages, interrupts, data records, or progress.
| h | Handle to an open connection. |
| callback | The callback function that was previously used to subscribe. If NULL, all subscriptions will be cleared. |
| gclib_result gclib_message | ( | gclib_handle | h, |
| char * | message, | ||
| size_t | len, | ||
| int | timeout ) |
Get a queued unsolicited message, or wait up to timeout ms for one to arrive.
| h | Handle to an open connection. |
| message | Output buffer for the message. |
| len | Length of output buffer. |
| timeout | Timeout value in milliseconds. If -1, wait forever. |
| gclib_result gclib_interrupt | ( | gclib_handle | h, |
| gclib_interrupt_t * | interrupt, | ||
| int | timeout ) |
Get a queued interrupt, or wait up to timeout ms for one to arrive.
| h | Handle to an open connection. |
| interrupt | Output interrupt. |
| timeout | Timeout value in milliseconds. If -1, wait forever. |
| gclib_result gclib_data_record | ( | gclib_handle | h, |
| gclib_data_record_handle * | data_record, | ||
| int | timeout ) |
Get a queued data record, or wait up to timeout ms for one to arrive.
The returned handle remains valid until the next call to gclib_data_record().
| h | Handle to an open connection. |
| data_record | Output data record handle. |
| timeout | Timeout value in milliseconds. If -1, wait forever. |