From 5e5a8e254e33dd7c21cdcc18871cebbca5cda9b5 Mon Sep 17 00:00:00 2001 From: Hein Date: Thu, 19 Sep 2024 22:52:39 +0200 Subject: [PATCH] Work on the mods --- .../.~lock.PM8000_Modbus_Map.csv# | 1 + .../genreg/PM8000_Modbus_Map_ad.h | 599 ++++++++++++++++++ docs/PowerLogic-PM8000/genreg/buildregmap2.py | 58 ++ firmware/modbus-sd/modbus-sd-01.ino | 136 ++-- 4 files changed, 743 insertions(+), 51 deletions(-) create mode 100644 docs/PowerLogic-PM8000/.~lock.PM8000_Modbus_Map.csv# create mode 100644 docs/PowerLogic-PM8000/genreg/PM8000_Modbus_Map_ad.h create mode 100644 docs/PowerLogic-PM8000/genreg/buildregmap2.py diff --git a/docs/PowerLogic-PM8000/.~lock.PM8000_Modbus_Map.csv# b/docs/PowerLogic-PM8000/.~lock.PM8000_Modbus_Map.csv# new file mode 100644 index 0000000..18edcd2 --- /dev/null +++ b/docs/PowerLogic-PM8000/.~lock.PM8000_Modbus_Map.csv# @@ -0,0 +1 @@ +,ASHNAN-AMD-WIN/warkanum,ashnan-amd-win,19.09.2024 22:18,file:///C:/Users/warkanum/AppData/Roaming/LibreOffice/4; \ No newline at end of file diff --git a/docs/PowerLogic-PM8000/genreg/PM8000_Modbus_Map_ad.h b/docs/PowerLogic-PM8000/genreg/PM8000_Modbus_Map_ad.h new file mode 100644 index 0000000..58d342c --- /dev/null +++ b/docs/PowerLogic-PM8000/genreg/PM8000_Modbus_Map_ad.h @@ -0,0 +1,599 @@ + +#include +struct PM8000RegType +{ + uint16_t regaddr; + uint8_t regtype; +}; + +const PROGMEM PM8000RegType registers[] = { + { 1837, 1} , + { 1838, 1} , + { 1839, 1} , + { 1840, 1} , + { 1841, 1} , + { 2700, 2} , + { 2702, 2} , + { 2704, 2} , + { 2706, 2} , + { 2708, 2} , + { 2710, 2} , + { 2712, 2} , + { 2714, 2} , + { 2716, 2} , + { 2718, 2} , + { 2720, 2} , + { 2722, 2} , + { 2724, 2} , + { 2726, 2} , + { 2728, 2} , + { 2730, 2} , + { 2732, 2} , + { 2734, 2} , + { 2736, 2} , + { 2738, 2} , + { 2740, 2} , + { 2742, 2} , + { 2744, 2} , + { 2746, 2} , + { 2748, 2} , + { 2750, 2} , + { 2754, 2} , + { 2756, 2} , + { 2758, 2} , + { 2762, 2} , + { 2768, 2} , + { 2772, 2} , + { 2774, 2} , + { 2776, 2} , + { 2778, 2} , + { 2780, 2} , + { 2782, 2} , + { 2784, 2} , + { 2786, 2} , + { 2788, 2} , + { 2790, 2} , + { 2792, 2} , + { 2794, 2} , + { 2796, 2} , + { 2798, 2} , + { 2800, 2} , + { 2802, 2} , + { 2804, 2} , + { 2806, 2} , + { 2808, 2} , + { 2810, 2} , + { 3000, 2} , + { 3002, 2} , + { 3004, 2} , + { 3006, 2} , + { 3008, 2} , + { 3010, 2} , + { 3020, 2} , + { 3022, 2} , + { 3024, 2} , + { 3026, 2} , + { 3028, 2} , + { 3030, 2} , + { 3032, 2} , + { 3036, 2} , + { 3054, 2} , + { 3056, 2} , + { 3058, 2} , + { 3060, 2} , + { 3062, 2} , + { 3064, 2} , + { 3066, 2} , + { 3068, 2} , + { 3070, 2} , + { 3072, 2} , + { 3074, 2} , + { 3076, 2} , + { 3110, 2} , + { 3204, 3} , + { 3208, 3} , + { 3212, 3} , + { 3216, 3} , + { 3220, 3} , + { 3224, 3} , + { 3228, 3} , + { 3232, 3} , + { 3236, 3} , + { 3240, 3} , + { 3244, 3} , + { 3248, 3} , + { 3256, 3} , + { 3260, 3} , + { 3264, 3} , + { 3268, 3} , + { 3272, 3} , + { 3276, 3} , + { 3280, 3} , + { 3284, 3} , + { 3288, 3} , + { 3292, 3} , + { 3296, 3} , + { 3300, 3} , + { 3358, 3} , + { 3362, 3} , + { 3370, 3} , + { 3374, 3} , + { 3378, 3} , + { 3386, 3} , + { 3398, 3} , + { 3414, 3} , + { 3418, 3} , + { 3422, 3} , + { 3426, 3} , + { 3430, 3} , + { 3434, 3} , + { 3438, 3} , + { 3442, 3} , + { 3446, 3} , + { 3450, 3} , + { 3454, 3} , + { 3458, 3} , + { 3462, 3} , + { 3466, 3} , + { 3470, 3} , + { 3474, 3} , + { 3478, 3} , + { 3482, 3} , + { 3486, 3} , + { 3490, 3} , + { 3650, 2} , + { 3652, 2} , + { 3654, 2} , + { 3656, 2} , + { 3658, 2} , + { 3660, 2} , + { 3668, 2} , + { 3670, 2} , + { 3672, 2} , + { 3674, 2} , + { 3676, 2} , + { 3678, 2} , + { 3680, 2} , + { 3682, 2} , + { 3684, 2} , + { 4196, 3} , + { 4200, 3} , + { 4204, 3} , + { 4208, 3} , + { 4228, 3} , + { 4232, 3} , + { 4236, 3} , + { 4240, 3} , + { 4260, 3} , + { 4264, 3} , + { 4268, 3} , + { 4272, 3} , + { 4292, 3} , + { 4296, 3} , + { 4300, 3} , + { 4304, 3} , + { 4324, 3} , + { 4328, 3} , + { 4332, 3} , + { 4336, 3} , + { 4356, 3} , + { 4360, 3} , + { 4364, 3} , + { 4368, 3} , + { 4800, 2} , + { 4802, 2} , + { 4804, 2} , + { 4806, 2} , + { 4816, 2} , + { 4818, 2} , + { 4820, 2} , + { 4822, 2} , + { 4832, 2} , + { 4834, 2} , + { 4836, 2} , + { 4838, 2} , + { 4848, 2} , + { 4850, 2} , + { 4852, 2} , + { 4854, 2} , + { 4864, 2} , + { 4866, 2} , + { 4868, 2} , + { 4870, 2} , + { 4880, 2} , + { 4882, 2} , + { 4884, 2} , + { 4886, 2} , + { 14045, 2} , + { 14049, 2} , + { 14325, 2} , + { 14329, 2} , + { 14585, 2} , + { 14589, 2} , + { 14605, 2} , + { 14609, 2} , + { 14625, 2} , + { 14629, 2} , + { 21000, 2} , + { 21002, 2} , + { 21004, 2} , + { 21006, 2} , + { 21008, 2} , + { 21010, 2} , + { 21016, 2} , + { 21018, 2} , + { 21020, 2} , + { 21022, 2} , + { 21024, 2} , + { 21026, 2} , + { 21028, 2} , + { 21030, 2} , + { 21034, 2} , + { 21040, 2} , + { 21042, 2} , + { 21044, 2} , + { 21046, 2} , + { 21048, 2} , + { 21050, 2} , + { 21052, 2} , + { 21054, 2} , + { 21056, 2} , + { 21058, 2} , + { 21060, 2} , + { 21062, 2} , + { 21358, 2} , + { 21360, 2} , + { 21362, 2} , + { 27218, 2} , + { 27220, 2} , + { 27222, 2} , + { 27224, 2} , + { 27226, 2} , + { 27228, 2} , + { 27238, 2} , + { 27240, 2} , + { 27242, 2} , + { 27244, 2} , + { 27246, 2} , + { 27248, 2} , + { 27250, 2} , + { 27254, 2} , + { 27278, 2} , + { 27286, 2} , + { 27294, 2} , + { 27616, 2} , + { 27644, 2} , + { 27646, 2} , + { 27648, 2} , + { 27650, 2} , + { 27652, 2} , + { 27654, 2} , + { 27656, 2} , + { 27658, 2} , + { 27660, 2} , + { 27672, 2} , + { 27674, 2} , + { 27676, 2} , + { 27682, 2} , + { 27694, 2} , + { 27696, 2} , + { 27698, 2} , + { 27700, 2} , + { 27702, 2} , + { 27704, 2} , + { 27714, 2} , + { 27716, 2} , + { 27718, 2} , + { 27720, 2} , + { 27722, 2} , + { 27724, 2} , + { 27726, 2} , + { 27730, 2} , + { 27754, 2} , + { 27762, 2} , + { 27770, 2} , + { 28092, 2} , + { 28120, 2} , + { 28122, 2} , + { 28124, 2} , + { 28126, 2} , + { 28128, 2} , + { 28130, 2} , + { 28132, 2} , + { 28134, 2} , + { 28136, 2} , + { 28162, 2} , + { 28164, 2} , + { 28166, 2} , + { 28172, 2} , + { 28180, 2} , + { 28182, 2} , + { 28184, 2} , + { 28186, 2} , + { 28188, 2} , + { 28190, 2} , + { 28192, 2} , + { 28194, 2} , + { 28196, 2} , + { 28208, 2} , + { 28210, 2} , + { 28212, 2} , + { 28218, 2} , + { 29884, 2} , + { 29886, 2} , + { 29888, 0} , + { 29898, 2} , + { 29900, 2} , + { 29902, 0} , + { 29912, 2} , + { 29914, 2} , + { 29916, 0} , + { 29926, 2} , + { 29928, 2} , + { 29930, 0} , + { 29940, 2} , + { 29942, 2} , + { 29944, 0} , + { 29954, 2} , + { 29956, 2} , + { 29958, 0} , + { 29968, 2} , + { 29970, 2} , + { 29972, 0} , + { 29982, 2} , + { 29984, 2} , + { 29986, 0} , + { 29996, 2} , + { 29998, 2} , + { 30000, 0} , + { 30010, 2} , + { 30012, 2} , + { 30014, 0} , + { 30024, 2} , + { 30026, 2} , + { 30028, 0} , + { 30038, 2} , + { 30040, 2} , + { 30042, 0} , + { 30052, 2} , + { 30054, 2} , + { 30056, 0} , + { 30066, 2} , + { 30068, 2} , + { 30070, 0} , + { 30080, 2} , + { 30082, 2} , + { 30084, 0} , + { 30094, 2} , + { 30096, 2} , + { 30098, 0} , + { 30108, 2} , + { 30110, 2} , + { 30112, 0} , + { 30222, 2} , + { 30224, 2} , + { 30226, 0} , + { 30236, 2} , + { 30238, 2} , + { 30240, 0} , + { 30250, 2} , + { 30252, 2} , + { 30254, 0} , + { 30264, 2} , + { 30266, 2} , + { 30268, 0} , + { 30278, 2} , + { 30280, 2} , + { 30282, 0} , + { 30292, 2} , + { 30294, 2} , + { 30296, 0} , + { 30306, 2} , + { 30308, 2} , + { 30310, 0} , + { 30320, 2} , + { 30322, 2} , + { 30324, 0} , + { 30334, 2} , + { 30336, 2} , + { 30338, 0} , + { 30348, 2} , + { 30350, 2} , + { 30352, 0} , + { 30362, 2} , + { 30364, 2} , + { 30366, 0} , + { 30376, 2} , + { 30378, 2} , + { 30380, 0} , + { 30390, 2} , + { 30392, 2} , + { 30394, 0} , + { 30404, 2} , + { 30406, 2} , + { 30408, 0} , + { 30418, 2} , + { 30420, 2} , + { 30422, 0} , + { 30432, 2} , + { 30434, 2} , + { 30436, 0} , + { 30446, 2} , + { 30448, 2} , + { 30450, 0} , + { 30460, 2} , + { 30462, 2} , + { 30464, 0} , + { 30474, 2} , + { 30476, 2} , + { 30478, 0} , + { 30488, 2} , + { 30490, 2} , + { 30492, 0} , + { 30502, 2} , + { 30504, 2} , + { 30506, 0} , + { 30516, 2} , + { 30518, 2} , + { 30520, 0} , + { 30530, 2} , + { 30532, 2} , + { 30534, 0} , + { 30544, 2} , + { 30546, 2} , + { 30548, 0} , + { 30558, 2} , + { 30560, 2} , + { 30562, 0} , + { 30572, 2} , + { 30574, 2} , + { 30576, 0} , + { 30586, 2} , + { 30588, 2} , + { 30590, 0} , + { 30600, 2} , + { 30602, 2} , + { 30604, 0} , + { 30614, 2} , + { 30616, 2} , + { 30618, 0} , + { 30628, 2} , + { 30630, 2} , + { 30632, 0} , + { 30642, 2} , + { 30644, 2} , + { 30646, 0} , + { 30656, 2} , + { 30658, 2} , + { 30660, 0} , + { 30670, 2} , + { 30672, 2} , + { 30674, 0} , + { 30684, 2} , + { 30686, 2} , + { 30688, 0} , + { 30698, 2} , + { 30700, 2} , + { 30702, 0} , + { 30712, 2} , + { 30714, 2} , + { 30716, 0} , + { 30822, 2} , + { 30824, 2} , + { 30826, 2} , + { 30828, 2} , + { 30830, 2} , + { 30832, 2} , + { 30834, 2} , + { 30836, 2} , + { 30838, 2} , + { 30840, 2} , + { 30842, 2} , + { 30844, 2} , + { 30846, 2} , + { 30848, 2} , + { 30850, 2} , + { 30852, 2} , + { 30854, 2} , + { 30856, 2} , + { 30858, 2} , + { 30860, 2} , + { 30862, 2} , + { 30864, 2} , + { 30866, 2} , + { 30868, 2} , + { 30870, 3} , + { 30874, 3} , + { 30878, 3} , + { 30882, 3} , + { 30886, 3} , + { 30890, 3} , + { 30894, 3} , + { 30898, 3} , + { 30902, 3} , + { 30906, 3} , + { 30910, 2} , + { 30912, 2} , + { 30914, 2} , + { 30916, 2} , + { 30918, 2} , + { 30920, 2} , + { 30922, 2} , + { 30924, 2} , + { 30926, 2} , + { 30928, 3} , + { 30932, 3} , + { 30936, 3} , + { 30940, 3} , + { 30944, 3} , + { 30948, 3} , + { 30952, 3} , + { 30956, 3} , + { 30960, 3} , + { 30964, 3} , + { 30968, 2} , + { 30970, 2} , + { 30972, 2} , + { 30974, 2} , + { 30976, 2} , + { 30978, 2} , + { 30980, 2} , + { 30982, 2} , + { 30984, 2} , + { 30986, 2} , + { 30988, 2} , + { 30990, 2} , + { 30992, 2} , + { 30994, 2} , + { 30996, 2} , + { 30998, 2} , + { 31000, 2} , + { 31002, 2} , + { 31004, 2} , + { 31006, 2} , + { 31008, 2} , + { 31010, 2} , + { 31012, 2} , + { 31014, 2} , + { 31016, 2} , + { 31018, 2} , + { 31020, 2} , + { 31022, 2} , + { 31024, 2} , + { 34352, 2} , + { 34354, 2} , + { 34358, 2} , + { 34360, 2} , + { 34364, 2} , + { 34366, 2} , + { 34400, 2} , + { 34402, 2} , + { 34404, 2} , + { 34406, 2} , + { 34408, 2} , + { 34410, 2} , + { 34412, 2} , + { 34414, 2} , + { 34416, 2} , + { 34472, 2} , + { 34474, 2} , + { 34476, 2} , + { 40000, 2} , + { 40002, 2} , + { 40004, 2} , + { 40006, 2} , + { 40008, 2} , + { 40010, 2} , + { 40012, 2} , + { 40014, 2} , + { 40016, 2} , + { 40018, 2} , + { 40020, 2} , + { 40022, 2} , + { 40024, 2} , + { 40026, 2} , + { 54396, 1} , + { 56977, 0} , + +} +; diff --git a/docs/PowerLogic-PM8000/genreg/buildregmap2.py b/docs/PowerLogic-PM8000/genreg/buildregmap2.py new file mode 100644 index 0000000..9bb1500 --- /dev/null +++ b/docs/PowerLogic-PM8000/genreg/buildregmap2.py @@ -0,0 +1,58 @@ +import csv + +print("Exporter") + +file = open("../PM8000_Modbus_Map.csv", "r") +data = list(csv.reader(file, delimiter=",")) +file.close() + +header = """ +#include +struct PM8000RegType +{ + uint16_t regaddr; + uint8_t regtype; +}; + +const PROGMEM PM8000RegType registers[] = { + [replace] +} +; +""" + + + +lines = "" +for d in range(1, len(data)): + typeint = 0 + if not data[d][1].isnumeric(): + continue + # if data[d][9] == "NONE": + # continue + if data[d][4] == "---" or data[d][4] == "seconds" or data[d][4] == "degrees": + continue + if data[d][3] == "UTF8" or data[d][3] == "DATETIME" or data[d][3] == "BITMAP" or data[d][3] == "N/A": + continue + if data[d][0].find("Magnitude") >= 0: + continue + + if data[d][3] == "INT16U": + typeint = 1 + elif data[d][3] == "FLOAT32": + typeint = 2 + elif data[d][3] == "INT64": + typeint = 3 + elif data[d][3] == "INT32U": + typeint = 4 + + + #print("{} @ {}:{} ({}) Tag: {}".format(data[d][0], data[d][1] ,data[d][2] ,data[d][3],data[d][7])) + lines += " { " + data[d][1] + ", " + str(typeint) + "} ,\n" +# + + +print(header.replace("[replace]", lines)) + +file = open("PM8000_Modbus_Map_ad.h", "w") +file.write(header.replace("[replace]", lines)) +file.close() \ No newline at end of file diff --git a/firmware/modbus-sd/modbus-sd-01.ino b/firmware/modbus-sd/modbus-sd-01.ino index ae95170..28f8191 100644 --- a/firmware/modbus-sd/modbus-sd-01.ino +++ b/firmware/modbus-sd/modbus-sd-01.ino @@ -1,66 +1,84 @@ #include #include -#include +#include #include -#include -#include +#include "PM8000_Modbus_Map_ad.h" +//#include + +#include +#include #define SD_CS_PIN 10 // Chip Select for SD Card //RS485 pins #define DE_RE_PIN 4 -#define RX_PIN 3 // SoftwareSerial RX pin -#define TX_PIN 2 // SoftwareSerial TX pin +#define RX_PIN 8 // SoftwareSerial RX pin +#define TX_PIN 7 // SoftwareSerial TX pin #define SLAVE_ID 101 +#define SERIAL_BAUDRATE 9600 + +// Try to select the best SD card configuration. +#define SPI_CLOCK SD_SCK_MHZ(50) +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK) +#endif // HAS_SDIO_CLASS RTC_DS3231 rtc; // Create an RTC object +SdFat32 sd; +//SdExFat sd; File dataFile; -SoftwareSerial modbusSerial(RX_PIN, TX_PIN); // Create a software serial instance +NeoSWSerial modbusSerial(RX_PIN, TX_PIN); // Create a software serial instance ModbusMaster node; void setup() { - Serial.begin(9600); // For debugging - Serial.println("Startup \n"); - + Serial.begin(SERIAL_BAUDRATE); // For debugging + Serial.println(F("Startup \n")); + // Initialize RTC if (!rtc.begin()) { - Serial.println("Couldn't find RTC\n"); + Serial.println(F("Couldn't find RTC\n")); } if (rtc.lostPower()) { - Serial.println("RTC lost power, let's set the time!\n"); + Serial.println(F("RTC lost power, let's set the time!\n")); // Comment out the following line once the time is set to avoid resetting on every start rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } - Serial.print("Time: "); + Serial.print(F("Time: ")); Serial.print(rtc.now().timestamp()); - Serial.println("\n"); + Serial.println(F("\n")); // Initialize SD card - Serial.println("SD card initializing..."); + Serial.println(F("SD card initializing...")); pinMode(SD_CS_PIN, OUTPUT); - if (!SD.begin(SPI_HALF_SPEED, SD_CS_PIN )) - { - Serial.println("SD card initialization failed!\n"); - return; + // if (!SD.begin(SPI_HALF_SPEED, SD_CS_PIN )) + // { + // Serial.println(F("SD card initialization failed!\n")); + // return; + // } + // Initialize the SD. + if (!sd.begin(SD_CONFIG)) { + sd.initErrorHalt(&Serial); + return; } - Serial.println("SD card initialized.\n"); + Serial.println(F("SD card initialized.\n")); - Serial.println("Initialize RS485 module / Modbus \n"); + Serial.println(F("Initialize RS485 module / Modbus \n")); pinMode(DE_RE_PIN, OUTPUT); digitalWrite(DE_RE_PIN, LOW); // Set to LOW for receiving mode initially - - modbusSerial.begin(9600 ); -// -// while (!modbusSerial) { -// } + modbusSerial.begin(SERIAL_BAUDRATE); + node.begin(SLAVE_ID, modbusSerial); node.preTransmission(preTransmission); node.postTransmission(postTransmission); @@ -81,7 +99,7 @@ void writeFile(char *str) { if (!dataFile) { - Serial.println("Error opening file"); + Serial.println(F("Error opening file")); return; } DateTime now = rtc.now(); @@ -97,7 +115,7 @@ void writeFile(char *str) dataFile.print(now.minute(), DEC); dataFile.print(':'); dataFile.print(now.second(), DEC); - dataFile.print(","); + dataFile.print(F(",")); dataFile.print(str); dataFile.println(); @@ -106,37 +124,25 @@ void writeFile(char *str) String getFilename() { DateTime now = rtc.now(); - String mb = ""; + String mb = F(""); mb += now.year(); mb += now.month(); mb += now.day(); - mb += ".csv"; + mb += F(".csv"); return mb; } -static unsigned long lastRefreshTime = 0; +unsigned long lastRefreshTime = 0; void loop() { - uint8_t result = result = node.readHoldingRegisters(2705 , 2); - - // Check if the read was successful - if (result == node.ku8MBSuccess) - { - Serial.print("Read successful: "); - - } else { - Serial.print("Read error: "); - Serial.println(result, HEX); - } - - + delay(100); if(millis() - lastRefreshTime >= 1000) { lastRefreshTime += 1000; - Serial.print("Time: "); + Serial.print(F("\nTime: ")); Serial.print(rtc.now().timestamp()); // Serial.print("\nHeep:"); // Serial.print(ESP.getFreeHeap()); @@ -144,22 +150,50 @@ void loop() // Open file for writing String filename = getFilename(); - Serial.print("Open Card "); + dataFile.open(filename.c_str(), FILE_WRITE); + Serial.print(F("Open Card ")); Serial.print(filename.c_str()); Serial.print("\n"); - - dataFile = SD.open(filename.c_str(), FILE_WRITE); - + + String mb = "modbus,"; + writeFile(mb.c_str()); if (dataFile) { dataFile.close(); // Close the file - Serial.print("Data written to SD card: "); + Serial.print(F("Data written to SD card: ")); Serial.print(filename.c_str()); - Serial.print("\n"); + Serial.print(F("\n")); } - Serial.print("\n\n"); + Serial.print(F("\n\n")); } + + + + for (int i = 0; i < 10; i++) { + + Serial.print(F("\naddress: ")); + Serial.print(registers[i].regaddr); + Serial.print(F("\ntype: ")); + Serial.print(registers[i].regtype); + Serial.print(F("\n ")); + if (registers[i].regaddr > 0) { + uint8_t result = node.readHoldingRegisters(registers[i].regaddr , 2); + + if (result == node.ku8MBSuccess) + { + Serial.print(F("Modbus Read successful:")); + + } else { + Serial.print(F("Modbus Read error: ")); + Serial.println(result, HEX); + } + } + } + + // // Check if the read was successful + + delay(100); }