* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <config.h>
#include <glib.h>
#include <string.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
#define LOG_PREFIX "modbus"
SR_PRIV extern const struct sr_modbus_dev_inst modbus_serial_rtu_dev;
static const struct sr_modbus_dev_inst *modbus_devs[] = {
-#ifdef HAVE_LIBSERIALPORT
- &modbus_serial_rtu_dev, /* Must be last as it matches any resource. */
+#ifdef HAVE_SERIAL_COMM
+ &modbus_serial_rtu_dev, /* Must be last as it matches any resource. */
#endif
};
+static const unsigned int modbus_devs_size = ARRAY_SIZE(modbus_devs);
+
static struct sr_dev_inst *sr_modbus_scan_resource(const char *resource,
const char *serialcomm, int modbusaddr,
struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus))
return NULL;
if (sr_modbus_open(modbus) != SR_OK) {
- sr_info("Couldn't open MODBUS device.");
+ sr_info("Couldn't open Modbus device.");
sr_modbus_free(modbus);
return NULL;
};
- if ((sdi = probe_device(modbus)))
- return sdi;
+ sdi = probe_device(modbus);
sr_modbus_close(modbus);
- sr_modbus_free(modbus);
- return NULL;
+ if (!sdi)
+ sr_modbus_free(modbus);
+
+ return sdi;
}
/**
- * Scan for MODBUS devices which match a probing function.
+ * Scan for Modbus devices which match a probing function.
*
* @param drvc The driver context doing the scan.
* @param options The scan options to find devies.
}
devices = NULL;
- for (i = 0; i < ARRAY_SIZE(modbus_devs); i++) {
+ for (i = 0; i < modbus_devs_size; i++) {
if ((resource && strcmp(resource, modbus_devs[i]->prefix))
|| !modbus_devs[i]->scan)
continue;
}
/**
- * Allocate and initialize a struct for a MODBUS device instance.
+ * Allocate and initialize a struct for a Modbus device instance.
*
* @param resource The resource description string.
* @param serialcomm Additionnal parameters for serial port resources.
gchar **params;
unsigned int i;
- for (i = 0; i < ARRAY_SIZE(modbus_devs); i++) {
+ for (i = 0; i < modbus_devs_size; i++) {
modbus_dev = modbus_devs[i];
if (!strncmp(resource, modbus_dev->prefix, strlen(modbus_dev->prefix))) {
sr_dbg("Opening %s device %s.", modbus_dev->name, resource);
}
/**
- * Open the specified MODBUS device.
+ * Open the specified Modbus device.
*
- * @param modbus Previously initialized MODBUS device structure.
+ * @param modbus Previously initialized Modbus device structure.
*
* @return SR_OK on success, SR_ERR on failure.
*/
}
/**
- * Add an event source for a MODBUS device.
+ * Add an event source for a Modbus device.
*
* @param session The session to add the event source to.
- * @param modbus Previously initialized MODBUS device structure.
+ * @param modbus Previously initialized Modbus device structure.
* @param events Events to check for.
* @param timeout Max time to wait before the callback is called, ignored if 0.
* @param cb Callback function to add. Must not be NULL.
}
/**
- * Remove event source for a MODBUS device.
+ * Remove event source for a Modbus device.
*
* @param session The session to remove the event source from.
- * @param modbus Previously initialized MODBUS device structure.
+ * @param modbus Previously initialized Modbus device structure.
*
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
* SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
}
/**
- * Send a MODBUS command.
+ * Send a Modbus command.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param request Buffer containing the MODBUS command to send.
+ * @param modbus Previously initialized Modbus device structure.
+ * @param request Buffer containing the Modbus command to send.
* @param request_size The size of the request buffer.
*
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
}
/**
- * Receive a MODBUS reply.
+ * Receive a Modbus reply.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param reply Buffer to store the received MODBUS reply.
+ * @param modbus Previously initialized Modbus device structure.
+ * @param reply Buffer to store the received Modbus reply.
* @param reply_size The size of the reply buffer.
*
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
while (reply_size > 0) {
len = modbus->read_data(modbus->priv, reply, reply_size);
if (len < 0) {
- sr_err("Incompletely read MODBUS response.");
+ sr_err("Incompletely read Modbus response.");
return SR_ERR;
} else if (len > 0) {
laststart = g_get_monotonic_time();
reply_size -= len;
elapsed_ms = (g_get_monotonic_time() - laststart) / 1000;
if (elapsed_ms >= modbus->read_timeout_ms) {
- sr_err("Timed out waiting for MODBUS response.");
+ sr_err("Timed out waiting for Modbus response.");
return SR_ERR;
}
}
}
/**
- * Send a MODBUS command and receive the corresponding reply.
+ * Send a Modbus command and receive the corresponding reply.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param request Buffer containing the MODBUS command to send.
+ * @param modbus Previously initialized Modbus device structure.
+ * @param request Buffer containing the Modbus command to send.
* @param request_size The size of the request buffer.
- * @param reply Buffer to store the received MODBUS reply.
+ * @param reply Buffer to store the received Modbus reply.
* @param reply_size The size of the reply buffer.
*
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
}
/**
- * Send a MODBUS read coils command and receive the corresponding coils values.
+ * Send a Modbus read coils command and receive the corresponding coils values.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param address The MODBUS address of the first coil to read, or -1 to read
+ * @param modbus Previously initialized Modbus device structure.
+ * @param address The Modbus address of the first coil to read, or -1 to read
* the reply of a previouly sent read coils command.
* @param nb_coils The number of coils to read.
* @param coils Buffer to store all the received coils values (1 bit per coil),
}
/**
- * Send a MODBUS read holding registers command and receive the corresponding
+ * Send a Modbus read holding registers command and receive the corresponding
* registers values.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param address The MODBUS address of the first register to read, or -1 to
+ * @param modbus Previously initialized Modbus device structure.
+ * @param address The Modbus address of the first register to read, or -1 to
* read the reply of a previouly sent read registers command.
* @param nb_registers The number of registers to read.
* @param registers Buffer to store all the received registers values,
}
/**
- * Send a MODBUS write coil command.
+ * Send a Modbus write coil command.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param address The MODBUS address of the coil to write.
+ * @param modbus Previously initialized Modbus device structure.
+ * @param address The Modbus address of the coil to write.
* @param value The new value to assign to this coil.
*
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
}
/**
- * Send a MODBUS write multiple registers command.
+ * Send a Modbus write multiple registers command.
*
- * @param modbus Previously initialized MODBUS device structure.
- * @param address The MODBUS address of the first register to write.
+ * @param modbus Previously initialized Modbus device structure.
+ * @param address The Modbus address of the first register to write.
* @param nb_registers The number of registers to write.
* @param registers Buffer holding all the registers values to write.
*
}
/**
- * Close MODBUS device.
+ * Close Modbus device.
*
- * @param modbus Previously initialized MODBUS device structure.
+ * @param modbus Previously initialized Modbus device structure.
*
* @return SR_OK on success, SR_ERR on failure.
*/
}
/**
- * Free MODBUS device.
+ * Free Modbus device.
*
- * @param modbus Previously initialized MODBUS device structure.
+ * @param modbus Previously initialized Modbus device structure.
*
* @return SR_OK on success, SR_ERR on failure.
*/