def test_serialcomm_close(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm.close() comm._conn.shutdown.assert_called_with() comm._conn.close.assert_called_with()
def test_serialcomm_read_raw_timeout(): with pytest.raises(IOError): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._conn.read = mock.MagicMock(side_effect=[b"a", b"b", b""]) _ = comm.read_raw(-1)
def test_loopbackcomm_read_raw_2char_terminator(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._conn.read = mock.MagicMock(side_effect=[b"a", b"b", b"c", b"\r", b"\n"]) comm._terminator = "\r\n" eq_(comm.read_raw(), b"abc") comm._conn.read.assert_has_calls([mock.call(1)] * 5) assert comm._conn.read.call_count == 5
def test_loopbackcomm_read_raw_2char_terminator(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._conn.read = mock.MagicMock( side_effect=[b"a", b"b", b"c", b"\r", b"\n"]) comm._terminator = "\r\n" assert comm.read_raw() == b"abc" comm._conn.read.assert_has_calls([mock.call(1)] * 5) assert comm._conn.read.call_count == 5
def test_serialcomm_address(): # Create our communicator comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() port_name = mock.PropertyMock(return_value="/dev/address") type(comm._conn).port = port_name # Check that our address function is working eq_(comm.address, "/dev/address") port_name.assert_called_with()
def test_serialcomm_address(): # Create our communicator comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() port_name = mock.PropertyMock(return_value="/dev/address") type(comm._conn).port = port_name # Check that our address function is working assert comm.address == "/dev/address" port_name.assert_called_with()
def test_serialcomm_terminator(): comm = SerialCommunicator(serial.Serial()) # Default terminator should be \n eq_(comm.terminator, "\n") comm.terminator = "*" eq_(comm.terminator, "*") comm.terminator = "\r\n" eq_(comm.terminator, "\r\n") eq_(comm._terminator, "\r\n")
def test_serialcomm_terminator(): comm = SerialCommunicator(serial.Serial()) # Default terminator should be \n assert comm.terminator == "\n" comm.terminator = "*" assert comm.terminator == "*" comm.terminator = "\r\n" assert comm.terminator == "\r\n" assert comm._terminator == "\r\n"
def test_serialcomm_read_raw(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._conn.read = mock.MagicMock(side_effect=[b"a", b"b", b"c", b"\n"]) eq_(comm.read_raw(), b"abc") comm._conn.read.assert_has_calls([mock.call(1)]*4) assert comm._conn.read.call_count == 4 comm._conn.read = mock.MagicMock() comm.read_raw(10) comm._conn.read.assert_called_with(10)
def test_serialcomm_timeout(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() timeout = mock.PropertyMock(return_value=30) type(comm._conn).timeout = timeout unit_eq(comm.timeout, 30 * u.second) timeout.assert_called_with() comm.timeout = 10 timeout.assert_called_with(10) comm.timeout = 1000 * u.millisecond timeout.assert_called_with(1)
def test_serialcomm_timeout(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() timeout = mock.PropertyMock(return_value=30) type(comm._conn).timeout = timeout unit_eq(comm.timeout, 30 * pq.second) timeout.assert_called_with() comm.timeout = 10 timeout.assert_called_with(10) comm.timeout = 1000 * pq.millisecond timeout.assert_called_with(1)
def new_serial_connection(port, baud=460800, timeout=3, write_timeout=3): """ Return a `pyserial.Serial` connection object for the specified serial port address. The same object will be returned for identical port addresses. This is done for Windows which doesn't like when you have multiple things opening the same serial port. Typically this isn't a problem because you only have one instrument per serial port, but adapters such as the Galvant Industries GPIBUSB adapter can have multiple instruments on a single virtual serial port. :param str port: Port address for the serial port :param int baud: Baud rate for the serial port connection :param int timeout: Communication timeout for reading from the serial port connection. Units are seconds. :param write_timeout: Communication timeout for writing to the serial port connection. Units are seconds. :return: A :class:`SerialCommunicator` object wrapping the connection :rtype: `SerialCommunicator` """ if not isinstance(port, str): raise TypeError('Serial port must be specified as a string.') if port not in serialObjDict or serialObjDict[port] is None: conn = SerialCommunicator( serial.Serial(port, baudrate=baud, timeout=timeout, writeTimeout=write_timeout)) serialObjDict[port] = conn # pylint: disable=protected-access if not serialObjDict[port]._conn.isOpen(): serialObjDict[port]._conn.open() return serialObjDict[port]
def test_serialcomm_query(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm.read = mock.MagicMock(return_value="answer") comm.sendcmd = mock.MagicMock() assert comm._query("mock") == "answer" comm.sendcmd.assert_called_with("mock") comm.read.assert_called_with(-1) comm._query("mock", size=10) comm.read.assert_called_with(10)
def test_serialcomm_query(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm.read = mock.MagicMock(return_value="answer") comm.sendcmd = mock.MagicMock() eq_(comm._query("mock"), "answer") comm.sendcmd.assert_called_with("mock") comm.read.assert_called_with(-1) comm._query("mock", size=10) comm.read.assert_called_with(10)
def test_serialcomm_read_raw(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._conn.read = mock.MagicMock(side_effect=[b"a", b"b", b"c", b"\n"]) assert comm.read_raw() == b"abc" comm._conn.read.assert_has_calls([mock.call(1)] * 4) assert comm._conn.read.call_count == 4 comm._conn.read = mock.MagicMock() comm.read_raw(10) comm._conn.read.assert_called_with(10)
def test_gpibusbcomm_init(): serial_comm = SerialCommunicator(serial.Serial()) serial_comm._conn = mock.MagicMock() serial_comm._query = mock.MagicMock(return_value="1") comm = GPIBCommunicator(serial_comm, 1) assert isinstance(comm._file, SerialCommunicator)
def test_serialcomm_flush_input(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm.flush_input() comm._conn.flushInput.assert_called_with()
def test_serialcomm_tell(): comm = SerialCommunicator(serial.Serial()) comm.tell()
def test_serialcomm_seek(): comm = SerialCommunicator(serial.Serial()) comm.seek(1)
def test_serialcomm_sendcmd(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._sendcmd("mock") comm._conn.write.assert_called_with(b"mock\n")
def test_serialcomm_tell(): with pytest.raises(NotImplementedError): comm = SerialCommunicator(serial.Serial()) comm.tell()
def test_serialcomm_init_wrong_filelike(): _ = SerialCommunicator("derp")
def test_init_mode_serial_comm(mocker): """Test initialization with SerialCommunicator""" comm = SerialCommunicator(serial.Serial()) mock_send = mocker.patch.object(comm, 'sendcmd') ik.srs.SRS830(comm) mock_send.assert_called_with("OUTX 2")
def test_serialcomm_init(): comm = SerialCommunicator(serial.Serial()) assert isinstance(comm._conn, serial.Serial) is True
def test_serialcomm_read_raw_timeout(): comm = SerialCommunicator(serial.Serial()) comm._conn = mock.MagicMock() comm._conn.read = mock.MagicMock(side_effect=[b"a", b"b", b""]) _ = comm.read_raw(-1)
def test_serialcomm_init_wrong_filelike(): with pytest.raises(TypeError): _ = SerialCommunicator("derp")