-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSerialtest.py
118 lines (109 loc) · 3.18 KB
/
Serialtest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import serial
import time
from datetime import datetime, timedelta
StartTime = datetime.now()
destination = "/home/pi/Desktop/Data/Particledata%s.txt" %str(StartTime)
a=open(destination,'w') #a means append to existing file, w means overwrite old
ser = serial.Serial(port='/dev/ttyUSB0',baudrate = 9600,parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
try:
ser.open()
except Exception, e:
print "Error: Cannot open serial port: " + str(e)
exit()
if ser.isOpen():
print ser.portstr
try:#flush input and output buffers that could be halting comunication
ser.flushInput()
ser.flushOutput()
except Exception, e1:
print "error communicating...: " + str(e1)
print 'enter your commands below. \r\nInsert "exit" to leave the aplication'
trash = ''
ser.write('U')
time.sleep(.5)
while ser.inWaiting() > 0:
trash += ser.read(1)
ser.write('c')
time.sleep(.5)
while ser.inWaiting() > 0:
trash += ser.read(1)
count = 0
while count < 200:
ser.write('e')
time.sleep(.5)
while ser.inWaiting() > 0:
trash += ser.read(1)
um1 = ''
um3 = ''
um5 = ''
um10 = ''
um15 = ''
um25 = ''
um50 = ''
um100 = ''
trash = ''
status = ''
ser.write('A')
time.sleep(.5)
trash = ser.read(1)
status =ser.read(1)
if status == " ":
status = 'No alarms'
elif status == "!":
status = 'Sensor Fail'
elif status == "$":
status = "Count Alarm"
else:
status = status
trash += ser.read(24)
um1 = ser.read(6)
trash += ser.read(5)
um3 = ser.read(6)
trash += ser.read(5)
um5 = ser.read(6)
trash += ser.read(5)
um10 = ser.read(6)
trash += ser.read(5)
um15 = ser.read(6)
trash += ser.read(5)
um25 = ser.read(6)
trash += ser.read(5)
um50 = ser.read(6)
trash += ser.read(5)
um100 = ser.read(6)
while ser.inWaiting() > 0:
trash += ser.read(1)
ser.write('c')
time.sleep(.5)
while ser.inWaiting() > 0:
trash += ser.read(1)
data = str(count)+'\t'+ str(status)+'\t'+str(um1)+'\t'+str(um3)+'\t'+str(um5)+'\t'+str(um10)+'\t'+str(um15)+'\t'+str(um25)+'\t'+str(um50)+'\t'+str(um100)
a.write("\n"+ str(datetime.now()) + "\t" + str(data))
ser.write('c')
time.sleep(.5)
while ser.inWaiting() > 0:
trash += ser.read(1)
time.sleep(9.5)
count += 1
print count
print 'exited sucsessfully;'
ser.write('e')
time.sleep(.5)
while ser.inWaiting() > 0:
trash += ser.read(1)
#print um5
## #get keyboard input
## input = raw_input("<< ")
## if input == 'exit' :
## ser.close()
## exit()
## else:
## #send character to the device
## ser.write(input)# may need \r or\n or \r\n to respond correctly
## trash = ''
## time.sleep(1)
## while ser.inWaiting() > 0:
## trash += ser.read(1)
##
## if trash != '':
## print ">>"+trash