Skip to content

Modbus Diagnostic Clear Counters Not Resetting

DodaTech Updated 2026-06-26 2 min read

In this tutorial, you'll learn about Modbus Diagnostic Clear Counters Not Resetting. We cover key concepts, practical examples, and best practices.

The Problem

Modbus FC 08 sub-function 01 (Clear Counters) does not reset diagnostic counters.

Quick Fix

Wrong

// Sending clear counters command
mb.diagnostic(1, 0x0001, 0x0000);  // Sub-function 01 = Clear Counters```

Diagnostic response echoes the request but counters are not cleared.


### Right

```cpp
#include <ModbusRtu.h>

Modbus master(1, Serial, 2);

void printDiagnostics(uint16_t *diag) {
  Serial.println("=== Modbus Diagnostics ===");
  Serial.print("Bus Message Count:   "); Serial.println(diag[0]);
  Serial.print("Bus CRC Error Count: "); Serial.println(diag[1]);
  Serial.print("Bus Exception Count: "); Serial.println(diag[2]);
  Serial.print("Slave Message Count: "); Serial.println(diag[3]);
  Serial.print("Slave No Response:   "); Serial.println(diag[4]);
  Serial.print("Slave NAK Count:     "); Serial.println(diag[5]);
  Serial.print("Slave Busy Count:    "); Serial.println(diag[6]);
  Serial.print("Bus Char Overrun:    "); Serial.println(diag[7]);
}

void setup() {
  Serial.begin(9600);
  master.begin(9600, SERIAL_8N1);
}

void loop() {
  uint16_t diag[8];

  // Read diagnostic counters (sub-function 00)
  int result = master.diagnostic(1, 0x0000, 0x0000, diag);
  if (result == 0) {
    Serial.println("Before clear:");
    printDiagnostics(diag);
  }

  // Clear counters (sub-function 01)
  result = master.diagnostic(1, 0x0001, 0x0000, diag);
  Serial.println(result == 0 ? "Clear counters sent" : "Clear failed");

  delay(100);

  // Read again to verify clear
  result = master.diagnostic(1, 0x0000, 0x0000, diag);
  if (result == 0) {
    Serial.println("After clear:");
    printDiagnostics(diag);
  }

  delay(5000);
}```

Before clear: Bus Message Count: 150 Bus CRC Error Count: 3 ... Clear counters sent After clear: Bus Message Count: 0 Bus CRC Error Count: 0 ...


## Prevention

Modbus FC 08 (Diagnostics) sub-function 00 returns diagnostic counters. Sub-function 01 clears them. Not all slaves support diagnostic clear. The diagnostic counters track: Bus Message Count, CRC Error Count, Exception Count, Slave Message Count, Slave No Response Count, Slave NAK Count, Slave Busy Count, Character Overrun Count. Clear after reading to establish a baseline for monitoring.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### Why won't counters clear?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Some slaves implement counters as read-only. Check the device manual for diagnostic support. Some industrial devices require password authentication to clear counters.</p>
<h3 id="what-diagnostic-sub-functions-exist">What diagnostic sub-functions exist?</h3><p>00=Return Query Data, 01=Restart Comm Option, 02=Return Diagnostic Register, 03=Change ASCII Delimiter, 04=Force Listen Only, 10=Clear Counters, 11=Return Bus Message Count, 12=Return Bus CRC Error Count, 13=Return Bus Exception Count, 14=Return Slave Message Count, 15=Return Slave No Response Count, 16=Return Slave NAK Count, 17=Return Slave Busy Count, 18=Return Bus Character Overrun Count.</p>
<h3 id="can-i-read-counters-on-any-device">Can I read counters on any device?</h3><p>No. FC 08 is optional. Many simple Modbus slaves (sensors, actuators) do not implement diagnostics.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro