Advertisement
HaVecko

MODBUS_Schneider iEM3155_read_only

Sep 28th, 2022 (edited)
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | Source Code | 0 0
  1. /*
  2. esp8266
  3. 14 / D5 - Rx
  4. 12 / D6 - Tx
  5. Elektromer Schneider iEM3155-M (38400) MODBUS
  6. RS485 to TTL module
  7. */
  8. #include <ModbusMaster.h>
  9. #include <SoftwareSerial.h>
  10. uint8_t result;
  11. uint16_t data[1024];
  12. uint16_t data_test1;
  13. uint16_t data_test2;
  14. uint16_t V1;
  15. uint16_t V2;
  16. uint16_t V3;
  17. uint16_t VAvg;
  18. uint16_t A1;
  19. uint16_t A2;
  20. uint16_t A3;
  21. uint16_t AAvg;
  22. uint16_t PwrFactor;
  23. uint16_t Freq;
  24. uint16_t Pwr1;
  25. uint16_t Pwr2;
  26. uint16_t Pwr3;
  27. uint16_t PwrTotal;
  28.  
  29. bool modbusError;
  30. SoftwareSerial mySerial(14, 12); // RX, TX
  31.  
  32. #define MAX485_DE      3
  33. #define MAX485_RE_NEG  2
  34.  
  35. ModbusMaster node;
  36.  
  37. void preTransmission()
  38. {
  39.   digitalWrite(MAX485_RE_NEG, 1);
  40.   digitalWrite(MAX485_DE, 1);
  41. }
  42.  
  43. void postTransmission()
  44. {
  45.   digitalWrite(MAX485_RE_NEG, 0);
  46.   digitalWrite(MAX485_DE, 0);
  47. }
  48.  
  49. void setup()
  50. {
  51.   pinMode(MAX485_RE_NEG, OUTPUT);
  52.   pinMode(MAX485_DE, OUTPUT);
  53.  
  54.   digitalWrite(MAX485_RE_NEG, 0);
  55.   digitalWrite(MAX485_DE, 0);
  56.  
  57.   Serial.begin(38400);
  58.   mySerial.begin(38400);
  59.  
  60.   node.begin(1, mySerial);
  61.  
  62.   node.preTransmission(preTransmission);
  63.   node.postTransmission(postTransmission);
  64.  
  65. }
  66.  
  67.  uint8_t readHoldingRegisters(uint16_t regpos, uint16_t size) {
  68.   uint8_t result;
  69.   result = node.readHoldingRegisters(regpos, size);
  70.   if (result == node.ku8MBSuccess)
  71.   {
  72.     for (int j = 0; j < size; j++)
  73.     {
  74.       data[j] = node.getResponseBuffer(j);
  75.     }
  76.   } else {
  77.     modbusError = true;
  78.   }
  79.  
  80.  
  81.   return result;
  82. }
  83.  
  84.  
  85. void loop() {
  86.   delay(1000);
  87.  Serial.println();  
  88.  
  89. // vycteni napeti faze 1 V1
  90.    delay(5);
  91.   readHoldingRegisters(0x0BD3, 1);
  92.     Serial.print("V1: ");
  93.  for(int i = 0; i < 1; i++)
  94. {
  95.   Serial.println(data[i], HEX);
  96.   V1 = data[i];
  97. }
  98. // vycteni napeti faze 2 V2
  99. delay(5);
  100.    readHoldingRegisters(0x0BD5, 1);
  101.    Serial.print("V2: ");
  102.  for(int i = 0; i < 1; i++)
  103. {
  104.   Serial.println(data[i], HEX);
  105.   V2 = data[i];
  106. }
  107. // vycteni napeti faze 3 V3
  108. delay(5);
  109.    readHoldingRegisters(0x0BD7, 1);
  110.    Serial.print("V3: ");
  111.  for(int i = 0; i < 1; i++)
  112. {
  113.   V3 = data[i];
  114.   Serial.println(data[i], HEX);
  115. }
  116. // vycteni napeti prumer VAvg
  117. delay(5);
  118.    readHoldingRegisters(0x0BDB, 1);
  119.    Serial.print("VAvg: ");
  120.  for(int i = 0; i < 1; i++)
  121. {
  122.   Serial.println(data[i], HEX);
  123.   VAvg = data[i];
  124. }
  125.  
  126. // vycteni proudu faze 1 A1
  127.    delay(5);
  128.   readHoldingRegisters(0x0BB7, 1);
  129.     Serial.print("A1: ");
  130.  for(int i = 0; i < 1; i++)
  131. {
  132.   Serial.println(data[i], HEX);
  133.   A1 = data[i];
  134. }
  135. // vycteni proudu faze 2 A2
  136. delay(5);
  137.    readHoldingRegisters(0x0BB9, 1);
  138.    Serial.print("A2: ");
  139.  for(int i = 0; i < 1; i++)
  140. {
  141.   Serial.println(data[i], HEX);
  142.   A2 = data[i];
  143. }
  144. // vycteni proudu faze 3 A3
  145. delay(5);
  146.    readHoldingRegisters(0x0BBB, 1);
  147.    Serial.print("A3: ");
  148.  for(int i = 0; i < 1; i++)
  149. {
  150.   Serial.println(data[i], HEX);
  151.   A3 = data[i];
  152. }
  153. // vycteni proudu prumer AAvg
  154. delay(5);
  155.    readHoldingRegisters(0x0BC1, 1);
  156.    Serial.print("AAvg: ");
  157.  for(int i = 0; i < 1; i++)
  158. {
  159.   Serial.println(data[i], HEX);
  160.   AAvg = data[i];
  161. }
  162. // vycteni vykon faze 1 Pwr1
  163.    delay(5);
  164.   readHoldingRegisters(0x0BED, 1);
  165.     Serial.print("Pwr1: ");
  166.  for(int i = 0; i < 1; i++)
  167. {
  168.   Serial.println(data[i], HEX);
  169.   Pwr1 = data[i];
  170. }
  171. // vycteni vykon faze 2 Pwr2
  172. delay(5);
  173.    readHoldingRegisters(0x0BEF, 1);
  174.    Serial.print("Pwr2: ");
  175.  for(int i = 0; i < 1; i++)
  176. {
  177.   Serial.println(data[i], HEX);
  178.   Pwr2 = data[i];
  179. }
  180. // vycteni vykon faze 3 Pwr3
  181. delay(5);
  182.    readHoldingRegisters(0x0BF1, 1);
  183.    Serial.print("Pwr3: ");
  184.  for(int i = 0; i < 1; i++)
  185. {
  186.   Serial.println(data[i], HEX);
  187.   Pwr3 = data[i];
  188. }
  189. // vycteni vykon prumer PwrTotal
  190. delay(5);
  191.    readHoldingRegisters(0x0BF3, 1);
  192.    Serial.print("PwrTotal: ");
  193.  for(int i = 0; i < 1; i++)
  194. {
  195.   Serial.println(data[i], HEX);
  196.   PwrTotal = data[i];
  197. }
  198. // vycteni uciniku PwrFactor
  199. delay(5);
  200.    readHoldingRegisters(0x0C0B, 1);
  201.    Serial.print("PwrFactor: ");
  202.  for(int i = 0; i < 1; i++)
  203. {
  204.   Serial.println(data[i], HEX);
  205.   PwrFactor = data[i];
  206. }
  207. // vycteni frekvence site Freq
  208. delay(5);
  209.    readHoldingRegisters(0x0C25, 1);
  210.    Serial.print("Freq: ");
  211.  for(int i = 0; i < 1; i++)
  212. {
  213.   Serial.println(data[i], HEX);
  214.   Freq = data[i];
  215. }
  216.  
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement