예제 #1
0
    def test_write(self):
        driver = USB1Driver('/dev/ttyS0')
        driver.open()

        msg = ChannelAssignMessage()
        count = driver.write(msg)
        self.assertEqual(self.written_data[-1], msg.encode())
        self.assertTrue(self.serial_flush_called)
예제 #2
0
    def test_write(self):
        self.driver.open()

        # Write calls .encode() on it's argument...
        # It would be better if read and write both worked in terms of
        # bytearrays.
        msg = ChannelAssignMessage()
        self.driver.write(msg)

        self.assertEqual(self.driver.written_data[-1], msg.encode())
        self.assertEqual(self.driver.log.logs[-1], (LOG_WRITE, msg.encode()))

        global dumps
        self.assertEqual(dumps[0], (msg.encode(), 'WRITE'))
예제 #3
0
    def test_write_raises_DriverError_on_SerialTimeoutException(self):
        def serial_write(self, data):
            raise SerialTimeoutException

        Serial.write = serial_write

        driver = USB1Driver('/dev/ttyS0')
        driver.open()

        msg = ChannelAssignMessage()
        with self.assertRaises(DriverError):
            count = driver.write(msg)
예제 #4
0
 def test_writing_to_closed_driver_raises_error(self):
     with self.assertRaises(DriverError):
         msg = ChannelAssignMessage()
         self.driver.write(msg)
예제 #5
0
    def open(self):
        #Setup the logger:
        self.logger = LogWriter(filename=self.outfile, runName=self.runName)
        # Initialize driver
        self.stick = driver.USB1Driver(self.serial,
                                       log=self.logger,
                                       debug=True,
                                       baud_rate=4800)
        self.stick.open()

        # Initialize event machine
        self.evm = event.EventMachine(self.stick)
        self.evm.registerCallback(MyCallback())
        self.evm.start()

        # Reset
        logging.debug("\n\n-------------------------------\n:")
        logging.debug("Setting UP")
        logging.debug("\n\n-------------------------------\n")
        msg = SystemResetMessage()
        self.stick.write(msg.encode())
        time.sleep(1)

        # Set network key
        msg = NetworkKeyMessage(key=NETKEY)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: SETTING NETWORK KEY')
        #            sys.exit()

        # Initialize it as a receiving channel using our network key
        msg = ChannelAssignMessage()
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: INITIALISING AS RECEIVING CHANNEL')
        #            sys.exit()

        # Now set the channel id for pairing with an ANT+ bike cadence/speed sensor
        msg = ChannelIDMessage(device_type=121)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: SETTING CHANNEL ID ')
        #            sys.exit()

        # Listen forever and ever (not really, but for a long time)
        msg = ChannelSearchTimeoutMessage(timeout=255)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UPL LISTENING TIMEOUT')
        #            sys.exit()

        # We want a ~4.05 Hz transmission period
        msg = ChannelPeriodMessage(period=8085)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: TRANSMISSION FREQUENCY')
        #            sys.exit()

        # And ANT frequency 57, of course
        msg = ChannelFrequencyMessage(frequency=57)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: SETTING FREQUENCY')
        #            sys.exit()

        # Time to go live
        msg = ChannelOpenMessage()
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: GOING LIVE')
        #            sys.exit()

        logging.debug("\n\n-------------------------------\n:")
        logging.debug("Listening for ANT events: Press CTRL+C to Exit.")
        logging.debug("\n\n-------------------------------\n")