#include <SoftwareSerial.h>
SoftwareSerial swSer(12, 14);
uint8_t ledPin = 2;
int delayTime = 1000;
unsigned long lastTime = 0;
int dataIndex=0;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
swSer.begin(4800);
}
// the loop function runs over and over again forever
void loop() {
if (millis() - lastTime >= delayTime) {
//閃爍燈
int ledStatus = digitalRead(ledPin);
digitalWrite(ledPin, !ledStatus);
uint8_t data[8]={0x01, 0x03, 0x00, 0x48, 0x00, 0x08, 0xC4, 0x1A};
for(int i=0; i<8; i=i+1) {
swSer.write(data[i]);
}
lastTime = millis();
}
uint8_t returnData[37];
while (swSer.available() > 0) { //當有資料由IM1266模組傳送過來
returnData[dataIndex]=swSer.read(); //將讀收到的資料儲存起來
dataIndex=dataIndex+1;
}
if(dataIndex==37) {
/*
for(int i=0; i<37; i++) {
Serial.println(returnData[i]);
}*/
float temp=((returnData[29]*256)+returnData[30])/100;
float V=((returnData[3]<<24)+(returnData[4]<<16)+(returnData[5]<<8)+returnData[6])/10000;
float I=((returnData[7]<<24)+(returnData[8]<<16)+(returnData[9]<<8)+returnData[10])/10000;
Serial.print(V);
Serial.print(" ");
Serial.print(I);
Serial.print(" ");
Serial.print(temp);
Serial.print("\n");
dataIndex=0;
}
}