Le compteur SDM120 ignore ma trame Modbus construite à la main depuis un Nano
Traduit de English par IA ·
Me revoilà avec une autre question de débutant, désolé.Je veux récupérer la tension et la puissance du compteur d’énergie Eastron SDM120 de ma maison dans Hermetry. Arduino Nano + module MAX485 sur les bornes RS485 du compteur, A sur A et B sur B, 120 ohms entre les deux à l’extrémité du compteur. DE et RE reliés ensemble sur D2. Le Nano transmettra plus tard les valeurs à un ESP32 ; pour l’instant, j’essaie simplement d’obtenir une réponse.Je ne voulais pas utiliser de bibliothèque parce que je voulais comprendre, donc je construis la trame à la main. Je lis le registre d’entrée 0x0000 (tension), 2 registres, adresse esclave 1 :
byte frame[] = {0x01, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, 0xCB};
digitalWrite(2, HIGH);
for (int i = 0; i < 8; i++) Serial.print(frame[i]);
digitalWrite(2, LOW);
Ensuite j’attends des octets. Rien ne revient jamais. L’afficheur du compteur indique l’adresse 001.
Back again with another newbie one, sorry.
I want my house’s Eastron SDM120 energy meter’s voltage and power in Hermetry. Arduino Nano + MAX485 module on the meter’s RS485 terminals, A to A and B to B, 120 ohm across at the meter end. DE and RE tied together on D2. The Nano will hand the values to an ESP32 later; right now I’m just trying to get any answer.
I didn’t want a library because I wanted to understand it, so I build the frame by hand. Read input register 0x0000 (voltage), 2 registers, slave address 1:
byte frame[] = {0x01, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, 0xCB};
digitalWrite(2, HIGH);
for (int i = 0; i < 8; i++) Serial.print(frame[i]);
digitalWrite(2, LOW);
Then I wait for bytes. Nothing ever comes back. Meter display shows address 001.
Traduit de English par IA ·
Pourquoi ne pas simplement utiliser une bibliothèque Modbus ? Ça semble être beaucoup de galère.
Why not just use a Modbus library? Seems like a lot of pain.
Traduit de English par IA ·
Sur quel débit en bauds le compteur est-il configuré ? Si je me souviens bien, le SDM120 sort d’usine à 2400, beaucoup de gens l’oublient.
Which baud rate is the meter on? The SDM120 ships at 2400 if I remember right, lots of people miss that.
Traduit de English par IA ·
J’ai vérifié le menu du compteur, 2400 8N1 et mon Serial.begin(2400, SERIAL_8N1). Ce n’est donc pas ça.
Checked the meter menu, 2400 8N1 and my Serial.begin(2400, SERIAL_8N1). So that’s not it.
Traduit de English par IA ·
print et write font la même chose je pense, peut-être que les octets CRC sont dans le mauvais ordre ?
print and write do the same thing I think, maybe the CRC bytes are in the wrong order?
Traduit de English par IA ·
L’ordre du CRC est correct, Modbus envoie d’abord l’octet de poids faible, et 0x71 0xCB est juste pour cette trame. Le problème vient de print.Serial.print(0x01) envoie le texte « 1 », soit l’octet 0x31. Serial.print(0x71) envoie « 113 », soit trois octets. Le compteur reçoit donc quelque chose comme « 1400021137… » en ASCII, ce qui n’est pas du tout une trame Modbus, et il reste silencieux. Serial.write envoie l’octet brut, ce dont Modbus RTU a besoin.Deuxième problème : vous coupez DE dès la fin de la boucle, mais à 2400 bauds les octets sortent encore de l’UART pendant environ 30 ms. Passer en réception coupe la fin de la trame. Attendez la fin de l’émission :
digitalWrite(2, HIGH);
Serial.write(frame, sizeof frame);
Serial.flush(); // revient quand le dernier octet a quitté l'UART
digitalWrite(2, LOW);
La réponse fait 9 octets : adresse, 0x04, nombre d’octets 0x04, quatre octets de données, CRC. La tension du SDM120 est un flottant 32 bits en big-endian, mot de poids fort en premier :
union { uint32_t u; float f; } v;
v.u = ((uint32_t)r[3] << 24) | ((uint32_t)r[4] << 16) | (r[5] << 8) | r[6];
// v.f est la tension
Comme la seule UART matérielle du Nano est désormais côté RS485, déboguez via une broche SoftwareSerial ou avec un clignotement, pas avec le moniteur série USB. Quand vous l’enverrez, envoyez {"voltage": 231.4} sous forme de nombre pour que le graphique fonctionne.
CRC order is right, Modbus sends the low byte first and 0x71 0xCB is correct for that frame. The problem is print.
Serial.print(0x01) sends the text « 1 », the byte 0x31. Serial.print(0x71) sends « 113 », three bytes. So the meter receives something like « 1400021137… » in ASCII, which isn’t a Modbus frame at all, and it stays quiet. Serial.write sends the raw byte, which is what Modbus RTU needs.
Second problem: you drop DE the moment the loop ends, but at 2400 baud the bytes are still going out of the UART for another ~30 ms. Switching to receive cuts the end of the frame. Wait for the transmit to finish:
digitalWrite(2, HIGH);
Serial.write(frame, sizeof frame);
Serial.flush(); // returns when the last byte has left the UART
digitalWrite(2, LOW);
The answer is 9 bytes: address, 0x04, byte count 0x04, four data bytes, CRC. The SDM120 voltage is a 32-bit float, big-endian, high word first:
union { uint32_t u; float f; } v;
v.u = ((uint32_t)r[3] << 24) | ((uint32_t)r[4] << 16) | (r[5] << 8) | r[6];
// v.f is the voltage
Since the Nano’s only hardware UART is now on the RS485 side, debug through a SoftwareSerial pin or a blink, not the USB serial monitor. When you send it on, send {"voltage": 231.4} as a number so the chart works.
Traduit de English par IA ·
Serial.write + flush et le compteur a répondu du premier coup ! 9 octets, 233.1 V. Je ne savais pas que print le transformait en texte. Je vais maintenant écrire la puissance (registre 0x000C), puis je passe à l’ESP32.
Serial.write + flush and the meter answered first time! 9 bytes, 233.1 V. I had no idea print turned it into text. Writing power (register 0x000C) next, then it goes over to the ESP32.
Traduit de English par IA ·
OK, donc print convertit en texte. Ça a du sens maintenant, je me trompais sur le CRC.
ok so print converts to text. Makes sense now, I was wrong about the CRC.
- 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.

