Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. #define SerialControl 7
  4.  
  5. #define RS485Tx HIGH
  6. #define RS485Rx LOW
  7.  
  8.  
  9. SoftwareSerial RS485Serial(11, 12);
  10. uint8_t window[8]; // Initialising window of 8 bytes
  11.  
  12. byte SendKWh[] = { 0x01, 0x03, 0x40, 0x34, 0x00, 0x02, 0x90, 0x05 };
  13.  
  14. // 0x01: Meter Address, 0x03: Read Function Code, 0x40: Register Address Variable (Start address high bit)
  15. // 0x34: Register Address Variable (Start address low bit)
  16. // 0x00: Register Quantity (High Bit), 0x02: Register Quantity (Low Bit)
  17. // 0x90: CRC check code (CRC code Low bit)
  18. // 0x05: CRC check code (CRC code High bit)
  19.  
  20. void dumpWin() {
  21. for (int i = 0; i < 8; i++) {
  22. Serial.print(window[i], HEX);
  23. Serial.print(" ");
  24. }
  25. Serial.println();
  26. }
  27.  
  28. void ReadRx() {
  29.  
  30. // Sliding Window Implementation for reading data
  31.  
  32. if(RS485Serial.available()) {
  33.  
  34. uint8_t b = RS485Serial.read();
  35.  
  36. dumpWin();
  37.  
  38.  
  39. // Slide the contents of the array down
  40. for (uint8_t i = 0; i < 7; i++) {
  41. window[i] = window[i+1];
  42. }
  43.  
  44. window[7] = b; // Next frame inserted at the top of previous
  45.  
  46. if ((window[0] == 0x01) && (window[1] == 0x03) &&
  47. (window[2] == 0x40) && (window[3] == 0x34)) {
  48.  
  49. // Doing something with window[4] and window[5] here
  50. }
  51. }
  52. }
  53.  
  54.  
  55.  
  56. void setup() {
  57.  
  58. Serial.begin(115200);
  59. pinMode(SerialControl, OUTPUT);
  60.  
  61. RS485Serial.begin(9600);
  62.  
  63. }
  64.  
  65. void loop() {
  66.  
  67. static uint32_t ts = millis();
  68.  
  69. if (millis() - ts >= 2000) {
  70. ts = millis();
  71. digitalWrite(SerialControl, RS485Tx);
  72. RS485Serial.write(SendKWh, sizeof(SendKWh));
  73. RS485Serial.flush();
  74. digitalWrite(SerialControl, RS485Rx);
  75. RS485Serial.listen();
  76. }
  77.  
  78. ReadRx();
  79. }
  80.  
  81. #include <SoftwareSerial.h>
  82.  
  83. #define SerialControl 7
  84.  
  85. #define RS485Tx HIGH
  86. #define RS485Rx LOW
  87.  
  88.  
  89. SoftwareSerial RS485Serial(11, 12);
  90.  
  91. byte SendKWh[] = { 0x01, 0x03, 0x40, 0x34, 0x00, 0x02, 0x90, 0x05 };
  92.  
  93. // 0x01: Meter Address, 0x03: Read Function Code, 0x40: Register Address Variable (Start address high bit)
  94. // 0x34: Register Address Variable (Start address low bit)
  95. // 0x00: Register Quantity (High Bit), 0x02: Register Quantity (Low Bit)
  96. // 0x90: CRC check code (CRC code Low bit)
  97. // 0x05: CRC check code (CRC code High bit)
  98.  
  99. uint8_t window[8]; // Initialising window of 8 bytes
  100.  
  101. void dumpWin() {
  102. for (int i = 0; i < 8; i++) {
  103. Serial.print(window[i], HEX);
  104. Serial.print(" ");
  105. }
  106. Serial.println();
  107. }
  108.  
  109. void ReadRx() {
  110. if(RS485Serial.available()) {
  111. uint8_t b = RS485Serial.read();
  112. dumpWin();
  113. // Slide the contents of the array down
  114. for (uint8_t i = 0; i < 7; i++) {
  115. window[i] = window[i+1];
  116. }
  117.  
  118. window[7] = b; // Next frame inserted at the top of previous
  119.  
  120. if ((window[0] == 0x01) && (window[1] == 0x03) &&
  121. (window[2] == 0x40) && (window[3] == 0x34)) {
  122. // Doing something with window[4] and window[5] here
  123. }
  124. }
  125. }
  126.  
  127.  
  128.  
  129. void setup() {
  130. Serial.begin(115200);
  131. pinMode(SerialControl, OUTPUT);
  132. digitalWrite(SerialControl, RS485Rx);
  133. RS485Serial.begin(9600);
  134.  
  135. }
  136.  
  137. void loop() {
  138. static uint32_t ts = millis();
  139.  
  140. if (millis() - ts >= 2000) {
  141. ts = millis();
  142. digitalWrite(SerialControl, RS485Tx);
  143. RS485Serial.write(SendKWh, sizeof(SendKWh));
  144. RS485Serial.flush();
  145. digitalWrite(SerialControl, RS485Rx);
  146. RS485Serial.listen();
  147. }
  148.  
  149. ReadRx();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement