예제 #1
0
def test_instrument_open_serial_by_usb_ids_multiple_matches(_, mock_comports):
    fake_device = ListPortInfo()
    fake_device.vid = 0
    fake_device.pid = 1000
    fake_device.serial_number = 'a1'
    fake_device.device = 'COM1'

    fake_device2 = ListPortInfo()
    fake_device2.vid = 0
    fake_device2.pid = 1000
    fake_device2.serial_number = 'b2'
    fake_device2.device = 'COM2'

    mock_comports.return_value = [fake_device, fake_device2]

    _ = ik.Instrument.open_serial(baud=1234, vid=0, pid=1000)
예제 #2
0
def fake_comports():
    """
    Generate a fake list of comports to compare against.
    """
    fake_device = ListPortInfo()
    fake_device.vid = 0
    fake_device.pid = 1000
    fake_device.serial_number = 'a1'
    fake_device.device = 'COM1'

    fake_device2 = ListPortInfo()
    fake_device2.vid = 1
    fake_device2.pid = 1010
    fake_device2.serial_number = 'c0'
    fake_device2.device = 'COM2'
    return [fake_device, fake_device2]
    def test_args_vendor_not_empty(self):
        """
        checks to make sure that the is_serial_usb function
        exists correctly when the args["vendor"] is not empty
        and port["manufacturer"] doesn't start with args["vendor"]
        """
        port, args = ListPortInfo(), Namespace()
        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"

        args.vid = None
        args.pid = None
        args.vendor = "Microsoft"

        response = is_usb_serial(port, args)
        self.assertFalse(response)
예제 #4
0
    def ftdiPorts(cls):
        # FIXME: for some reason, I can't get the Sutter URls with the
        # wildcard and I must handcode the following. It appears the wildcard
        # ftdi:///? does not work for some reason.
        vidpids = [(4930, 1)] # Must add custom pairs in the future.

        urls = []        
        for vid, pid in vidpids:
            pyftdidevices = Ftdi.list_devices(url="ftdi://{0}:{1}/1".format(vid, pid))
            for device, address in pyftdidevices:
                thePort = ListPortInfo(device="")
                thePort.vid = device.vid
                thePort.pid = device.pid
                thePort.serial_number = device.sn
                thePort.device = "ftdi://{0}:{1}:{2}/{3}".format(device.vid, device.pid, device.sn, address)
                urls.append(thePort)

        return urls
예제 #5
0
    def test_listen_exception(self, mock_port_list, mock_serial, mock_json):
        """
        tests the listen method with invalid input
        """
        port = ListPortInfo()
        port.vid = "vid"
        port.pid = "pid"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        mock_json.side_effect = Exception("ex")

        mock_port_list.return_value = [port]

        test_input = "{'value': 'value'}"
        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "usb",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            with LogCapture() as capture:
                transceiver = Transceiver(log_file_name="LOG_FILE")

                mock_receiver = MagicMock()
                mock_receiver.readline.return_value.decode.return_value = test_input
                transceiver.serial = mock_receiver

                with self.assertRaises(Exception):
                    transceiver.listen()

                capture.check(
                    ("LOG_FILE", "INFO", "Port device found: usb"),
                    ("LOG_FILE", "INFO", "Opening serial on: usb"),
                    ("LOG_FILE", "ERROR", "error occurred: ex"),
                )
예제 #6
0
    def test_send(self, mock_port_list, mock_serial):
        """
        tests the send method
        """
        port = ListPortInfo()
        port.vid = "vid"
        port.pid = "pid"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        mock_port_list.return_value = [port]

        test_payload = {"value": "value"}

        output = get_serial_stream(test_payload)

        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "usb",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            with LogCapture() as capture:
                transciever = Transceiver(log_file_name="LOG_FILE")

                mock_serial_sender = MagicMock()
                transciever.serial = mock_serial_sender

                transciever.send(test_payload)
                mock_serial_sender.write.assert_called_with(output)
                capture.check(
                    ("LOG_FILE", "INFO", "Port device found: usb"),
                    ("LOG_FILE", "INFO", "Opening serial on: usb"),
                    ("LOG_FILE", "INFO", "sending"),
                    ("LOG_FILE", "INFO", "{'value': 'value'}"),
                )
    def test_pass(self):
        """
        insure that is_serial_usb returns true if all test cases haven't
        failed
        """
        port, args = ListPortInfo(), Namespace()

        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"
        port.serial_number = "456"
        port.interface = "bar"

        args.vid = None
        args.pid = None
        args.vendor = None
        args.serial = None
        args.intf = None

        response = is_usb_serial(port, args)
        self.assertTrue(response)
    def test_args_intf_not_empty(self):
        """
        checks to make sure that the is_serial_usb function
        exists correctly when the args["serial"] is not empty
        and port["interface"] is none
        """
        port, args = ListPortInfo(), Namespace()

        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"
        port.serial_number = "456"
        port.interface = None

        args.vid = None
        args.pid = None
        args.vendor = None
        args.serial = None
        args.intf = "foo"

        response = is_usb_serial(port, args)
        self.assertFalse(response)