コード例 #1
0
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()
コード例 #2
0
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)
コード例 #3
0
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()
コード例 #4
0
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
コード例 #5
0
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
コード例 #6
0
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()
コード例 #7
0
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()
コード例 #8
0
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")
コード例 #9
0
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"
コード例 #10
0
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)
コード例 #11
0
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")
コード例 #12
0
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)
コード例 #13
0
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)
コード例 #14
0
ファイル: serial_manager.py プロジェクト: yinxx/InstrumentKit
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]
コード例 #15
0
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)
コード例 #16
0
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)
コード例 #17
0
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)
コード例 #18
0
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)
コード例 #19
0
def test_serialcomm_flush_input():
    comm = SerialCommunicator(serial.Serial())
    comm._conn = mock.MagicMock()
    comm.flush_input()

    comm._conn.flushInput.assert_called_with()
コード例 #20
0
def test_serialcomm_tell():
    comm = SerialCommunicator(serial.Serial())
    comm.tell()
コード例 #21
0
def test_serialcomm_seek():
    comm = SerialCommunicator(serial.Serial())
    comm.seek(1)
コード例 #22
0
def test_serialcomm_sendcmd():
    comm = SerialCommunicator(serial.Serial())
    comm._conn = mock.MagicMock()

    comm._sendcmd("mock")
    comm._conn.write.assert_called_with(b"mock\n")
コード例 #23
0
def test_serialcomm_sendcmd():
    comm = SerialCommunicator(serial.Serial())
    comm._conn = mock.MagicMock()

    comm._sendcmd("mock")
    comm._conn.write.assert_called_with(b"mock\n")
コード例 #24
0
def test_serialcomm_tell():
    with pytest.raises(NotImplementedError):
        comm = SerialCommunicator(serial.Serial())
        comm.tell()
コード例 #25
0
def test_serialcomm_init_wrong_filelike():
    _ = SerialCommunicator("derp")
コード例 #26
0
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")
コード例 #27
0
def test_serialcomm_init():
    comm = SerialCommunicator(serial.Serial())
    assert isinstance(comm._conn, serial.Serial) is True
コード例 #28
0
def test_serialcomm_tell():
    comm = SerialCommunicator(serial.Serial())
    comm.tell()
コード例 #29
0
def test_serialcomm_flush_input():
    comm = SerialCommunicator(serial.Serial())
    comm._conn = mock.MagicMock()
    comm.flush_input()

    comm._conn.flushInput.assert_called_with()
コード例 #30
0
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)
コード例 #31
0
def test_serialcomm_init_wrong_filelike():
    with pytest.raises(TypeError):
        _ = SerialCommunicator("derp")
コード例 #32
0
def test_serialcomm_seek():
    comm = SerialCommunicator(serial.Serial())
    comm.seek(1)
コード例 #33
0
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)