-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflysorterSerial.py
More file actions
127 lines (112 loc) · 3.84 KB
/
flysorterSerial.py
File metadata and controls
127 lines (112 loc) · 3.84 KB
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
119
120
121
122
123
124
125
126
127
##
## This copyrighted software is distributed under the GPL v2.0 license.
## See the LICENSE file for more details.
##
## Serial port handling class
import serial
import os
import sys
import glob
import time
# Serial communications class that is used for multiple devices.
#
# In MAPLE, the smoothie board is a serial devices. The smoothie is connected directly
# via USB.
#
# Documentation is available online for G-codes (smoothie):
#
# http://smoothieware.org/supported-g-codes and
# http://reprap.org/wiki/G-code
#
# This class is also used to communicate with the Fly Dispenser (if connected & used).
# The Dispenser, similar to the Smoothie, connects by USB and appears as a COM port.
class serialDevice:
"""Serial class for FlySorter serial device (FlyDispenser, MAPLE robot, etc)."""
WaitTimeout = 3
portName = ""
def __init__(self, port, baud = 9600, timeout = float(0.1)):
self.isOpened = False
try:
self.ser = serial.Serial(port, baudrate = baud, timeout = timeout)
except:
print "Failed to open port", port
return
self.isOpened = True
def close(self):
self.ser.close()
# Retrieve any waiting data on the port
def getSerOutput(self):
#print "GSO:"
output = ''
while True:
# read() blocks for the timeout set above *if* there is nothing to read
# otherwise it returns immediately
byte = self.ser.read(1)
if byte is None or byte == '':
break
output += byte
if byte == '\n':
break
#print "GSO Output:", output
return output
# Block and wait for the device to reply with "ok" or "OK"
# Times out after self.WaitTimeout (set above)
def waitForOK(self):
#print "WFO:"
output = ''
timeoutMax = self.WaitTimeout / self.ser.timeout
timeoutCount = 0
while True:
byte = self.ser.read(1)
if byte is None or byte == '':
timeoutCount += 1
time.sleep(1)
else:
output += byte
if timeoutCount > timeoutMax:
print 'Serial timeout.'
break
if byte == '\n':
break
if ( not output.startswith("ok") ) and ( not output.startswith("OK") ):
print "Unexpected serial output:", output.rstrip('\r\n'), "(", ':'.join(x.encode('hex') for x in output), ")"
# Send a command to the device via serial port
# Asynchronous by default - doesn't wait for reply
def sendCmd(self, cmd):
#print "SC:", cmd
self.ser.write(cmd)
self.ser.flush()
# Send a command to the device via serial port
# Waits to receive reply of "ok" or "OK" via waitForOK()
def sendSyncCmd(self, cmd):
#print "SSC:", cmd
self.ser.flushInput()
self.ser.write(cmd)
self.ser.flush()
self.waitForOK()
# Send a command and retrieve the reply
def sendCmdGetReply(self, cmd):
self.ser.flushInput()
self.ser.write(cmd)
self.ser.flush()
return self.getSerOutput()
def availablePorts():
"""Lists 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]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
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