El medidor SDM120 ignora mi trama Modbus construida a mano desde un Nano
Traducido del English por IA ·
Otra vez yo, con otra pregunta de principiante, perdón.Quiero leer el voltaje y la potencia del medidor de energía Eastron SDM120 de mi casa en Hermetry. Arduino Nano + módulo MAX485 en los terminales RS485 del medidor, A con A y B con B, 120 ohm entre ellos en el extremo del medidor. DE y RE unidos en D2. Más adelante el Nano pasará los valores a un ESP32; ahora mismo solo intento obtener alguna respuesta.No quise usar una biblioteca porque quería entender cómo funciona, así que construyo la trama a mano. Leo el registro de entrada 0x0000 (voltaje), 2 registros, dirección de esclavo 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);
Luego espero bytes. Nunca llega nada de vuelta. La pantalla del medidor muestra la dirección 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.
Traducido del English por IA ·
¿Por qué no usas simplemente una biblioteca de Modbus? Parece mucho lío.
Why not just use a Modbus library? Seems like a lot of pain.
Traducido del English por IA ·
¿A qué velocidad en baudios está configurado el medidor? Si no me equivoco, el SDM120 viene de fábrica a 2400, mucha gente no se da cuenta.
Which baud rate is the meter on? The SDM120 ships at 2400 if I remember right, lots of people miss that.
Traducido del English por IA ·
Revisé el menú del medidor, 2400 8N1 y mi Serial.begin(2400, SERIAL_8N1). Así que no es eso.
Checked the meter menu, 2400 8N1 and my Serial.begin(2400, SERIAL_8N1). So that’s not it.
Traducido del English por IA ·
Creo que print y write hacen lo mismo, ¿quizás los bytes CRC están en el orden incorrecto?
print and write do the same thing I think, maybe the CRC bytes are in the wrong order?
Traducido del English por IA ·
El orden del CRC es correcto, Modbus envía primero el byte bajo y 0x71 0xCB es correcto para esa trama. El problema es print.Serial.print(0x01) envía el texto «1», el byte 0x31. Serial.print(0x71) envía «113», tres bytes. Así que el medidor recibe algo como «1400021137…» en ASCII, que no es una trama Modbus en absoluto, y se queda en silencio. Serial.write envía el byte en bruto, que es lo que necesita Modbus RTU.Segundo problema: sueltas DE en cuanto termina el bucle, pero a 2400 baudios los bytes siguen saliendo de la UART durante unos 30 ms más. Cambiar a recepción corta el final de la trama. Espera a que termine la transmisión:
digitalWrite(2, HIGH);
Serial.write(frame, sizeof frame);
Serial.flush(); // retorna cuando el último byte ha salido de la UART
digitalWrite(2, LOW);
La respuesta son 9 bytes: dirección, 0x04, número de bytes 0x04, cuatro bytes de datos, CRC. La tensión del SDM120 es un float de 32 bits, big-endian, con la palabra alta primero:
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 es la tensión
Como la única UART hardware del Nano ahora está en el lado RS485, depura con un pin SoftwareSerial o con un parpadeo, no con el monitor serie USB. Cuando lo envíes, manda {"voltage": 231.4} como número para que el gráfico funcione.
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.
Traducido del English por IA ·
¡Serial.write + flush y el medidor respondió a la primera! 9 bytes, 233.1 V. No tenía ni idea de que print lo convertía en texto. Ahora voy a escribir la potencia (registro 0x000C) y luego paso al 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.
Traducido del English por IA ·
Vale, o sea que print convierte a texto. Ahora tiene sentido, me equivoqué con el CRC.
ok so print converts to text. Makes sense now, I was wrong about the CRC.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

