-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial.py
117 lines (97 loc) · 3.37 KB
/
Serial.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
import serial
import time
import sys
import glob
from datetime import datetime
def make_dir(path):
if not os.path.exists(path):
os.makedirs(path)
class Serialport():
def __init__(self):
self.SerialPortList= []
self.SerialPortUsed = None
self.SerialPort=None
#self.SendInterval = 1
self.Modem=None
self.DetectPlatformOS()
def GetPlatformOS(self):
return self.Platform
def DetectPlatformOS(self):
"""Find out PlatformOS
"""
if sys.platform.startswith('win'):
self.Platform = "win"
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
self.Platform = "linux"
elif sys.platform.startswith('darwin'):
self.Platform = "darwin"
else:
self.Platform = "NaN"
raise EnvironmentError('Unsupported platform')
def SetSendInterval(self,interval):
"""Set interval of request sending with ms
divide to 1000 (time.sleep use second)
"""
self.SendInterval = interval/1000.0 #time.sleep use second
def GetSendInterval(self):
"""return SendInterval
"""
return self.SendInterval*1000
def SetSerialPort(self, port,baudrate=9600):
"""Set serial port to use modem
"""
if self.SerialPortUsed != None:
self.SerialPort.close()
self.SerialPortUsed = port
self.SerialPort = serial.Serial(port,baudrate,timeout=0)
self.SerialPort.close()
self.SerialPort.open()
print "set port",port
def GetSerialPortList(self):
"""return available Serial port
"""
self.FindSerialPort()
return self.SerialPortList
def GetSerialReadline(self):
return self.SerialPort.readline()
def SendSerialCommand(self,command):
"""Send Serial command to serial port
"""
if self.SerialPortUsed != None:
self.SerialPort.write(command)
#time.sleep(self.SendInterval)
# return self.SerialPort.read(size=100)
text=self.SerialPort.readline()
while len(text) < 10:
text=self.SerialPort.readline()
return text
else:
raise EnvironmentError('Serial Port is not choosen')
def GetPlatform(self):
return self.Platform
def FindSerialPort(self):
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
self.SerialPortList[:]=[]
if self.Platform == "win":
ports = ['COM%s' % (i + 1) for i in range(256)]
elif self.Platform == "linux":
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif self.Platform == "darwin":
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
#print ports
for port in ports:
try:
s = serial.Serial(port)
s.close()
self.SerialPortList.append(port)
print port
except (OSError, serial.SerialException):
pass