]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - support/sdk/python/tos.py
Fix the issue with tos-deluge not timing out properly.
[tinyos-2.x.git] / support / sdk / python / tos.py
index 83c08ade848d8aacc4b2c05a920b509087597f37..dee4de43af77364e5d3abbcfeb4d61ce6222d280 100644 (file)
@@ -67,15 +67,16 @@ class Timeout(Exception):
 def getSource(comm):
     source = comm.split('@')
     params = source[1].split(':')
+    debug = '--debug' in sys.argv
     if source[0] == 'serial':
         try:
-            return Serial(params[0], int(params[1]), flush=True, debug=('--debug' in sys.argv))
+            return Serial(params[0], int(params[1]), flush=True, debug=debug)
         except:
             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)
+            return SerialMIB600(params[0], int(params[1]), debug=debug)
         except:
             print "ERROR: Unable to initialize a network connection to", comm
             print "ERROR:", traceback.format_exc()
@@ -89,6 +90,11 @@ class Serial:
         self.ackTimeout = ackTimeout
         self._ts = None
 
+        if port.startswith('COM') or port.startswith('com'):
+            port = int(port[3:]) - 1
+        elif port.isdigit():
+            port = int(port) - 1
+
         self._s = serial.Serial(port, int(baudrate), rtscts=0, timeout=0.5)
         self._s.flushInput()
         if flush:
@@ -113,6 +119,7 @@ class Serial:
         #print "DEBUG: putBytes:", data
         for b in data:
             self._s.write(struct.pack('B', b))
+            time.sleep(0.000001)
 
     def getTimeout(self):
         return self._s.timeout
@@ -121,7 +128,7 @@ class Serial:
         self._s.timeout = timeout
 
 class SerialMIB600:
-    def __init__(self, host, port=10002, debug=False, readTimeout=None, ackTimeout=0.05):
+    def __init__(self, host, port=10002, debug=False, readTimeout=None, ackTimeout=0.5):
         self.debug = debug
         self.readTimeout = readTimeout
         self.ackTimeout = ackTimeout
@@ -369,6 +376,7 @@ class SimpleAM(object):
     def write(self, packet, amId, timeout=5, blocking=True, inc=1):
         self.seqno = (self.seqno + inc) % 256
         prevTimeout = self._source.getTimeout()
+        ack = None
         end = None
         if timeout: end = time.time() + timeout
         while not end or time.time() < end:
@@ -395,7 +403,7 @@ class SimpleAM(object):
                 break
         self._source.setTimeout(prevTimeout)
         #print 'SimpleAM:write: got an ack:', ack, ack.seqno == self.seqno
-        return ack.seqno == self.seqno
+        return (ack != None and ack.seqno == self.seqno)
 
     def setOobHook(self, oobHook):
         self.oobHook = oobHook
@@ -440,6 +448,8 @@ class AM(SimpleAM):
         r = super(AM, self).write(packet, amId, timeout, blocking)
         while not r:
             r = super(AM, self).write(packet, amId, timeout, blocking, inc=0)
+            if timeout and not r:
+               raise Timeout
         return True
 
 
@@ -611,7 +621,7 @@ class Packet:
         return self._names
 
     def values(self):
-        return self._names
+        return self._values
 
     # Custom functions
     def names(self):