changeset 6:ca63391a2214

redo lots of serial calls
author drewp@bigasterisk.com
date Sun, 05 Feb 2023 14:28:57 -0800
parents 696a46a1b239
children 25f0c04e1654
files telemetrix_local.py
diffstat 1 files changed, 47 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/telemetrix_local.py	Sun Feb 05 14:26:04 2023 -0800
+++ b/telemetrix_local.py	Sun Feb 05 14:28:57 2023 -0800
@@ -356,8 +356,8 @@
                 await self.shutdown()
             raise RuntimeError
         else:
-            if firmware_version[2] < 5:
             log.debug(f'p3')
+            if firmware_version[1] < 5:
                 raise RuntimeError('Please upgrade the server firmware to version 5.0.0 or greater')
             log.info(f'Telemetrix4Arduino Version Number: {firmware_version[1]}.'
                      f'{firmware_version[2]}.{firmware_version[3]}')
@@ -412,9 +412,8 @@
                 continue
             print('\nChecking {}'.format(port.device))
             try:
-                self.serial_port = TelemetrixAioSerial(port.device, 115200,
-                                                       telemetrix_aio_instance=self,
-                                                       close_loop_on_error=self.close_loop_on_shutdown)
+                self.serial_port = TelemetrixAioSerial(port.device, 115200)
+                await self.serial_port.open()
             except SerialException:
                 continue
             # create a list of serial ports that we opened
@@ -423,9 +422,6 @@
             # display to the user
             print('\t' + port.device)
 
-            # clear out any possible data in the input buffer
-            await self.serial_port.reset_input_buffer()
-
         # wait for arduino to reset
         print('\nWaiting {} seconds(arduino_wait) for Arduino devices to '
               'reset...'.format(self.arduino_wait))
@@ -438,45 +434,44 @@
             self.serial_port = serial_port
 
             command = [PrivateConstants.ARE_U_THERE]
-            await self._send_command(command)
-            # provide time for the reply
-            await asyncio.sleep(.1)
-
-            i_am_here = await self.serial_port.read(3)
-
-            if not i_am_here:
+            try:
+                i_am_here = await asyncio.wait_for(self._send_command(command, has_response=True), timeout=0.5)
+            except asyncio.TimeoutError:
+                log.info(f'no answer- retrying')
                 continue
-
+            
             # got an I am here message - is it the correct ID?
             if i_am_here[2] == self.arduino_instance_id:
                 self.com_port = serial_port.com_port
                 return
+        raise ValueError("not found")
 
     async def _manual_open(self):
         """
         Com port was specified by the user - try to open up that port
 
         """
-        # if port is not found, a serial exception will be thrown
-        print('Opening {} ...'.format(self.com_port))
-        self.serial_port = TelemetrixAioSerial(self.com_port, 115200,
-                                               telemetrix_aio_instance=self,
-                                               close_loop_on_error=self.close_loop_on_shutdown)
-
-        print('Waiting {} seconds for the Arduino To Reset.'
-              .format(self.arduino_wait))
-        await asyncio.sleep(self.arduino_wait)
-        command = [PrivateConstants.ARE_U_THERE]
-        await self._send_command(command)
-        # provide time for the reply
-        await asyncio.sleep(.1)
-
-        print(f'Searching for correct arduino_instance_id: {self.arduino_instance_id}')
-        i_am_here = await self.serial_port.read(3)
+        log.info(f'Searching for correct arduino_instance_id: {self.arduino_instance_id}')
+        while True:
+            # if port is not found, a serial exception will be thrown
+            log.info('Opening {} ...'.format(self.com_port))
+            self.serial_port = TelemetrixAioSerial(self.com_port, 115200)
+            # log.debug('await open')
+            await self.serial_port.open()
+
+            # print('Waiting {} seconds for the Arduino To Reset.'
+            #       .format(self.arduino_wait))
+            # await asyncio.sleep(self.arduino_wait)
+            command = [PrivateConstants.ARE_U_THERE]
+            try:
+                i_am_here = await asyncio.wait_for(self._send_command(command, has_response=True), timeout=0.5)
+            except asyncio.TimeoutError:
+                log.info(f'no answer- retrying')
+                continue
+            break          
 
         if not i_am_here:
-            print(f'ERROR: correct arduino_instance_id not found')
-
+            log.warning(f'ERROR: correct arduino_instance_id not found')
 
         log.info('Correct arduino_instance_id found\n')
 
@@ -487,13 +482,10 @@
         :returns: Firmata firmware version
         """
         command = [PrivateConstants.GET_FIRMWARE_VERSION]
-        await self._send_command(command)
-        # provide time for the reply
-        await asyncio.sleep(.1)
-        if not self.ip_address:
-            firmware_version = await self.serial_port.read(5)
-        else:
-            firmware_version = list(await self.sock.read(5))
+        log.debug('GET_FIRMWARE_VERSION')
+        firmware_version = await self._send_command(command, has_response=True)
+        log.debug(f'{firmware_version=}\n')
+        
         return firmware_version
 
     async def analog_write(self, pin, value):
@@ -1443,10 +1435,10 @@
                     await self.shutdown()
                 raise RuntimeError('Unknown pin state')
 
-        if command:
-            await self._send_command(command)
 
         await asyncio.sleep(.05)
+        await self._send_command(command)
+
 
     async def servo_detach(self, pin_number):
         """
@@ -2505,13 +2497,19 @@
 
         :returns: number of bytes sent
         """
-        # the length of the list is added at the head
-        command.insert(0, len(command))
-        # print(command)
-        send_message = bytes(command)
-
-        if not self.ip_address:
-            await self.serial_port.write(send_message)
+        if self.serial_port is None: 
+            raise TypeError
+        if command:
+            # the length of the list is added at the head
+            command.insert(0, len(command))
+            # print(command)
+            send_message = bytes(command)
+            log.debug(f'sending {CommandName[command[1]]} {command[2:]}')
+            if not self.ip_address:
+                await self.serial_port.write(send_message)
+            else:
+                await self.sock.write(send_message)
+                # await asyncio.sleep(.1)
         else:
             await self.sock.write(send_message)
             # await asyncio.sleep(.1)