Source

pyFirmata_nickraptis_mq / FlushInput.diff

# HG changeset patch
# Parent a006ad9770f98dfb6bec9ffba3d310a6fa1a64b7
Flush input between resets to prevent poisoning

diff -r a006ad9770f9 -r bec77585be7f pyfirmata/mockup.py
--- a/pyfirmata/mockup.py	Sun May 22 13:17:07 2011 +0200
+++ b/pyfirmata/mockup.py	Mon May 23 00:03:33 2011 +0300
@@ -52,6 +52,9 @@
     def close(self):
         self.clear()
         
+    def flushInput(self):
+        self.clear()
+        
     def inWaiting(self):
         return len(self)
         
diff -r a006ad9770f9 -r bec77585be7f pyfirmata/pyfirmata.py
--- a/pyfirmata/pyfirmata.py	Sun May 22 13:17:07 2011 +0200
+++ b/pyfirmata/pyfirmata.py	Mon May 23 00:03:33 2011 +0300
@@ -68,16 +68,19 @@
     
     def __init__(self, port, layout, baudrate=57600, name=None):
         self.sp = serial.Serial(port, baudrate)
-        # Allow 5 secs for Arduino's auto-reset to happen
-        # Alas, Firmata blinks it's version before printing it to serial
-        # For 2.3, even 5 seconds might not be enough.
+        # Allow for Arduino's auto-reset to happen
+        # and flush the buffer to prevent poisoning.
         # TODO Find a more reliable way to wait until the board is ready
-        self.pass_time(5)
+        self.pass_time(0.1)
+        self.sp.flushInput()
         self.name = name
         if not self.name:
             self.name = port
         self.setup_layout(layout)
         # Iterate over the first messages to get firmware data
+        # Firmata blinks it's version before printing it to serial
+        # For 2.3, 5 seconds might not be enough.
+        self.pass_time(5)
         while self.bytes_available():
             self.iterate()
         # TODO Test whether we got a firmware name and version, otherwise there