When the rate of successful acks is low (as it is right now on my machine)
we need tighter timings to make fast progress for bulk uploads (Deluge T2
for example).
"""
import sys, struct, time, socket, operator, os
"""
import sys, struct, time, socket, operator, os
params = source[1].split(':')
if source[0] == 'serial':
try:
params = source[1].split(':')
if source[0] == 'serial':
try:
- return Serial(params[0], int(params[1]), flush=True, debug=False)
+ return Serial(params[0], int(params[1]), flush=True, debug=('--debug' in sys.argv))
- print "ERROR: Unable to initialize serial port connection to", comm
- sys.exit(-1)
+ print "ERROR: Unable to initialize a serial connection to", comm
+ raise Exception
elif source[0] == 'network':
try:
return SerialMIB600(params[0], int(params[1]), debug=False)
except:
elif source[0] == 'network':
try:
return SerialMIB600(params[0], int(params[1]), debug=False)
except:
- print "ERROR: Unable to initialize serial port connection to", comm
- sys.exit(-1)
+ print "ERROR: Unable to initialize a network connection to", comm
+ print "ERROR:", traceback.format_exc()
+ raise Exception
raise Exception
class Serial:
raise Exception
class Serial:
- def __init__(self, port, baudrate, flush=False, debug=False, timeout=None):
+ def __init__(self, port, baudrate, flush=False, debug=False, readTimeout=None, ackTimeout=0.02):
+ self.readTimeout = readTimeout
+ self.ackTimeout = ackTimeout
self._ts = None
self._s = serial.Serial(port, int(baudrate), rtscts=0, timeout=0.5)
self._ts = None
self._s = serial.Serial(port, int(baudrate), rtscts=0, timeout=0.5)
if not self.debug:
sys.stdout.write("\n")
self._s.close()
if not self.debug:
sys.stdout.write("\n")
self._s.close()
- self._s = serial.Serial(port, baudrate, rtscts=0, timeout=timeout)
+ self._s = serial.Serial(port, baudrate, rtscts=0, timeout=readTimeout)
def getByte(self):
c = self._s.read()
def getByte(self):
c = self._s.read()
self._s.timeout = timeout
class SerialMIB600:
self._s.timeout = timeout
class SerialMIB600:
- def __init__(self, host, port=10002, debug=False):
+ def __init__(self, host, port=10002, debug=False, readTimeout=None, ackTimeout=0.05):
+ self.readTimeout = readTimeout
+ self.ackTimeout = ackTimeout
self._ts = None
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._s.connect((host, port))
print "Connected"
def getByte(self):
self._ts = None
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._s.connect((host, port))
print "Connected"
def getByte(self):
+ try:
+ c = self._s.recv(1)
+ except socket.timeout:
+ c = ''
if c == '':
raise Timeout
#print 'Serial:getByte: 0x%02x' % ord(c)
if c == '':
raise Timeout
#print 'Serial:getByte: 0x%02x' % ord(c)
except Timeout:
return None
except Timeout:
return None
- def write(self, payload, seqno, timeout=0.2):
+ def write(self, payload, seqno):
"""
Write a packet. If the payload argument is a list, it is
assumed to be exactly the payload. Otherwise the payload is
"""
Write a packet. If the payload argument is a list, it is
assumed to be exactly the payload. Otherwise the payload is
print s
class SimpleAM(object):
print s
class SimpleAM(object):
- def __init__(self, s, oobHook=None):
- self._s = HDLC(s)
+ def __init__(self, source, oobHook=None):
+ self._source = source
+ self._hdlc = HDLC(source)
self.seqno = 0
self.oobHook = oobHook
def read(self, timeout=None):
self.seqno = 0
self.oobHook = oobHook
def read(self, timeout=None):
- f = self._s.read(timeout)
+ f = self._hdlc.read(timeout)
if f:
return ActiveMessage(NoAckDataFrame(f))
return None
if f:
return ActiveMessage(NoAckDataFrame(f))
return None
- def write(self, packet, amId, timeout=None, blocking=True, inc=1):
+ def write(self, packet, amId, timeout=5, blocking=True, inc=1):
self.seqno = (self.seqno + inc) % 256
self.seqno = (self.seqno + inc) % 256
- while True:
- self._s.write(ActiveMessage(packet, amId=amId), seqno=self.seqno, timeout=timeout)
+ prevTimeout = self._source.getTimeout()
+ end = None
+ if timeout: end = time.time() + timeout
+ while not end or time.time() < end:
+ self._hdlc.write(ActiveMessage(packet, amId=amId), seqno=self.seqno)
if not blocking:
return True
if not blocking:
return True
- f = self._s.read(timeout)
+ start = time.time()
+ f = self._hdlc.read(self._source.ackTimeout)
continue
ack = AckFrame(f)
continue
ack = AckFrame(f)
- while ack.protocol != SERIAL_PROTO_ACK:
+ while ack.protocol != SERIAL_PROTO_ACK and (not end or time.time() < end):
if self.oobHook:
self.oobHook(ActiveMessage(NoAckDataFrame(f)))
else:
print 'SimpleAM:write: skip', ack, f
if self.oobHook:
self.oobHook(ActiveMessage(NoAckDataFrame(f)))
else:
print 'SimpleAM:write: skip', ack, f
- f = self._s.read(timeout)
+ f = self._hdlc.read(self._source.ackTimeout)
break
ack = AckFrame(f)
if f != None:
break
break
ack = AckFrame(f)
if f != None:
break
+ self._source.setTimeout(prevTimeout)
#print 'SimpleAM:write: got an ack:', ack, ack.seqno == self.seqno
return ack.seqno == self.seqno
#print 'SimpleAM:write: got an ack:', ack, ack.seqno == self.seqno
return ack.seqno == self.seqno
except:
try:
s = getSource(os.environ['MOTECOM'])
except:
try:
s = getSource(os.environ['MOTECOM'])
- print "ERROR: please indicate a way to connect to the mote"
+ print "ERROR: Please indicate a way to connect to the mote"
sys.exit(-1)
if oobHook == None:
oobHook = printfHook
sys.exit(-1)
if oobHook == None:
oobHook = printfHook