-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (69 loc) · 2.87 KB
/
Copy pathProgram.cs
File metadata and controls
87 lines (69 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using RJCP.IO.Ports;
namespace PMS5003 {
public class Program {
private static ushort GetShort(byte b1, byte b2)
{
return (ushort) (b2 | b1 << 8);
}
public static void Main(string[] args) {
if (args.Length != 1)
{
Console.WriteLine("Usage: PMS5003.exe <port>");
return;
}
var portName = args[0];
using (var port = new SerialPortStream(portName))
{
port.BaudRate = 9600;
port.Parity = Parity.None;
port.Open();
while (true)
{
// search for header byte1
while (port.ReadByte() != 0x42) { }
if (port.ReadByte() != 0x4D)
{
Console.WriteLine("WARNING: Bad header");
continue;
}
// Frame length
var fmLength = GetShort((byte) port.ReadByte(), (byte) port.ReadByte());
// Datas
var buffer = new byte[fmLength];
port.Read(buffer, 0, buffer.Length);
// Read as ushort
var data = new ushort[fmLength / 2];
for (int i = 0; i < buffer.Length; i+=2)
{
data[i / 2] = GetShort(buffer[i], buffer[i + 1]);
}
// Checksum
ushort receiveSum = 0x42 + 0x4D + 28;
for (byte i = 0; i < (buffer.Length - 2); i++)
{
receiveSum += buffer[i];
}
if (receiveSum - data[13] != 0)
{
Console.WriteLine("WARNING: Bad Checksum");
continue;
}
Console.WriteLine($"PM1.0 ug / m3: {data[0]}");
Console.WriteLine($"PM2.5 ug / m3: {data[1]}");
Console.WriteLine($"PM10 ug/ m3: {data[2]}");
Console.WriteLine($"PM1.0 ug / m3(atmos env): {data[3]}");
Console.WriteLine($"PM2.5 ug / m3(atmos env): {data[4]}");
Console.WriteLine($"PM10 ug/ m3(atmos env): {data[5]}");
Console.WriteLine($"> 0.3um in 0.1L air: {data[6]}");
Console.WriteLine($"> 0.5um in 0.1L air: {data[7]}");
Console.WriteLine($"> 1.0um in 0.1L air: {data[8]}");
Console.WriteLine($"> 2.5um in 0.1L air: {data[9]}");
Console.WriteLine($"> 5.0um in 0.1L air: {data[10]}");
Console.WriteLine($"> 10um in 0.1L air: {data[11]}");
}
port.Close();
}
}
}
}