-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (52 loc) · 1.49 KB
/
main.py
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
import serial
import time
import signal
import threading
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
from PMS7003 import PMS7003
dust = PMS7003()
# Baud Rate
Speed = 9600
# UART / USB Serial
USB0 = '/dev/ttyUSB0'
UART = '/dev/tty.usbserial-14320'
exitThread = False # 쓰레드 종료용 변수
#쓰레드 종료용 시그널 함수
def handler(signum, frame):
exitThread = True
#본 쓰레드
def readThread(ser):
global exitThread
# 쓰레드 종료될때까지 계속 돌림
while not exitThread:
buffer = ser.read(1024)
if (dust.protocol_chk(buffer)):
data = dust.unpack_data(buffer)
print("PMS 7003 dust data")
print("PM 1.0 : %s" % (data[dust.DUST_PM1_0_ATM]))
print("PM 2.5 : %s" % (data[dust.DUST_PM2_5_ATM]))
print("PM 10.0 : %s" % (data[dust.DUST_PM10_0_ATM]))
else:
print("data read Err")
if __name__ == "__main__":
#종료 시그널 등록
signal.signal(signal.SIGINT, handler)
# USE PORT
SERIAL_PORT = UART
# serial setting
ser = serial.Serial(SERIAL_PORT, Speed, timeout=1)
#시리얼 읽을 쓰레드 생성
thread = threading.Thread(target=readThread, args=(ser,))
#시작!
thread.start()
# import serial
#
# PORT = '/dev/tty.usbserial-14320'
# baudrate = 9600
# device = serial.Serial(PORT, baudrate=baudrate)
# print(device.name)
# y = device.readline()
# print(y.decode(encoding='cp949'))
#