epsilonrt / modbus-arduino

A library that allows your Arduino to communicate via Modbus protocol, acting as a slave. Application layer library (OSI 7), used by all implementations over serial line and TCP/IP.
https://epsilonrt.github.io/modbus-arduino/
BSD 3-Clause "New" or "Revised" License
9 stars 1 forks source link

ESP32C3 hang when Coil read 8n's coil #2

Closed epsilonrt closed 1 year ago

epsilonrt commented 1 year ago

Whole Modbus program hang when read 8 or 16 or 8*n coils using Read Coil Status (FC=01). However it is normal when read 1-7 ,9-15 , 17-23 , etc. The loop is still running. Seem something overflow in the program.

2023/04/08 23:17:24 >>> 0E 01 00 00 00 08 3D 33
2023/04/08 23:17:24 < 0E 01 01 2A D3 43 43 4F 52 52 55 50 54 20 48 45 41 50 3A 20 42 61 64 20 74 61 69 6C 20 61 74 20 30 78 33 66 63 39 31 37 34 37 2E 20 45 78 70 65 63 74 65 64 20 30 78 62 61 61 64 35 36 37 38 20 67 6F 74 20 30 78 62 61 61 64 35 36 66 38 0D 0A
epsilonrt commented 1 year ago
#include <Arduino.h>
#include <ModbusSerial.h>

ModbusSerial mb (Serial, 10);

const uint8_t NbOfCoil = 19;
const uint16_t FirstCoil = 20;
const uint32_t CoilValues = 0x056BCD;

void setup() {

  Serial.begin (38400);
  mb.config (38400);

  for (uint8_t i = 0; i < NbOfCoil; i++) {

    mb.addCoil (FirstCoil -1 + i, (CoilValues & (1 << i)) != 0);
  }
}

void loop() {

  mb.task();
}

Reading test with mbpoll :

C:\Users\pasca>mbpoll -a10 -b38400 -t0 -r20 -c19 -1 -v com6
mbpoll 0.1-19 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2015 Pascal JEAN aka epsilonRT
All rights reserved.
This software is governed by the CeCILL license <http://www.cecill.info>

Opening com6 at 38400 bauds (E, 8, 1)
Protocol configuration: Modbus RTU
Slave configuration...: address = [10]
                        start reference = 20, count = 19
Communication.........: com6, 38400-8E1
                        t/o 1.00 s, poll rate 1000 ms
Data type.............: discrete output (coil)

-- Polling slave 10...
[0A][01][00][13][00][13][8D][79]
Waiting for a confirmation...
<0A><01><03><4D><EB><05><23><D1>
[20]:   1
[21]:   0
[22]:   1
[23]:   1
[24]:   0
[25]:   0
[26]:   1
[27]:   0
[28]:   1
[29]:   1
[30]:   0
[31]:   1
[32]:   0
[33]:   1
[34]:   1
[35]:   1
[36]:   1
[37]:   0
[38]:   1

The bug is confirmed, the origin of the problem is identical to #1: bad decrementation of the numregs variable