Browse Source

Timeout: use the instance attribute instead of class constant

Roman Chyla 8 years ago
parent
commit
9167fef8b6
2 changed files with 23 additions and 20 deletions
  1. 3 6
      nodemcu_uploader/main.py
  2. 20 14
      nodemcu_uploader/uploader.py

+ 3 - 6
nodemcu_uploader/main.py

@@ -33,7 +33,7 @@ def destination_from_source(sources):
                 newsources.append(filename)
                 #always use forward slash at destination
                 destinations.append(filename.replace('\\', '/'))
-            
+
     return [newsources, destinations]
 
 
@@ -235,10 +235,10 @@ def main_func():
 
     # let uploader user the default (short) timeout for establishing connection
     uploader = Uploader(args.port, args.baud, start_baud=args.start_baud)
-    
+
     # and reset the timeout (if we have the uploader&timeout)
     if args.timeout:
-        uploader.TIMEOUT = args.timeout
+        uploader.set_timeout(args.timeout)
 
     if args.operation == 'upload':
         operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,
@@ -262,6 +262,3 @@ def main_func():
             uploader.node_restart()
     #no uploader related commands after this point
     uploader.close()
-
-
-

+ 20 - 14
nodemcu_uploader/uploader.py

@@ -8,9 +8,11 @@ import hashlib
 import os
 import serial
 
-from .exceptions import CommunicationTimeout, DeviceNotFoundException, BadResponseException
+from .exceptions import CommunicationTimeout, DeviceNotFoundException, \
+    BadResponseException
 from .utils import default_port, system
-from .luacode import DOWNLOAD_FILE, RECV_LUA, SEND_LUA, LUA_FUNCTIONS, LIST_FILES, UART_SETUP, PRINT_FILE
+from .luacode import DOWNLOAD_FILE, RECV_LUA, SEND_LUA, LUA_FUNCTIONS, \
+    LIST_FILES, UART_SETUP, PRINT_FILE
 
 log = logging.getLogger(__name__)
 
@@ -18,9 +20,9 @@ __all__ = ['Uploader', 'default_port']
 
 SYSTEM = system()
 
-BLOCK_START='\x01';
-NUL = '\x00';
-ACK = '\x06';
+BLOCK_START = '\x01'
+NUL = '\x00'
+ACK = '\x06'
 
 class Uploader(object):
     """Uploader is the class for communicating with the nodemcu and
@@ -31,12 +33,13 @@ class Uploader(object):
     TIMEOUT = 5
     PORT = default_port()
 
-    def __init__(self, port=PORT, baud=BAUD, start_baud=START_BAUD):
+    def __init__(self, port=PORT, baud=BAUD, start_baud=START_BAUD, timeout=TIMEOUT):
+        self.set_timeout(timeout)
         log.info('opening port %s with %s baud', port, start_baud)
         if port == 'loop://':
-            self._port = serial.serial_for_url(port, start_baud, timeout=Uploader.TIMEOUT)
+            self._port = serial.serial_for_url(port, start_baud, timeout=timeout)
         else:
-            self._port = serial.Serial(port, start_baud, timeout=Uploader.TIMEOUT)
+            self._port = serial.Serial(port, start_baud, timeout=timeout)
 
         self.start_baud = start_baud
         self.baud = baud
@@ -48,7 +51,7 @@ class Uploader(object):
 
         def sync():
             # Get in sync with LUA (this assumes that NodeMCU gets reset by the previous two lines)
-            log.debug('getting in sync with LUA');
+            log.debug('getting in sync with LUA')
             self.clear_buffers()
             try:
                 self.exchange(';') # Get a defined state
@@ -78,6 +81,10 @@ class Uploader(object):
             #pySerial 2.7
             self._port.baudrate = baud
 
+    def set_timeout(self, timeout):
+        """Set the timeout for the communication with the device."""
+        timeout = int(timeout) # will raise on Error
+        self._timeout = timeout == 0 and 999999 or timeout
 
     def clear_buffers(self):
         try:
@@ -91,7 +98,7 @@ class Uploader(object):
 
     def expect(self, exp='> ', timeout=None):
         """will wait for exp to be returned from nodemcu or timeout"""
-        timeout = timeout or self.TIMEOUT
+        timeout = timeout or self._timeout
         #do NOT set timeout on Windows
         if SYSTEM != 'Windows':
             timer = self._port.timeout
@@ -137,7 +144,7 @@ class Uploader(object):
     def exchange(self, output, timeout=None):
         self.writeln(output)
         self._port.flush()
-        return self.expect(timeout=timeout or self.TIMEOUT)
+        return self.expect(timeout=timeout or self._timeout)
 
 
     def close(self):
@@ -199,13 +206,13 @@ class Uploader(object):
         log.info('receiveing ' + sent_filename)
 
         #ACK to start download
-        self.write(ACK, True);
+        self.write(ACK, True)
         buffer = ''
         data = ''
         chunk, buffer = self.read_chunk(buffer)
         #read chunks until we get an empty which is the end
         while chunk != '':
-            self.write(ACK,True);
+            self.write(ACK, True)
             data = data + chunk
             chunk, buffer = self.read_chunk(buffer)
         return data
@@ -408,4 +415,3 @@ class Uploader(object):
         res = self.exchange(cmd)
         log.info(res)
         return res
-