libsigrok  0.4.0
sigrok hardware access and backend library
Macros | Enumerations | Functions | Variables
modbus.c File Reference
#include <config.h>
#include <glib.h>
#include <string.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
+ Include dependency graph for modbus.c:

Go to the source code of this file.

Macros

#define LOG_PREFIX   "modbus"
 

Enumerations

enum  {
  MODBUS_READ_COILS = 0x01,
  MODBUS_READ_HOLDING_REGISTERS = 0x03,
  MODBUS_WRITE_COIL = 0x05,
  MODBUS_WRITE_MULTIPLE_REGISTERS = 0x10
}
 

Functions

SR_PRIV GSList * sr_modbus_scan (struct drv_context *drvc, GSList *options, struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus))
 Scan for Modbus devices which match a probing function. More...
 
SR_PRIV struct sr_modbus_dev_inst * modbus_dev_inst_new (const char *resource, const char *serialcomm, int modbusaddr)
 Allocate and initialize a struct for a Modbus device instance. More...
 
SR_PRIV int sr_modbus_open (struct sr_modbus_dev_inst *modbus)
 Open the specified Modbus device. More...
 
SR_PRIV int sr_modbus_source_add (struct sr_session *session, struct sr_modbus_dev_inst *modbus, int events, int timeout, sr_receive_data_callback cb, void *cb_data)
 Add an event source for a Modbus device. More...
 
SR_PRIV int sr_modbus_source_remove (struct sr_session *session, struct sr_modbus_dev_inst *modbus)
 Remove event source for a Modbus device. More...
 
SR_PRIV int sr_modbus_request (struct sr_modbus_dev_inst *modbus, uint8_t *request, int request_size)
 Send a Modbus command. More...
 
SR_PRIV int sr_modbus_reply (struct sr_modbus_dev_inst *modbus, uint8_t *reply, int reply_size)
 Receive a Modbus reply. More...
 
SR_PRIV int sr_modbus_request_reply (struct sr_modbus_dev_inst *modbus, uint8_t *request, int request_size, uint8_t *reply, int reply_size)
 Send a Modbus command and receive the corresponding reply. More...
 
SR_PRIV int sr_modbus_read_coils (struct sr_modbus_dev_inst *modbus, int address, int nb_coils, uint8_t *coils)
 Send a Modbus read coils command and receive the corresponding coils values. More...
 
SR_PRIV int sr_modbus_read_holding_registers (struct sr_modbus_dev_inst *modbus, int address, int nb_registers, uint16_t *registers)
 Send a Modbus read holding registers command and receive the corresponding registers values. More...
 
SR_PRIV int sr_modbus_write_coil (struct sr_modbus_dev_inst *modbus, int address, int value)
 Send a Modbus write coil command. More...
 
SR_PRIV int sr_modbus_write_multiple_registers (struct sr_modbus_dev_inst *modbus, int address, int nb_registers, uint16_t *registers)
 Send a Modbus write multiple registers command. More...
 
SR_PRIV int sr_modbus_close (struct sr_modbus_dev_inst *modbus)
 Close Modbus device. More...
 
SR_PRIV void sr_modbus_free (struct sr_modbus_dev_inst *modbus)
 Free Modbus device. More...
 

Variables

SR_PRIV const struct sr_modbus_dev_inst modbus_serial_rtu_dev
 

Macro Definition Documentation

#define LOG_PREFIX   "modbus"

Definition at line 26 of file modbus.c.

Enumeration Type Documentation

anonymous enum
Enumerator
MODBUS_READ_COILS 
MODBUS_READ_HOLDING_REGISTERS 
MODBUS_WRITE_COIL 
MODBUS_WRITE_MULTIPLE_REGISTERS 

Definition at line 315 of file modbus.c.

Function Documentation

SR_PRIV struct sr_modbus_dev_inst* modbus_dev_inst_new ( const char *  resource,
const char *  serialcomm,
int  modbusaddr 
)

Allocate and initialize a struct for a Modbus device instance.

Parameters
resourceThe resource description string.
serialcommAdditionnal parameters for serial port resources.
Returns
The allocated sr_modbus_dev_inst structure or NULL on failure.

Definition at line 141 of file modbus.c.

References sr_modbus_free(), and SR_OK.

+ Here is the call graph for this function:

SR_PRIV int sr_modbus_close ( struct sr_modbus_dev_inst *  modbus)

Close Modbus device.

Parameters
modbusPreviously initialized Modbus device structure.
Returns
SR_OK on success, SR_ERR on failure.

Definition at line 561 of file modbus.c.

SR_PRIV void sr_modbus_free ( struct sr_modbus_dev_inst *  modbus)

Free Modbus device.

Parameters
modbusPreviously initialized Modbus device structure.
Returns
SR_OK on success, SR_ERR on failure.

Definition at line 573 of file modbus.c.

Referenced by modbus_dev_inst_new().

+ Here is the caller graph for this function:

SR_PRIV int sr_modbus_open ( struct sr_modbus_dev_inst *  modbus)

Open the specified Modbus device.

Parameters
modbusPreviously initialized Modbus device structure.
Returns
SR_OK on success, SR_ERR on failure.

Definition at line 178 of file modbus.c.

SR_PRIV int sr_modbus_read_coils ( struct sr_modbus_dev_inst *  modbus,
int  address,
int  nb_coils,
uint8_t *  coils 
)

Send a Modbus read coils command and receive the corresponding coils values.

Parameters
modbusPreviously initialized Modbus device structure.
addressThe Modbus address of the first coil to read, or -1 to read the reply of a previouly sent read coils command.
nb_coilsThe number of coils to read.
coilsBuffer to store all the received coils values (1 bit per coil), or NULL to send the read coil command without reading the reply.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, SR_ERR_DATA upon invalid data, or SR_ERR on failure.

Definition at line 398 of file modbus.c.

References MODBUS_READ_COILS, SR_ERR_ARG, SR_ERR_DATA, sr_modbus_reply(), sr_modbus_request(), and SR_OK.

+ Here is the call graph for this function:

SR_PRIV int sr_modbus_read_holding_registers ( struct sr_modbus_dev_inst *  modbus,
int  address,
int  nb_registers,
uint16_t *  registers 
)

Send a Modbus read holding registers command and receive the corresponding registers values.

Parameters
modbusPreviously initialized Modbus device structure.
addressThe Modbus address of the first register to read, or -1 to read the reply of a previouly sent read registers command.
nb_registersThe number of registers to read.
registersBuffer to store all the received registers values, or NULL to send the read holding registers command without reading the reply.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, SR_ERR_DATA upon invalid data, or SR_ERR on failure.

Definition at line 446 of file modbus.c.

References MODBUS_READ_HOLDING_REGISTERS, SR_ERR_ARG, SR_ERR_DATA, sr_modbus_reply(), sr_modbus_request(), and SR_OK.

+ Here is the call graph for this function:

SR_PRIV int sr_modbus_reply ( struct sr_modbus_dev_inst *  modbus,
uint8_t *  reply,
int  reply_size 
)

Receive a Modbus reply.

Parameters
modbusPreviously initialized Modbus device structure.
replyBuffer to store the received Modbus reply.
reply_sizeThe size of the reply buffer.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR on failure.

Definition at line 248 of file modbus.c.

References SR_ERR, SR_ERR_ARG, and SR_OK.

Referenced by sr_modbus_read_coils(), sr_modbus_read_holding_registers(), and sr_modbus_request_reply().

+ Here is the caller graph for this function:

SR_PRIV int sr_modbus_request ( struct sr_modbus_dev_inst *  modbus,
uint8_t *  request,
int  request_size 
)

Send a Modbus command.

Parameters
modbusPreviously initialized Modbus device structure.
requestBuffer containing the Modbus command to send.
request_sizeThe size of the request buffer.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR on failure.

Definition at line 229 of file modbus.c.

References SR_ERR_ARG.

Referenced by sr_modbus_read_coils(), sr_modbus_read_holding_registers(), and sr_modbus_request_reply().

+ Here is the caller graph for this function:

SR_PRIV int sr_modbus_request_reply ( struct sr_modbus_dev_inst *  modbus,
uint8_t *  request,
int  request_size,
uint8_t *  reply,
int  reply_size 
)

Send a Modbus command and receive the corresponding reply.

Parameters
modbusPreviously initialized Modbus device structure.
requestBuffer containing the Modbus command to send.
request_sizeThe size of the request buffer.
replyBuffer to store the received Modbus reply.
reply_sizeThe size of the reply buffer.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR on failure.

Definition at line 305 of file modbus.c.

References sr_modbus_reply(), sr_modbus_request(), and SR_OK.

Referenced by sr_modbus_write_coil(), and sr_modbus_write_multiple_registers().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

SR_PRIV GSList* sr_modbus_scan ( struct drv_context *  drvc,
GSList *  options,
struct sr_dev_inst *(*)(struct sr_modbus_dev_inst *modbus)  probe_device 
)

Scan for Modbus devices which match a probing function.

Parameters
drvcThe driver context doing the scan.
optionsThe scan options to find devies.
probe_deviceThe callback function that will be called for each found device to validate whether this device matches what we are scanning for.
Returns
A list of the devices found or NULL if no devices were found.

Definition at line 74 of file modbus.c.

References sr_config::data, sr_config::key, SR_CONF_CONN, SR_CONF_MODBUSADDR, and SR_CONF_SERIALCOMM.

SR_PRIV int sr_modbus_source_add ( struct sr_session session,
struct sr_modbus_dev_inst *  modbus,
int  events,
int  timeout,
sr_receive_data_callback  cb,
void *  cb_data 
)

Add an event source for a Modbus device.

Parameters
sessionThe session to add the event source to.
modbusPreviously initialized Modbus device structure.
eventsEvents to check for.
timeoutMax time to wait before the callback is called, ignored if 0.
cbCallback function to add. Must not be NULL.
cb_dataData for the callback function. Can be NULL.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR_MALLOC upon memory allocation errors.

Definition at line 196 of file modbus.c.

SR_PRIV int sr_modbus_source_remove ( struct sr_session session,
struct sr_modbus_dev_inst *  modbus 
)

Remove event source for a Modbus device.

Parameters
sessionThe session to remove the event source from.
modbusPreviously initialized Modbus device structure.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, or SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon internal errors.

Definition at line 213 of file modbus.c.

SR_PRIV int sr_modbus_write_coil ( struct sr_modbus_dev_inst *  modbus,
int  address,
int  value 
)

Send a Modbus write coil command.

Parameters
modbusPreviously initialized Modbus device structure.
addressThe Modbus address of the coil to write.
valueThe new value to assign to this coil.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, SR_ERR_DATA upon invalid data, or SR_ERR on failure.

Definition at line 490 of file modbus.c.

References MODBUS_WRITE_COIL, SR_ERR_ARG, SR_ERR_DATA, sr_modbus_request_reply(), and SR_OK.

+ Here is the call graph for this function:

SR_PRIV int sr_modbus_write_multiple_registers ( struct sr_modbus_dev_inst *  modbus,
int  address,
int  nb_registers,
uint16_t *  registers 
)

Send a Modbus write multiple registers command.

Parameters
modbusPreviously initialized Modbus device structure.
addressThe Modbus address of the first register to write.
nb_registersThe number of registers to write.
registersBuffer holding all the registers values to write.
Returns
SR_OK upon success, SR_ERR_ARG upon invalid arguments, SR_ERR_DATA upon invalid data, or SR_ERR on failure.

Definition at line 526 of file modbus.c.

References MODBUS_WRITE_MULTIPLE_REGISTERS, SR_ERR_ARG, SR_ERR_DATA, sr_modbus_request_reply(), and SR_OK.

+ Here is the call graph for this function:

Variable Documentation

SR_PRIV const struct sr_modbus_dev_inst modbus_serial_rtu_dev

Definition at line 194 of file modbus_serial_rtu.c.