2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Aurelien Jacobs <aurel@gnuage.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
26 #define LOG_PREFIX "modbus"
28 static const struct sr_modbus_dev_inst *modbus_devs[] = {
31 static struct sr_dev_inst *sr_modbus_scan_resource(const char *resource,
32 const char *serialcomm, int modbusaddr,
33 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus))
35 struct sr_modbus_dev_inst *modbus;
36 struct sr_dev_inst *sdi;
38 if (!(modbus = modbus_dev_inst_new(resource, serialcomm, modbusaddr)))
41 if (sr_modbus_open(modbus) != SR_OK) {
42 sr_info("Couldn't open MODBUS device.");
43 sr_modbus_free(modbus);
47 if ((sdi = probe_device(modbus)))
50 sr_modbus_close(modbus);
51 sr_modbus_free(modbus);
56 * Scan for MODBUS devices which match a probing function.
58 * @param drvc the driver context doing the scan.
59 * @param options the scan options to find devies.
60 * @param probe_device the callback function that will be called for each
61 * found devices to validate wheter this device matches
62 * what we are scanning for.
64 * @return a list of the devices found or NULL if no device found.
66 SR_PRIV GSList *sr_modbus_scan(struct drv_context *drvc, GSList *options,
67 struct sr_dev_inst *(*probe_device)(struct sr_modbus_dev_inst *modbus))
69 GSList *resources, *l, *devices;
70 struct sr_dev_inst *sdi;
71 const char *resource = NULL;
72 const char *serialcomm = NULL;
77 for (l = options; l; l = l->next) {
78 struct sr_config *src = l->data;
81 resource = g_variant_get_string(src->data, NULL);
83 case SR_CONF_SERIALCOMM:
84 serialcomm = g_variant_get_string(src->data, NULL);
86 case SR_CONF_MODBUSADDR:
87 modbusaddr = g_variant_get_uint64(src->data);
93 for (i = 0; i < ARRAY_SIZE(modbus_devs); i++) {
94 if ((resource && strcmp(resource, modbus_devs[i]->prefix))
95 || !modbus_devs[i]->scan)
97 resources = modbus_devs[i]->scan(modbusaddr);
98 for (l = resources; l; l = l->next) {
99 res = g_strsplit(l->data, ":", 2);
100 if (res[0] && (sdi = sr_modbus_scan_resource(res[0],
101 serialcomm ? serialcomm : res[1],
102 modbusaddr, probe_device))) {
103 devices = g_slist_append(devices, sdi);
104 sdi->connection_id = g_strdup(l->data);
108 g_slist_free_full(resources, g_free);
111 if (!devices && resource) {
112 sdi = sr_modbus_scan_resource(resource, serialcomm, modbusaddr,
115 devices = g_slist_append(NULL, sdi);
118 /* Tack a copy of the newly found devices onto the driver list. */
120 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
126 * Allocate and initialize struct for a MODBUS device instance.
128 * @param resource the resource description string.
129 * @param serialcomm additionnal parameters for serial port resources.
131 * @return the allocated sr_modbus_dev_inst structure or NULL on failure.
133 SR_PRIV struct sr_modbus_dev_inst *modbus_dev_inst_new(const char *resource,
134 const char *serialcomm, int modbusaddr)
136 struct sr_modbus_dev_inst *modbus = NULL;
137 const struct sr_modbus_dev_inst *modbus_dev;
141 for (i = 0; i < ARRAY_SIZE(modbus_devs); i++) {
142 modbus_dev = modbus_devs[i];
143 if (!strncmp(resource, modbus_dev->prefix, strlen(modbus_dev->prefix))) {
144 sr_dbg("Opening %s device %s.", modbus_dev->name, resource);
145 modbus = g_malloc(sizeof(*modbus));
146 *modbus = *modbus_dev;
147 modbus->priv = g_malloc0(modbus->priv_size);
148 modbus->read_timeout_ms = 1000;
149 params = g_strsplit(resource, "/", 0);
150 if (modbus->dev_inst_new(modbus->priv, resource,
151 params, serialcomm, modbusaddr) != SR_OK) {
152 sr_modbus_free(modbus);
164 * Open MODBUS device.
166 * @param modbus Previously initialized MODBUS device structure.
168 * @return SR_OK on success, SR_ERR on failure.
170 SR_PRIV int sr_modbus_open(struct sr_modbus_dev_inst *modbus)
172 return modbus->open(modbus->priv);
176 * Add an event source for an MODBUS device.
178 * @param session The session to add the event source to.
179 * @param modbus Previously initialized MODBUS device structure.
180 * @param events Events to check for.
181 * @param timeout Max time to wait before the callback is called, ignored if 0.
182 * @param cb Callback function to add. Must not be NULL.
183 * @param cb_data Data for the callback function. Can be NULL.
185 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
186 * SR_ERR_MALLOC upon memory allocation errors.
188 SR_PRIV int sr_modbus_source_add(struct sr_session *session,
189 struct sr_modbus_dev_inst *modbus, int events, int timeout,
190 sr_receive_data_callback cb, void *cb_data)
192 return modbus->source_add(session, modbus->priv, events, timeout, cb, cb_data);
196 * Remove event source for an MODBUS device.
198 * @param session The session to remove the event source from.
199 * @param modbus Previously initialized MODBUS device structure.
201 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
202 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
205 SR_PRIV int sr_modbus_source_remove(struct sr_session *session,
206 struct sr_modbus_dev_inst *modbus)
208 return modbus->source_remove(session, modbus->priv);
212 * Send a MODBUS command.
214 * @param modbus Previously initialized MODBUS device structure.
215 * @param request buffer containing the MODBUS command to send.
216 * @param request_size the size of the request buffer.
218 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
221 SR_PRIV int sr_modbus_request(struct sr_modbus_dev_inst *modbus,
222 uint8_t *request, int request_size)
224 if (!request || request_size < 1)
227 return modbus->send(modbus->priv, request, request_size);
231 * Receive a MODBUS reply.
233 * @param modbus Previously initialized MODBUS device structure.
234 * @param reply buffer to store the received MODBUS reply.
235 * @param reply_size the size of the reply buffer.
237 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
240 SR_PRIV int sr_modbus_reply(struct sr_modbus_dev_inst *modbus,
241 uint8_t *reply, int reply_size)
245 unsigned int elapsed_ms;
247 if (!reply || reply_size < 2)
250 laststart = g_get_monotonic_time();
252 ret = modbus->read_begin(modbus->priv, reply);
261 while (reply_size > 0) {
262 len = modbus->read_data(modbus->priv, reply, reply_size);
264 sr_err("Incompletely read MODBUS response.");
266 } else if (len > 0) {
267 laststart = g_get_monotonic_time();
271 elapsed_ms = (g_get_monotonic_time() - laststart) / 1000;
272 if (elapsed_ms >= modbus->read_timeout_ms) {
273 sr_err("Timed out waiting for MODBUS response.");
278 ret = modbus->read_end(modbus->priv);
286 * Send a MODBUS command and receive the corresponding reply.
288 * @param modbus Previously initialized MODBUS device structure.
289 * @param request buffer containing the MODBUS command to send.
290 * @param request_size the size of the request buffer.
291 * @param reply buffer to store the received MODBUS reply.
292 * @param reply_size the size of the reply buffer.
294 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
297 SR_PRIV int sr_modbus_request_reply(struct sr_modbus_dev_inst *modbus,
298 uint8_t *request, int request_size, uint8_t *reply, int reply_size)
301 ret = sr_modbus_request(modbus, request, request_size);
304 return sr_modbus_reply(modbus, reply, reply_size);
308 MODBUS_READ_COILS = 0x01,
309 MODBUS_READ_HOLDING_REGISTERS = 0x03,
310 MODBUS_WRITE_COIL = 0x05,
311 MODBUS_WRITE_MULTIPLE_REGISTERS = 0x10,
314 static int sr_modbus_error_check(const uint8_t *reply)
316 const char *function = "UNKNOWN";
317 const char *error = NULL;
320 if (!(reply[0] & 0x80))
323 switch (reply[0] & ~0x80) {
324 case MODBUS_READ_COILS:
325 function = "MODBUS_READ_COILS"; break;
326 case MODBUS_READ_HOLDING_REGISTERS:
327 function = "READ_HOLDING_REGISTERS"; break;
328 case MODBUS_WRITE_COIL:
329 function = "WRITE_COIL"; break;
330 case MODBUS_WRITE_MULTIPLE_REGISTERS:
331 function = "WRITE_MULTIPLE_REGISTERS"; break;
336 error = "ILLEGAL FUNCTION"; break;
338 error = "ILLEGAL DATA ADDRESS"; break;
340 error = "ILLEGAL DATA VALUE"; break;
342 error = "SLAVE DEVICE FAILURE"; break;
344 error = "ACKNOWLEDGE"; break;
346 error = "SLAVE DEVICE BUSY"; break;
348 error = "MEMORY PARITY ERROR"; break;
350 error = "GATEWAY PATH UNAVAILABLE"; break;
352 error = "GATEWAY TARGET DEVICE FAILED TO RESPOND"; break;
355 snprintf(buf, sizeof(buf), "0x%X", reply[1]);
359 sr_err("%s error executing %s function.", error, function);
364 * Send a MODBUS read coils command and receive the corresponding coils values.
366 * @param modbus Previously initialized MODBUS device structure.
367 * @param address the MODBUS address of the first coil to read,
368 * or -1 to read the reply of a previouly sent
369 * read coils command.
370 * @param nb_coils the number of coils to read.
371 * @param coils buffer to store all the received coils values (1 bit per coil),
372 * or NULL to send the read coil command without reading the reply.
374 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
375 * SR_ERR_DATA upon invalid data, or SR_ERR on failure.
377 SR_PRIV int sr_modbus_read_coils(struct sr_modbus_dev_inst *modbus,
378 int address, int nb_coils, uint8_t *coils)
380 uint8_t request[5], reply[2 + (nb_coils + 7) / 8];
383 if (address < -1 || address > 0xFFFF || nb_coils < 1 || nb_coils > 2000)
386 W8 (request+0, MODBUS_READ_COILS);
387 WB16(request+1, address);
388 WB16(request+3, nb_coils);
391 ret = sr_modbus_request(modbus, request, sizeof(request));
397 ret = sr_modbus_reply(modbus, reply, sizeof(reply));
400 if (sr_modbus_error_check(reply))
402 if (reply[0] != request[0] || R8(reply+1) != (uint8_t)((nb_coils+7)/8))
404 memcpy(coils, reply+2, (nb_coils+7)/8);
411 * Send a MODBUS read holding registers command and receive the corresponding
414 * @param modbus Previously initialized MODBUS device structure.
415 * @param address the MODBUS address of the first register to read,
416 * or -1 to read the reply of a previouly sent
417 * read registers command.
418 * @param nb_registers the number of registers to read.
419 * @param registers buffer to store all the received registers values,
420 * or NULL to send the read holding registers command
421 * without reading the reply.
423 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
424 * SR_ERR_DATA upon invalid data, or SR_ERR on failure.
426 SR_PRIV int sr_modbus_read_holding_registers(struct sr_modbus_dev_inst *modbus,
427 int address, int nb_registers, uint16_t *registers)
429 uint8_t request[5], reply[2 + 2*nb_registers];
432 if (address < -1 || address > 0xFFFF
433 || nb_registers < 1 || nb_registers > 125)
436 W8 (request+0, MODBUS_READ_HOLDING_REGISTERS);
437 WB16(request+1, address);
438 WB16(request+3, nb_registers);
441 ret = sr_modbus_request(modbus, request, sizeof(request));
447 ret = sr_modbus_reply(modbus, reply, sizeof(reply));
450 if (sr_modbus_error_check(reply))
452 if (reply[0] != request[0] || R8(reply+1) != (uint8_t)(2*nb_registers))
454 memcpy(registers, reply+2, 2*nb_registers);
461 * Send a MODBUS write coil command.
463 * @param modbus Previously initialized MODBUS device structure.
464 * @param address the MODBUS address of the coil to write.
465 * @param value the new value to assign to this coil.
467 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
468 * SR_ERR_DATA upon invalid data, or SR_ERR on failure.
470 SR_PRIV int sr_modbus_write_coil(struct sr_modbus_dev_inst *modbus,
471 int address, int value)
473 uint8_t request[5], reply[5];
476 if (address < 0 || address > 0xFFFF)
479 W8 (request+0, MODBUS_WRITE_COIL);
480 WB16(request+1, address);
481 WB16(request+3, value ? 0xFF00 : 0);
483 ret = sr_modbus_request_reply(modbus, request, sizeof(request),
484 reply , sizeof(reply));
487 if (sr_modbus_error_check(reply))
489 if (memcmp(request, reply, sizeof(reply)))
495 * Send a MODBUS write multiple registers command.
497 * @param modbus Previously initialized MODBUS device structure.
498 * @param address the MODBUS address of the first register to write.
499 * @param nb_registers the number of registers to write.
500 * @param registers buffer holding all the registers values to write.
502 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
503 * SR_ERR_DATA upon invalid data, or SR_ERR on failure.
505 SR_PRIV int sr_modbus_write_multiple_registers(struct sr_modbus_dev_inst*modbus,
506 int address, int nb_registers, uint16_t *registers)
508 uint8_t request[6+2*nb_registers], reply[5];
511 if (address < 0 || address > 0xFFFF
512 || nb_registers < 1 || nb_registers > 123 || !registers)
515 W8 (request+0, MODBUS_WRITE_MULTIPLE_REGISTERS);
516 WB16(request+1, address);
517 WB16(request+3, nb_registers);
518 W8 (request+5, 2*nb_registers);
519 memcpy(request+6, registers, 2*nb_registers);
521 ret = sr_modbus_request_reply(modbus, request, sizeof(request),
522 reply , sizeof(reply));
525 if (sr_modbus_error_check(reply))
527 if (memcmp(request, reply, sizeof(reply)))
533 * Close MODBUS device.
535 * @param modbus Previously initialized MODBUS device structure.
537 * @return SR_OK on success, SR_ERR on failure.
539 SR_PRIV int sr_modbus_close(struct sr_modbus_dev_inst *modbus)
541 return modbus->close(modbus->priv);
545 * Free MODBUS device.
547 * @param modbus Previously initialized MODBUS device structure.
549 * @return SR_OK on success, SR_ERR on failure.
551 SR_PRIV void sr_modbus_free(struct sr_modbus_dev_inst *modbus)
553 modbus->free(modbus->priv);
554 g_free(modbus->priv);