Congelador a -12,5 °C aparece como 65411 después de pasar por mi esclavo Modbus
Traducido del English por IA ·
Estudiante, mi primer proyecto Modbus. Un Arduino Mega lee una sonda de congelador y actúa como esclavo Modbus RTU (biblioteca ModbusRTUSlave). Guardo la temperatura multiplicada por 10 en el holding register 0 para conservar un decimal. Una Raspberry Pi la consulta con pymodbus y envía temp a Hermetry.Las temperaturas positivas son perfectas: 4,2 °C se envía como 42 y sale como 4,2. Pero el congelador está bajo cero, y ahí obtengo disparates. -12,5 °C se convierte en 6541,1 en Hermetry. El valor bruto del registro en la Raspberry Pi es 65411.
rr = client.read_holding_registers(0, count=1, slave=3)
temp = rr.registers[0] / 10 # 6541.1 ??
En el Arduino escribo holdingRegisters[0] = temp * 10; donde temp es un float. ¿Qué estoy haciendo mal?
Student, first Modbus project. Arduino Mega reads a freezer probe and acts as a Modbus RTU slave (ModbusRTUSlave library). I store the temperature times 10 in holding register 0 so I keep one decimal. A Raspberry Pi polls it with pymodbus and sends temp to Hermetry.
Positive temperatures are perfect: 4.2 C goes in as 42 and comes out as 4.2. But the freezer is below zero, and there I get nonsense. -12.5 C turns into 6541.1 in Hermetry. Raw register value on the Pi is 65411.
rr = client.read_holding_registers(0, count=1, slave=3)
temp = rr.registers[0] / 10 # 6541.1 ??
On the Arduino I write holdingRegisters[0] = temp * 10; where temp is a float. What am I doing wrong?
Traducido del English por IA ·
¿Quizás Modbus simplemente no admite números negativos? ¿Podrías sumar 100 a todo y restarlo después?
Maybe Modbus just doesn’t do negative numbers? Could you add 100 to everything and subtract it again later?
Traducido del English por IA ·
Un offset funciona, la gente lo hace, pero aquí no lo necesitas. El número está bien, el problema está en el lado de la lectura.
An offset works, people do it, but you don’t need it here. The number is fine, it’s the reading side.
Traducido del English por IA ·
Tuve exactamente lo mismo en un laboratorio el semestre pasado, con un registrador de un refrigerador. Los registros Modbus son de solo 16 bits y no saben de signos. Tu Arduino guarda -125 en complemento a dos, es decir 0xFF83, y como número sin signo eso es 65411. Entonces la Pi lo lee como sin signo.Así que conviértelo de nuevo en la Pi:
raw = rr.registers[0]
if raw > 32767: # top bit set means negative in int16
raw -= 65536
temp = raw / 10 # -12.5
O deja que pymodbus lo haga, el cliente tiene una función auxiliar para esto (en la 3.x):
temp = client.convert_from_registers(rr.registers, data_type=client.DATATYPE.INT16) / 10
En el lado del Arduino, haz el cast antes del registro para que el redondeo sea explícito: holdingRegisters[0] = (uint16_t)(int16_t)lround(temp * 10);. Con int16 x10 tienes de -3276.8 a 3276.7 C, más que suficiente para un congelador.Si una pasarela consulta el dispositivo en lugar de tu script, configura el tipo de ese registro como signed 16-bit en la configuración de la pasarela y ella hará la misma conversión.
Had the exact same thing in a lab last term with a fridge logger. Modbus registers are just 16 bits, they don’t know about signs. Your Arduino stores -125 as two’s complement, which is 0xFF83, and as an unsigned number that’s 65411. The Pi then reads it as unsigned.
So convert it back on the Pi:
raw = rr.registers[0]
if raw > 32767: # top bit set means negative in int16
raw -= 65536
temp = raw / 10 # -12.5
Or let pymodbus do it, the client has a helper for this (in 3.x):
temp = client.convert_from_registers(rr.registers, data_type=client.DATATYPE.INT16) / 10
On the Arduino side, cast before the register so rounding is explicit: holdingRegisters[0] = (uint16_t)(int16_t)lround(temp * 10);. With int16 x10 you have -3276.8 to 3276.7 C, plenty for a freezer.
If a gateway polls it instead of your script, set that register’s type to signed 16-bit in the gateway config and it does the same conversion.
Traducido del English por IA ·
Lo mismo ocurre con los valores de 32 bits repartidos en dos registros; además, hay que acertar con el orden de las palabras. Qué diversión.
Same goes for 32-bit values split over two registers, then you also have to get the word order right. Fun times.
Traducido del English por IA ·
El arreglo raw -= 65536 funciona, Hermetry muestra -12.5 ahora y el gráfico ya no salta a 6000. Los 6541 puntos de ayer siguen en el historial, pero todo lo nuevo está bien jaja. Gracias Aoife.
The raw -= 65536 fix works, Hermetry shows -12.5 now and the chart doesn’t jump to 6000 anymore. Yesterday’s 6541 points are still sitting in the history, but everything new is right lol. Thanks Aoife.
- 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.

