資料來源:http://maker.tn.edu.tw/modules/tad_book3/page.php?tbsn=21&tbdsn=379
PMS5003T 主動式傳輸協議
默認串列傳輸速率: 9600bps 校驗位:無 停止位: 1 位
協議總長度: 32 位元組
1 | 起始符 1 | 0x42 (固定) |
2 | 起始符 2 | 0x4d (固定) |
3 | 幀長度高八位 | 幀長度=2x13+2(資料+校驗位元) |
4 | 幀長度低八位 | |
5 | 數據 1 高八位 | *資料 1 表示 PM1.0 濃度(CF=1,標準顆粒物)單位μ g/m3 |
6 | 數據 1 低八位 | |
7 | 數據 2 高八位 | 資料 2 表示 PM2.5 濃度(CF=1,標準顆粒物) 單位μ g/m3 |
8 | 數據 2 低八位 | |
9 | 數據 3 高八位 | 資料 3 表示 PM10 濃度(CF=1,標準顆粒物) 單位μ g/m3 |
10 | 數據 3 低八位 | |
11 | 數據 4 高八位 | *資料 4 表示 PM1.0 濃度(大氣環境下) 單位μ g/m3 |
12 | 數據 4 低八位 | |
13 | 數據 5 高八位 | 資料 5 表示 PM2.5 濃度(大氣環境下) 單位μ g/m3 |
14 | 數據 5 低八位 | |
15 | 數據 6 高八位 | 資料 6 表示 PM10 濃度 (大氣環境下) 單位μ g/m3 |
16 | 數據 6 低八位 | |
17 | 數據 7 高八位 | 資料 7 表示 0.1 升空氣中直徑在 0.3um 以上 顆粒物個數 |
18 | 數據 7 低八位 | |
19 | 數據 8 高八位 | 資料 8 表示 0.1 升空氣中直徑在 0.5um 以上 顆粒物個數 |
20 | 數據 8 低八位 | |
21 | 數據 9 高八位 | 資料 9 表示 0.1 升空氣中直徑在 1.0um 以上 顆粒物個數 |
22 | 數據 9 低八位 | |
23 | 數據 10 高八位 | 資料 10 表示 0.1 升空氣中直徑在 2.5um 以上 顆粒物個數 |
24 | 數據 10 低八位 | |
25 | 數據 11 高八位 | 溫度。 注:真實溫度值=本數值/10 單位:℃ |
26 | 數據 11 低八位 | |
27 | 數據 12 高八位 | 濕度。 注:真實濕度值=本數值/10 單位:% |
28 | 數據 12 低八位 | |
29 | 數據 13 高八位 | 版本號 |
30 | 數據 13 低八位 | 錯誤代碼 |
31 | 資料和校驗高八位元 | 校驗碼=起始符 1+起始符 2+……..+數據 13 低八位 |
32 | 資料和校驗低八位元 |
程式範例
#include <SoftwareSerial.h>
SoftwareSerial PmsSerial(
2
,
3
);
// RX, TX
static
unsigned
int
pm_cf_10;
//定義全域變數
static
unsigned
int
pm_cf_25;
static
unsigned
int
pm_cf_100;
static
unsigned
int
pm_at_10;
static
unsigned
int
pm_at_25;
static
unsigned
int
pm_at_100;
static
unsigned
int
particulate03;
static
unsigned
int
particulate05;
static
unsigned
int
particulate10;
static
unsigned
int
particulate25;
static
float sencorT;
static
float sencorH;
void
getG5(unsigned char ucData)
//獲取G5的值
{
static
unsigned
int
ucRxBuffer[
250
];
static
unsigned
int
ucRxCnt =
0
;
ucRxBuffer[ucRxCnt++] = ucData;
if
(ucRxBuffer[
0
] !=
0x42
&& ucRxBuffer[
1
] !=
0x4D
)
//資料頭判斷
{
ucRxCnt =
0
;
return
;
}
if
(ucRxCnt >
32
)
//資料位元數判斷//G5T為32
{
pm_cf_10=(
int
)ucRxBuffer[
4
] *
256
+ (
int
)ucRxBuffer[
5
];
//大氣環境下PM2.5濃度計算
pm_cf_25=(
int
)ucRxBuffer[
6
] *
256
+ (
int
)ucRxBuffer[
7
];
pm_cf_100=(
int
)ucRxBuffer[
8
] *
256
+ (
int
)ucRxBuffer[
9
];
pm_at_10=(
int
)ucRxBuffer[
10
] *
256
+ (
int
)ucRxBuffer[
11
];
pm_at_25=(
int
)ucRxBuffer[
12
] *
256
+ (
int
)ucRxBuffer[
13
];
pm_at_100=(
int
)ucRxBuffer[
14
] *
256
+ (
int
)ucRxBuffer[
15
];
particulate03=(
int
)ucRxBuffer[
16
] *
256
+ (
int
)ucRxBuffer[
17
];
particulate05=(
int
)ucRxBuffer[
18
] *
256
+ (
int
)ucRxBuffer[
19
];
particulate10=(
int
)ucRxBuffer[
20
] *
256
+ (
int
)ucRxBuffer[
21
];
particulate25=(
int
)ucRxBuffer[
22
] *
256
+ (
int
)ucRxBuffer[
23
];
sencorT=((
int
)ucRxBuffer[
24
] *
256
+ (
int
)ucRxBuffer[
25
])/
10
;
sencorH=((
int
)ucRxBuffer[
26
] *
256
+ (
int
)ucRxBuffer[
27
])/
10
;
if
(pm_cf_25 >
999
)
//如果PM2.5數值>1000,返回重新計算
{
ucRxCnt =
0
;
return
;
}
ucRxCnt =
0
;
return
;
}
}
void
setup() {
PmsSerial.begin(
9600
);
PmsSerial.setTimeout(
1500
);
Serial.begin(
9600
);
Serial.print(
"PM_CF1.0"
);Serial.print(
","
);
Serial.print(
"PM_CF2.5"
);Serial.print(
","
);
Serial.print(
"PM_CF10"
);Serial.print(
","
);
Serial.print(
"PM_AQI1.0"
);Serial.print(
","
);
Serial.print(
"PM_AQI2.5"
);Serial.print(
","
);
Serial.print(
"PM_AQI10"
);Serial.print(
","
);
Serial.print(
"PM_count03"
);Serial.print(
","
);
Serial.print(
"PM_count05"
);Serial.print(
","
);
Serial.print(
"PM_count10"
);Serial.print(
","
);
Serial.print(
"PM_count25"
);Serial.print(
","
);
Serial.print(
"Temperature"
);Serial.print(
","
);
Serial.println(
"Humandity"
);
Serial.print(
"(ug/m3)"
);Serial.print(
","
);
Serial.print(
"(ug/m3)"
);Serial.print(
","
);
Serial.print(
"(ug/m3)"
);Serial.print(
","
);
Serial.print(
"(ug/m3)"
);Serial.print(
","
);
Serial.print(
"(ug/m3)"
);Serial.print(
","
);
Serial.print(
"(ug/m3)"
);Serial.print(
","
);
Serial.print(
"(pcs/0.01cf)"
);Serial.print(
","
);
Serial.print(
"(pcs/0.01cf)"
);Serial.print(
","
);
Serial.print(
"(pcs/0.01cf)"
);Serial.print(
","
);
Serial.print(
"(pcs/0.01cf)"
);Serial.print(
","
);
Serial.print(
"(C)"
);Serial.print(
","
);
Serial.println(
"(%)"
);
}
void
loop() {
while
(PmsSerial.available()) {
getG5(PmsSerial.read());
}
Serial.print(pm_cf_10);Serial.print(
","
);
Serial.print(pm_cf_25);Serial.print(
","
);
Serial.print(pm_cf_100);Serial.print(
","
);
Serial.print(pm_at_10);Serial.print(
","
);
Serial.print(pm_at_25);Serial.print(
","
);
Serial.print(pm_at_100);Serial.print(
","
);
Serial.print(particulate03);Serial.print(
","
);
Serial.print(particulate05);Serial.print(
","
);
Serial.print(particulate10);Serial.print(
","
);
Serial.print(particulate25);Serial.print(
","
);
Serial.print(sencorT);Serial.print(
","
);
Serial.println(sencorH);
delay(
2000
);
}
沒有留言:
張貼留言