espressif / esp-modbus

ESP-Modbus - the officially suppported library for Modbus protocol (serial RS485 + TCP over WiFi or Ethernet).
Apache License 2.0
85 stars 46 forks source link

Invalid register reading after error "E (92525) MB_PORT_COMMON: vMBMasterRunResRelease(195): Resource release failure" (IDFGH-10168) #30

Closed kotyara12 closed 1 year ago

kotyara12 commented 1 year ago
  mb_param_request_t _request = {
     .slave_addr = _address,
     .command = cmd,
     .reg_start=reg,
     .reg_size = 1
   };
   return mbc_master_send_request(&_request, (void*)value);

The following code reads only those registers that are preconfigured:

esp_err_t err = ESP_OK;
  uint16_t bufH, bufT, bufC, bufP = 0;

  // Humidity (moisture)
  if ((_item2) && (err == ESP_OK)) {
    err = readModbusRegister(FUNCTION_CODE_STATUS_READ, REG_STATUS_HUMIDITY, &bufH);
  };
  // Temperature
  if ((_item1) && (err == ESP_OK)) {
    err = readModbusRegister(FUNCTION_CODE_STATUS_READ, REG_STATUS_TEMPERATURE, &bufT);
  };
  // Conductivity
  if ((_item3) && (err == ESP_OK)) {
    err = readModbusRegister(FUNCTION_CODE_STATUS_READ, REG_STATUS_CONDUCTIVITY, &bufC);
  };
  // PH
  if ((_item4) && (err == ESP_OK)) {
    err = readModbusRegister(FUNCTION_CODE_STATUS_READ, REG_STATUS_PH, &bufP);
  };

For a while everything works fine! But after a random time, an error is thrown:

E (92525) MB_PORT_COMMON: vMBMasterRunResRelease(195): Resource release failure.

After that, a mess begins on the RS485 bus - the registers can start to "shift", for example, I request temperature, and PH or humidity comes through the bus. And exactly the same behavior is observed on other bus devices.

The boss will rip my head off

PS: Yes, I saw in the examples that mbc_master_send_request is not directly called anywhere, but a scheme of pre-configured parameters is used. But it's kind of a shame. To read a couple of registers to create a super-bulky structure....

alisitsyn commented 1 year ago

Hi @kotyara12 ,

The freemodbus in esp-idf v4.4.3 component is not updated to recent version. In order to check the issue could you try to update your modbus component in the freemodbus folder? This component folder must be replaced in the following path: C:\Users\USERNAME\.platformio\packages\framework-espidf\components\freemodbus. Please update the whole folder to the component files located here. Let me know if you have issues with this. Thanks.

kotyara12 commented 1 year ago

Hi, @alisitsyn

Thanks! I've been browsing through the issues here and came across a suggestion to add

set(EXCLUDE_COMPONENTS freemodbus)

I guess this should update espmodbus to an up to date version? I'll check a little later.

Alas, I can't use ESP-IDF 5.0.1 yet, because the problem with a crash when calling localtime_r has not yet been fixed there

alisitsyn commented 1 year ago

@kotyara12 ,

Yes, thank you for update. Yes, you can use the latest version of v1.0.10 and add the above line to the cmake file to exclude the freemodbus component and use the latest esp-modbus instead. In most cases this allows to fix this issue. However, the latest component may have some issues in some rare cases when your application intensively uses WIFI and network services. In order to check this approach you can update the component to experimental one which I included above.

I guess this should update espmodbus to an up to date version? I'll check a little later.

You also need the manifest file in your project main component folder:

idf_component.yml:

dependencies:
  idf: ">=4.1"
  espressif/esp-modbus:
    version: "^1.0"
kotyara12 commented 1 year ago

@alisitsyn

Thank you very much, this seems to have helped. At least the device works stably for several days, without errors in data transmission over the bus.

alisitsyn commented 1 year ago

@kotyara12 ,

Thank you for update. It is good that it works for your project. This update is temporary and the event tracking will be updated in new revision of esp-modbus. Let me know if you still have any issues.