|
@@ -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
|
|
|
-
|