Created
August 11, 2020 22:59
-
-
Save tomekziel/11b24cd3891d284eee50084c08f6f15c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.IO.Ports; | |
namespace UD18TesterReader | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new Program().mainLoop(); | |
} | |
StreamWriter streamWriter; | |
public void mainLoop() | |
{ | |
Int32 i = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; | |
streamWriter = new StreamWriter(i + ".txt"); | |
write("datetime,unixtime,volt,amp,mah,wh,dmin,dplus,h,m,s\n"); | |
var serialPort = new SerialPort("COM7"); | |
serialPort.Open(); | |
while (true) | |
{ | |
waitForHeader(serialPort); | |
readAndPrintData(serialPort); | |
} | |
} | |
private void readAndPrintData(SerialPort sp) | |
{ | |
write(DateTime.UtcNow.ToString("s")); | |
write(", "); | |
Int32 i = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; | |
write(i.ToString()); | |
write(", "); | |
//=========================== volt | |
int v0 = sp.ReadByte(); | |
int v1 = sp.ReadByte(); | |
int v2 = sp.ReadByte(); | |
int xyz = v0 << 16 | v1 << 8 | v2; // volt | |
write(xyz.ToString()); | |
write(", "); | |
//=========================== amp | |
v0 = sp.ReadByte(); | |
v1 = sp.ReadByte(); | |
v2 = sp.ReadByte(); | |
xyz = v0 << 16 | v1 << 8 | v2; // amp | |
write(xyz.ToString()); | |
write(", "); | |
//=========================== mAH | |
v0 = sp.ReadByte(); | |
v1 = sp.ReadByte(); | |
v2 = sp.ReadByte(); | |
xyz = v0 << 16 | v1 << 8 | v2; // mah | |
write(xyz.ToString()); | |
write(", "); | |
//=========================== wH | |
v0 = sp.ReadByte(); | |
v1 = sp.ReadByte(); | |
v2 = sp.ReadByte(); | |
int v3 = sp.ReadByte(); | |
xyz = v0 << 24 | v1 << 16 | v2 << 8 | v3; // wH | |
write(xyz.ToString()); | |
write(", "); | |
//=========================== d- | |
v0 = sp.ReadByte(); | |
v1 = sp.ReadByte(); | |
xyz = v0 << 8 | v1; // d- | |
write(xyz.ToString()); | |
write(", "); | |
//=========================== d+ | |
v0 = sp.ReadByte(); | |
v1 = sp.ReadByte(); | |
xyz = v0 << 8 | v1; // d+ | |
write(xyz.ToString()); | |
write(", "); | |
//=========================== nothing | |
v0 = sp.ReadByte(); | |
v1 = sp.ReadByte(); | |
v2 = sp.ReadByte(); | |
//=========================== HH | |
v0 = sp.ReadByte(); | |
write(v0.ToString()); | |
write(", "); | |
//=========================== MM | |
v0 = sp.ReadByte(); | |
write(v0.ToString()); | |
write(", "); | |
//=========================== SS | |
v0 = sp.ReadByte(); | |
write(v0.ToString()); | |
write("\n"); | |
} | |
public void write(string s) | |
{ | |
Console.Write(s); | |
streamWriter.Write(s); | |
streamWriter.Flush(); | |
} | |
public void waitForHeader(SerialPort sp) | |
{ | |
int[] header = new int[4]; | |
while (header[0] != 0xff || header[1] != 0x55 || header[2] != 0x01 || header[3] != 0x03 ) | |
{ | |
header[0] = header[1]; | |
header[1] = header[2]; | |
header[2] = header[3]; | |
header[3] = sp.ReadByte(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment