Ported Kaifa class to Arduino. Reading list ID and time...

This commit is contained in:
Roar Fredriksen 2017-09-22 00:46:26 +02:00
parent 853c88a395
commit 00f34f00ff
5 changed files with 101 additions and 4 deletions

View File

@ -14,10 +14,12 @@
#include "DlmsReader.h"
#include "Crc16.h"
#include "KaifaHan.h"
DlmsReader reader;
byte buffer[512];
int bytesRead;
KaifaHan kaifa;
void setup() {
// Initialize the Serial1 port for debugging
@ -41,11 +43,22 @@ void loop() {
{
byte newByte = Serial.read();
//if (newByte < 0x10) Serial1.print("0");
//Serial1.print(newByte, HEX);
Serial1.print(newByte, HEX);
if (reader.Read(newByte))
{
bytesRead = reader.GetRawData(buffer, 0, 512);
byte list = kaifa.GetListID(buffer, 0, bytesRead);
Serial1.println("");
Serial1.print("List #");
Serial1.print(list, HEX);
time_t time = kaifa.GetPackageTime(buffer, 0, bytesRead);
Serial1.print(" (time is ");
Serial1.print(time);
Serial1.println("):");
writeAndEmptyBuffer();
}
}

View File

@ -0,0 +1,57 @@
#include "KaifaHan.h"
byte KaifaHan::GetListID(byte *buffer, int start, int length)
{
if (length > 23)
{
byte list = buffer[start + 23];
if (list == List1) return List1;
if (list == List2) return List2;
if (list == List3) return List3;
}
return ListUnknown;
}
long KaifaHan::GetPackageTime(byte *buffer, int start, int length)
{
const int timeStart = 10;
int year = buffer[start + timeStart] << 8 |
buffer[start + timeStart + 1];
int month = buffer[start + timeStart + 2];
int day = buffer[start + timeStart + 3];
int hour = buffer[start + timeStart + 5];
int minute = buffer[start + timeStart + 6];
int second = buffer[start + timeStart + 7];
return toUnixTime(year, month, day, hour, minute, second);
}
time_t KaifaHan::toUnixTime(int year, int month, int day, int hour, int minute, int second)
{
byte daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
long secondsPerMinute = 60;
long secondsPerHour = secondsPerMinute * 60;
long secondsPerDay = secondsPerHour * 24;
long time = (year - 1970) * secondsPerDay * 365L;
for (int yearCounter = 1970; yearCounter<year; yearCounter++)
if ((yearCounter % 4 == 0) && ((yearCounter % 100 != 0) || (yearCounter % 400 == 0)))
time += secondsPerDay;
if (month > 2 && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
time += secondsPerDay;
for (int monthCounter = 1; monthCounter<month; monthCounter++)
time += daysInMonth[monthCounter - 1] * secondsPerDay;
time += (day - 1) * secondsPerDay;
time += hour * secondsPerHour;
time += minute * secondsPerMinute;
time += second;
return (time_t)time;
}

View File

@ -0,0 +1,27 @@
#ifndef _KAIFAHAN_h
#define _KAIFAHAN_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
class KaifaHan
{
public:
const byte ListUnknown = 0x00;
const byte List1 = 0x01;
const byte List2 = 0x0D;
const byte List3 = 0x12;
byte GetListID(byte *buffer, int start, int length);
long GetPackageTime(byte *buffer, int start, int length);
protected:
private:
time_t toUnixTime(int year, int month, int day, int hour, int minute, int second);
};
#endif

View File

@ -15,7 +15,7 @@ namespace HanDebugger
public const byte List2 = 0x0D;
public const byte List3 = 0x12;
public static byte GetPackageID(byte[] package, int start, int length)
public static byte GetListID(byte[] package, int start, int length)
{
switch (package[start + 23])
{

View File

@ -20,7 +20,7 @@ namespace HanDebuggerTest
var lines = packages.Select(line => line.Trim().Split(' ').Select(v => (byte)int.Parse(v, System.Globalization.NumberStyles.HexNumber)).ToArray()).ToArray();
foreach (var line in lines)
{
byte list = KaifaHanBeta.GetPackageID(line, 0, line.Length);
byte list = KaifaHanBeta.GetListID(line, 0, line.Length);
if (packageCount.ContainsKey(list))
packageCount[list]++;
else
@ -54,7 +54,7 @@ namespace HanDebuggerTest
var lines = packages.Select(line => line.Trim().Split(' ').Select(v => (byte)int.Parse(v, System.Globalization.NumberStyles.HexNumber)).ToArray()).ToArray();
foreach (var line in lines)
{
if (KaifaHanBeta.GetPackageID(line, 0, line.Length) == KaifaHanBeta.List1)
if (KaifaHanBeta.GetListID(line, 0, line.Length) == KaifaHanBeta.List1)
{
consumption.Add(KaifaHanBeta.GetInt(0, line, 0, line.Length));
}