Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define SerialControl 7
- #define RS485Tx HIGH
- #define RS485Rx LOW
- SoftwareSerial RS485Serial(11, 12);
- uint8_t window[8]; // Initialising window of 8 bytes
- byte SendKWh[] = { 0x01, 0x03, 0x40, 0x34, 0x00, 0x02, 0x90, 0x05 };
- // 0x01: Meter Address, 0x03: Read Function Code, 0x40: Register Address Variable (Start address high bit)
- // 0x34: Register Address Variable (Start address low bit)
- // 0x00: Register Quantity (High Bit), 0x02: Register Quantity (Low Bit)
- // 0x90: CRC check code (CRC code Low bit)
- // 0x05: CRC check code (CRC code High bit)
- void dumpWin() {
- for (int i = 0; i < 8; i++) {
- Serial.print(window[i], HEX);
- Serial.print(" ");
- }
- Serial.println();
- }
- void ReadRx() {
- // Sliding Window Implementation for reading data
- if(RS485Serial.available()) {
- uint8_t b = RS485Serial.read();
- dumpWin();
- // Slide the contents of the array down
- for (uint8_t i = 0; i < 7; i++) {
- window[i] = window[i+1];
- }
- window[7] = b; // Next frame inserted at the top of previous
- if ((window[0] == 0x01) && (window[1] == 0x03) &&
- (window[2] == 0x40) && (window[3] == 0x34)) {
- // Doing something with window[4] and window[5] here
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(SerialControl, OUTPUT);
- RS485Serial.begin(9600);
- }
- void loop() {
- static uint32_t ts = millis();
- if (millis() - ts >= 2000) {
- ts = millis();
- digitalWrite(SerialControl, RS485Tx);
- RS485Serial.write(SendKWh, sizeof(SendKWh));
- RS485Serial.flush();
- digitalWrite(SerialControl, RS485Rx);
- RS485Serial.listen();
- }
- ReadRx();
- }
- #include <SoftwareSerial.h>
- #define SerialControl 7
- #define RS485Tx HIGH
- #define RS485Rx LOW
- SoftwareSerial RS485Serial(11, 12);
- byte SendKWh[] = { 0x01, 0x03, 0x40, 0x34, 0x00, 0x02, 0x90, 0x05 };
- // 0x01: Meter Address, 0x03: Read Function Code, 0x40: Register Address Variable (Start address high bit)
- // 0x34: Register Address Variable (Start address low bit)
- // 0x00: Register Quantity (High Bit), 0x02: Register Quantity (Low Bit)
- // 0x90: CRC check code (CRC code Low bit)
- // 0x05: CRC check code (CRC code High bit)
- uint8_t window[8]; // Initialising window of 8 bytes
- void dumpWin() {
- for (int i = 0; i < 8; i++) {
- Serial.print(window[i], HEX);
- Serial.print(" ");
- }
- Serial.println();
- }
- void ReadRx() {
- if(RS485Serial.available()) {
- uint8_t b = RS485Serial.read();
- dumpWin();
- // Slide the contents of the array down
- for (uint8_t i = 0; i < 7; i++) {
- window[i] = window[i+1];
- }
- window[7] = b; // Next frame inserted at the top of previous
- if ((window[0] == 0x01) && (window[1] == 0x03) &&
- (window[2] == 0x40) && (window[3] == 0x34)) {
- // Doing something with window[4] and window[5] here
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(SerialControl, OUTPUT);
- digitalWrite(SerialControl, RS485Rx);
- RS485Serial.begin(9600);
- }
- void loop() {
- static uint32_t ts = millis();
- if (millis() - ts >= 2000) {
- ts = millis();
- digitalWrite(SerialControl, RS485Tx);
- RS485Serial.write(SendKWh, sizeof(SendKWh));
- RS485Serial.flush();
- digitalWrite(SerialControl, RS485Rx);
- RS485Serial.listen();
- }
- ReadRx();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement