-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_serial_arduino.py
82 lines (59 loc) · 1.56 KB
/
windows_serial_arduino.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
import serial
import time
import sys
import glob
import platform
def serial_ports():
if sys.platform.startswith('win'):
ports = ['COM' + str(i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this is to exclude your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
result = serial_ports()
plat=platform.system()
print plat
if not result:
print "No Serialports available!"
print "Press Ctrl+C to exit the program"
else:
print "Available Serialports"
print serial_ports()
print
if plat.startswith('Win'):
port1=result[0]
print "opening "+str(port1)
ser = serial.Serial(port1,9600,timeout=1)
time.sleep(2)
print"Connected"
while 1:
print "Getting Data:"
print
ser.write("t")
t=ser.read(10)
#ser.write("p")
#p=ser.read(20)
#time.sleep(0.25)
ser.write("h")
h=ser.read(10)
ser.write("d")
d=ser.read(10)
ser.write("s")
s=ser.read(10)
print "Temperature " + str(t)
print "Humidity " + str(h)
#print "Pressure " + str(p)
print "direction " + str(d)
print "speed " + str(s)
print
time.sleep(1)