# Modbus TCP

## Example

All function codes for Modbus TCP/IP are supported. You can use the client like this:

    #!/usr/bin/env python
    # scripts/examples/simple_tcp_client.py
    import socket

    from umodbus import conf
    from umodbus.client import tcp

    # Enable values to be signed (default is False).
    conf.SIGNED_VALUES = True

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('localhost', 502))

    # Returns a message or Application Data Unit (ADU) specific for doing
    # Modbus TCP/IP.
    message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1])

    # Response depends on Modbus function code. This particular returns the
    # amount of coils written, in this case it is.
    response = tcp.send_message(message, sock)

    sock.close()

## API

umodbus.client.tcp.send_message(adu, sock)

  Send ADU over socket to to server and return parsed response.

  Parameters:

    - adu – Request ADU.

    - sock – Socket instance.

  Returns:

    Parsed response from server.

umodbus.client.tcp.parse_response_adu(resp_adu, req_adu=None)

  Parse response ADU and return response data. Some functions require request ADU to fully understand request ADU.

  Parameters:

    - resp_adu – Resonse ADU.

    - req_adu – Request ADU, default None.

  Returns:

    Response data.

umodbus.client.tcp.read_coils(slave_id, starting_address, quantity)

  Return ADU for Modbus function code 01: Read Coils.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.read_discrete_inputs(slave_id, starting_address, quantity)

  Return ADU for Modbus function code 02: Read Discrete Inputs.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.read_holding_registers(slave_id, starting_address, quantity)

  Return ADU for Modbus function code 03: Read Holding Registers.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.read_input_registers(slave_id, starting_address, quantity)

  Return ADU for Modbus function code 04: Read Input Registers.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.write_single_coil(slave_id, address, value)

  Return ADU for Modbus function code 05: Write Single Coil.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.write_single_register(slave_id, address, value)

  Return ADU for Modbus function code 06: Write Single Register.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.write_multiple_coils(slave_id, starting_address, values)

  Return ADU for Modbus function code 15: Write Multiple Coils.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.

umodbus.client.tcp.write_multiple_registers(slave_id, starting_address, values)

  Return ADU for Modbus function code 16: Write Multiple Registers.

  Parameters:

    slave_id – Number of slave.

  Returns:

    Byte array with ADU.