Lire un compteur de tableau via Modbus RTU : comment 43 66 99 9A devient-il 230.6 V ?
Traduit de English par IA ·
Je dirige des installations depuis 24 ans et je n’ai jamais eu à décoder une trame Modbus à la main, nous y voilà donc.Un de nos techniciens de maintenance a construit un petit enregistreur : un Arduino Uno avec une carte MAX485 qui lit le compteur de tableau sur la ligne 4 (Modbus RTU, 9600 8N1, adresse esclave 1). L’Uno transmet les valeurs par liaison série à un Raspberry Pi qui les publie sur Hermetry. Il a quitté l’entreprise avant de terminer la partie décodage.L’Uno envoie cette requête et obtient une réponse à chaque fois, donc le câblage fonctionne :
TX: 01 03 00 00 00 02 C4 0B
RX: 01 03 04 43 66 99 9A 3A 1E
Le tableau affiche lui-même 230.6 V sur L1. Je ne vois pas comment ces octets donnent 230.6. Quelle partie de la réponse correspond à la valeur, et comment la transformer en un nombre que je peux envoyer ?
24 years running plants and I have never had to decode a Modbus frame by hand, so here we are.
One of our maintenance techs built a small logger: Arduino Uno with a MAX485 board reading the panel meter on line 4 (Modbus RTU, 9600 8N1, slave address 1). The Uno passes the values over serial to a Raspberry Pi that publishes them to Hermetry. He left for a new job before finishing the decoding part.
The Uno sends this request and gets a reply every time, so the wiring works:
TX: 01 03 00 00 00 02 C4 0B
RX: 01 03 04 43 66 99 9A 3A 1E
The panel itself shows 230.6 V on L1. I can’t see how those bytes turn into 230.6. What part of the reply is the value, and how do I turn it into a number I can send?
Traduit de English par IA ·
43 66 est-il la tension en hexadécimal ? 0x4366 vaut 17254, donc peut-être faut-il diviser par quelque chose ?
Is 43 66 the voltage in hex? 0x4366 is 17254, so maybe divide by something?
Traduit de English par IA ·
De quel compteur s’agit-il ? La table des registres du manuel indique si une valeur est un float ou un entier mis à l’échelle.
Which meter is it? The manual’s register map says whether a value is a float or a scaled integer.
Traduit de English par IA ·
J’ai trouvé le manuel sur la porte du tableau. « Tension L1, adresse 0x0000, float, 2 registres. »
Found the manual in the panel door. « Voltage L1, address 0x0000, float, 2 registers. »
Traduit de English par IA ·
Ensuite, les 2 registres forment un float 32 bits. Je reprends ta réponse point par point :
01 slave address
03 function (read holding registers)
04 byte count, 4 bytes follow
43 66 99 9A data: register 0 = 0x4366, register 1 = 0x999A
3A 1E CRC
Si on assemble les quatre octets de données en 0x4366999A et qu’on les lit comme un float IEEE 754 : signe 0, l’exposant donne 2^7 = 128, la mantisse vaut environ 1,8016, et 128 x 1,8016 = 230,6. Donc le 0x4366 de Wilson n’est que la première moitié du float, ce qui explique pourquoi la division ne donne jamais le bon résultat.Sur l’Uno, les octets arrivent en commençant par le poids fort, mais l’AVR est little endian : il faut donc les inverser avant de les copier dans un float :
uint8_t b[4] = { rx[6], rx[5], rx[4], rx[3] }; // 9A 99 66 43
float v;
memcpy(&v, b, 4);
Serial.print("{"l1.voltage":");
Serial.print(v, 1);
Serial.println("}");
Certains compteurs envoient les deux registres dans l’ordre inverse (word swap). Si tu obtiens quelque chose comme 1.2e-23 au lieu d’une tension, échange rx[3..4] avec rx[5..6]. Vérifie aussi le CRC avant de faire confiance à une réponse.
Then the 2 registers are one 32-bit float. Splitting your reply:
01 slave address
03 function (read holding registers)
04 byte count, 4 bytes follow
43 66 99 9A data: register 0 = 0x4366, register 1 = 0x999A
3A 1E CRC
Put the four data bytes together as 0x4366999A and read them as an IEEE 754 float: sign 0, exponent gives 2^7 = 128, mantissa about 1.8016, and 128 x 1.8016 = 230.6. So Wilson’s 0x4366 is the first half of the float, which is why dividing never comes out right.
On the Uno the bytes arrive most significant first but the AVR is little endian, so reverse them before copying into a float:
uint8_t b[4] = { rx[6], rx[5], rx[4], rx[3] }; // 9A 99 66 43
float v;
memcpy(&v, b, 4);
Serial.print("{"l1.voltage":");
Serial.print(v, 1);
Serial.println("}");
Some meters send the two registers the other way round (word swapped). If you ever get something like 1.2e-23 instead of a voltage, swap rx[3..4] with rx[5..6]. Check the CRC before trusting a reply too.
Traduit de English par IA ·
Cela vaut la peine d’envoyer un compteur d’erreurs avec les mesures, modbus.crc_errors ou similaire. Sur nos sites, la hausse de ce compteur a toujours été le premier signe d’un fil A/B desserré.
Worth sending an error counter next to the readings, modbus.crc_errors or similar. On our sites that counter going up was always the first sign of a loose A/B wire.
Traduit de English par IA ·
230,6 sur le tableau de bord, conforme au panneau. J’ai transmis ce fil au technicien qui a repris le projet, et il a ajouté les autres phases ainsi que le contrôle CRC en un après-midi. Wilson, j’ai d’abord essayé ton idée de division moi aussi, tu es en bonne compagnie.
230.6 on the dashboard, matching the panel. I passed this thread to the tech who took over and he added the other phases and the CRC check in an afternoon. Wilson, I tried your divide idea first too, so you’re in good company.
- Vous devez être connecté pour répondre à ce sujet.
Rejoignez la discussion
Connectez-vous pour poser et répondre à des questions, aimer les messages utiles et marquer la réponse qui a résolu votre problème. Chaque contribution rapporte des points et des niveaux.

