Modbus lib can not drive

Hi,All from mgos newbie.

I have trouble with mgos_modbus.h

This is my code. follow example in lib.

#include "mgos_modbus.h"

static void print_buffer(struct mbuf buffer, uint8_t status) {
  char str[1024];
  int length = 0;
  for (int i = 0; i < buffer.len && i < sizeof(str) / 3; i++) {
    length += sprintf(str + length, "%.2x ", buffer.buf[i]);
  }
  if (status == RESP_SUCCESS) {
    LOG(LL_INFO, ("%f - VALID RESPONSE, Status: %d, Buffer: %.*s", mgos_uptime(), status, length, str));
  } else {
    LOG(LL_INFO, ("%f - Invalid response, Status: %d, Buffer: %.*s", mgos_uptime(), status, length, str));
  }
}

void mb_read_response_test(uint8_t status, struct mb_request_info mb_ri, struct mbuf response, void* param) {
  print_buffer(response, status);
}

// This is Loop() *********
static void timer_cb(void *arg) {

  mb_write_single_register(1, 0, 1, mb_read_response_test, NULL);
  mgos_msleep(1000);
  mb_write_single_register(1, 0, 2, mb_read_response_test, NULL);
  mgos_msleep(1000);
  mb_write_single_register(1, 0, 4, mb_read_response_test, NULL);
  mgos_msleep(1000);
  mb_write_single_register(1, 0, 8, mb_read_response_test, NULL);
  mgos_msleep(1000);
  mb_write_single_register(1, 0, 16, mb_read_response_test, NULL);
  mgos_msleep(1000);
  // LOG(LL_INFO,
  //     ("%s uptime: %.2lf, RAM: %lu, %lu free", (s_tick_tock ? "Tick" : "Tock"),
  //      mgos_uptime(), (unsigned long) mgos_get_heap_size(),
  //      (unsigned long) mgos_get_free_heap_size()));
  // s_tick_tock = !s_tick_tock;
  (void) arg;
}

// This is setup() ****
enum mgos_app_init_result mgos_app_init(void) {

   if (!mgos_modbus_connect()) {
    LOG(LL_INFO, ("Unable to connect MODBUS"));
  }
  /* Simple repeating timer */
  mgos_set_timer(6000, MGOS_TIMER_REPEAT, timer_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

mos.yml

  - ["modbus.enable", true]
  - ["modbus.timeout", 0]
  - ["modbus.uart_no", 1]
  - ["modbus.uart_tx_pin", 18]
  - ["modbus.uart_rx_pin", 19]
  - ["modbus.tx_en_enable", true]
  - ["modbus.tx_en_gpio", 2]
  - ["modbus.tx_en_gpio_val", true]
  - ["modbus.baudrate", 9600]
  - ["modbus.parity", 0]
  - ["modbus.stop_bits", 1]

Log info

[Dec  3 15:19:45.949] mgos_modbus.c:421       Write Single Register, Address: 00
[Dec  3 15:19:45.949] mgos_modbus.c:263       SlaveID: 01, Function: 06 - Initialize Modbus
[Dec  3 15:19:45.949] mgos_modbus.c:323       SlaveID: 01, Function: 06 - Transmit Buffer 8
[Dec  3 15:19:45.949] mgos_modbus.c:73        SlaveID: 01, Function: 06 - Buffer: 01 06 00 00 00 01 48 0a 
[Dec  3 15:19:45.949] mgos_modbus.c:334       SlaveID: 01, Function: 06 - Modbus Transaction Start
[Dec  3 15:19:46.982] mgos_modbus.c:421       Write Single Register, Address: 00
[Dec  3 15:19:47.978] mgos_modbus.c:421       Write Single Register, Address: 00
[Dec  3 15:19:48.992] mgos_modbus.c:421       Write Single Register, Address: 00
[Dec  3 15:19:50.005] mgos_modbus.c:421       Write Single Register, Address: 00
[Dec  3 15:19:51.019] mgos_mongoose.c:66      New heap free LWM: 173468
[Dec  3 15:19:51.019] mgos_rpc_channel_ws:209 0x3ffc2010 TCP connected
[Dec  3 15:19:51.019] #####Drive Relay10/26 06:41:18.371 Current time: 6:41
[Dec  3 15:19:51.784] mgos_modbus.c:227       SlaveID: 01, Function: 06 - Modbus response received 8
[Dec  3 15:19:51.784] mgos_modbus.c:73        SlaveID: 01, Function: 06 - Buffer: 01 06 00 00 00 01 48 0a 

My Question is:
Why in 1 loop cycle. Modbus can drive just only one command. How could I drive a several command in 1 loop cycle. Or I have to set any config?

Thank you for reply. And sorry for my english. :smiley:

According to your log, a response from the first call to mb_write_single_register is received after ~6 seconds.
The other requests can’t be processed until the first one is completed. You should check the return value of mb_write_single_register.
Check the source code.

So,I have to send command again after modbus response.Right?

The library you mention does not belong to Mongoose-OS, you should ask the one who wrote it for help
There is an example on the same repo you took this from, run the example and ask the author if you can’t make it run.

1 Like

Thank you for your reply.
Do you have other library or official modbus mongoose os library? Recommend me please.

@1115 The library you use has been added to the Mongoose OS libraries some time ago.