Bladeren bron

Do not change timeout on windows.

According to [1] so can chaning of timeout corrupt data.

1. http://sourceforge.net/p/pyserial/bugs/178/

Closes #31, Closes #38
kmpm 8 jaren geleden
bovenliggende
commit
9d5de83288
2 gewijzigde bestanden met toevoegingen van 56 en 8 verwijderingen
  1. 10 8
      lib/uploader.py
  2. 46 0
      tests/winserial.py

+ 10 - 8
lib/uploader.py

@@ -8,13 +8,14 @@ import hashlib
 import os
 import serial
 
-from .utils import default_port
+from .utils import default_port, system
 from .luacode import DOWNLOAD_FILE, SAVE_LUA, LUA_FUNCTIONS, LIST_FILES, UART_SETUP, PRINT_FILE
 
 log = logging.getLogger(__name__)
 
 __all__ = ['Uploader', 'default_port']
 
+SYSTEM = system()
 
 class Uploader(object):
     """Uploader is the class for communicating with the nodemcu and
@@ -78,12 +79,13 @@ class Uploader(object):
 
     def expect(self, exp='> ', timeout=TIMEOUT):
         """will wait for exp to be returned from nodemcu or timeout"""
-        timer = self._port.timeout
+        if SYSTEM != 'Windows':
+            timer = self._port.timeout
 
-        # Checking for new data every 100us is fast enough
-        lt = 0.0001
-        if self._port.timeout != lt:
-            self._port.timeout = lt
+            # Checking for new data every 100us is fast enough
+            lt = 0.0001
+            if self._port.timeout != lt:
+                self._port.timeout = lt
 
         end = time.time() + timeout
 
@@ -94,8 +96,8 @@ class Uploader(object):
 
         if time.time() > end and not data.endswith(exp) and len(exp) > 0:
             raise Exception('Timeout expecting ' + exp)
-
-        self._port.timeout = timer
+        if SYSTEM != 'Windows':
+            self._port.timeout = timer
         log.debug('expect returned: `{0}`'.format(data))
         return data
 

+ 46 - 0
tests/winserial.py

@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2015-2016 Peter Magnusson <peter@birchroad.net>
+import unittest
+import os
+import serial
+from lib.utils import default_port
+import time
+
+SERIALPORT = os.environ.get('SERIALPORT', default_port())
+
+def expect(port, timeout, exp='> '):
+    timer = port.timeout
+    # lt = 0.0001
+    # if port.timeout != lt:
+    #     port.timeout = lt
+    end = time.time() + timeout
+
+    data = ''
+    while not data.endswith(exp) and time.time() <= end:
+        data += port.read()
+
+    # port.timeout = timer
+
+    return data
+
+
+
+class WinSerialCase(unittest.TestCase):
+    port = None
+    def setUp(self):
+        self.port = serial.serial_for_url(SERIALPORT, 9600, timeout=5)
+
+    def tearDown(self):
+        self.port.close()
+
+    def test_sync(self):
+        self.port.write('print("%self%");\n'.encode())
+        expect(self.port, 3)
+
+        for i in range(50):
+            self.port.write('print("%self%");\n'.encode())
+            res = expect(self.port, 1)
+            self.assertEqual(res, 'print("%self%");\r\n%self%\r\n> ')
+            time.sleep(0.2)
+
+