mikroavr

read_modbus_float

Jun 9th, 2026
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ModbusMaster.h>
  2.  
  3. ModbusMaster node;
  4.  
  5. // Struktur Union untuk konversi Float (32-bit)
  6. union FloatConverter {
  7.   uint16_t regs[2];
  8.   float value;
  9. };
  10.  
  11. // Struktur Union untuk konversi Unsigned Long / uint32_t (32-bit)
  12. union UlongConverter {
  13.   uint16_t regs[2];
  14.   uint32_t value;
  15. };
  16.  
  17. void setup() {
  18.   Serial.begin(115200);
  19.   // node.begin(1, Serial2); // Sesuaikan ID Slave dan Serial Modbus Anda
  20. }
  21.  
  22. void loop() {
  23.   // Ambil data setiap 5 detik
  24.   bacaPrimaryActiveEnergy();
  25.   bacaSecondaryActiveEnergy();
  26.  
  27.   Serial.println("--------------------------------------------");
  28.   delay(5000);
  29. }
  30.  
  31. // ====================================================================
  32. // CONTOH 1: Membaca Sisi PRIMARY SIDE (Tipe Data: Float)
  33. // ====================================================================
  34. void bacaPrimaryActiveEnergy() {
  35.   // Alamat 0x008A (Total positive active energy - Primary)
  36.   uint8_t result = node.readHoldingRegisters(0x008A, 2);
  37.  
  38.   if (result == node.ku8MBSuccess) {
  39.     FloatConverter dataPrimary;
  40.    
  41.     // Sesuaikan urutan word (Big-Endian format)
  42.     dataPrimary.regs[1] = node.getResponseBuffer(0); // High Word
  43.     dataPrimary.regs[0] = node.getResponseBuffer(1); // Low Word
  44.    
  45.     float kwh_primary = dataPrimary.value;
  46.    
  47.     Serial.print("[PRIMARY] Total Positive Active Energy: ");
  48.     Serial.print(kwh_primary, 2);
  49.     Serial.println(" kWh (Nilai aktual lapangan)");
  50.   } else {
  51.     Serial.printf("[PRIMARY] Gagal membaca register! Error: 0x%02X\n", result);
  52.   }
  53. }
  54.  
  55. // ====================================================================
  56. // CONTOH 2: Membaca Sisi SECONDARY SIDE (Tipe Data: Ulong)
  57. // ====================================================================
  58. void bacaSecondaryActiveEnergy() {
  59.   // Alamat 0x0082 (Total positive active energy - Secondary)
  60.   uint8_t result = node.readHoldingRegisters(0x0082, 2);
  61.  
  62.   if (result == node.ku8MBSuccess) {
  63.     UlongConverter dataSecondary;
  64.    
  65.     // Gabungkan bitwise atau menggunakan union
  66.     dataSecondary.regs[1] = node.getResponseBuffer(0); // High Word
  67.     dataSecondary.regs[0] = node.getResponseBuffer(1); // Low Word
  68.    
  69.     uint32_t raw_value = dataSecondary.value;
  70.    
  71.     // Sesuai keterangan di datasheet: dikali faktor skala 0.001
  72.     float kwh_secondary = (float)raw_value * 0.001;
  73.    
  74.     Serial.print("[SECONDARY] Raw Value: ");
  75.     Serial.print(raw_value);
  76.     Serial.print(" | Terkonversi: ");
  77.     Serial.print(kwh_secondary, 3);
  78.     Serial.println(" kWh (Sisi Sekunder Sensor)");
  79.   } else {
  80.     Serial.printf("[SECONDARY] Gagal membaca register! Error: 0x%02X\n", result);
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment