modbus_client.c File Reference

Modbus/TCP client. More...

Go to the source code of this file.

Macros

#define TRACE_LEVEL   MODBUS_TRACE_LEVEL
 

Functions

error_t modbusClientInit (ModbusClientContext *context)
 Initialize Modbus/TCP client context. More...
 
error_t modbusClientSetTimeout (ModbusClientContext *context, systime_t timeout)
 Set timeout value for blocking operations. More...
 
error_t modbusClientSetUnitId (ModbusClientContext *context, uint8_t unitId)
 Set unit identifier. More...
 
error_t modbusClientBindToInterface (ModbusClientContext *context, NetInterface *interface)
 Bind the Modbus/TCP client to a particular network interface. More...
 
error_t modbusClientConnect (ModbusClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
 Establish connection with the Modbus/TCP server. More...
 
error_t modbusClientReadCoils (ModbusClientContext *context, uint16_t address, uint_t quantity, uint8_t *value)
 Read coils. More...
 
error_t modbusClientReadDiscreteInputs (ModbusClientContext *context, uint8_t address, uint_t quantity, uint8_t *value)
 Read discrete inputs. More...
 
error_t modbusClientReadHoldingRegs (ModbusClientContext *context, uint16_t address, uint_t quantity, uint16_t *value)
 Read holding registers. More...
 
error_t modbusClientReadInputRegs (ModbusClientContext *context, uint16_t address, uint_t quantity, uint16_t *value)
 Read input registers. More...
 
error_t modbusClientWriteSingleCoil (ModbusClientContext *context, uint16_t address, bool_t value)
 Write single coil. More...
 
error_t modbusClientWriteSingleReg (ModbusClientContext *context, uint16_t address, uint16_t value)
 Write single register. More...
 
error_t modbusClientWriteMultipleCoils (ModbusClientContext *context, uint16_t address, uint_t quantity, const uint8_t *value)
 Write multiple coils. More...
 
error_t modbusClientWriteMultipleRegs (ModbusClientContext *context, uint16_t address, uint_t quantity, const uint16_t *value)
 Write multiple registers. More...
 
error_t modbusClientMaskWriteReg (ModbusClientContext *context, uint16_t address, uint16_t andMask, uint16_t orMask)
 Apply AND/OR bitmask to a register. More...
 
error_t modbusClientReadWriteMultipleRegs (ModbusClientContext *context, uint16_t readAddress, uint16_t readQuantity, uint16_t *readValue, uint16_t writeAddress, uint16_t writeQuantity, const uint16_t *writeValue)
 Read/write multiple registers. More...
 
error_t modbusClientGetExceptionCode (ModbusClientContext *context, ModbusExceptionCode *exceptionCode)
 Retrieve exception code. More...
 
error_t modbusClientDisconnect (ModbusClientContext *context)
 Disconnect from the Modbus/TCP server. More...
 
void modbusClientDeinit (ModbusClientContext *context)
 Release Modbus/TCP client context. More...
 

Detailed Description

Modbus/TCP client.

License

Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
1.9.0

Definition in file modbus_client.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   MODBUS_TRACE_LEVEL

Definition at line 30 of file modbus_client.c.

Function Documentation

◆ modbusClientBindToInterface()

error_t modbusClientBindToInterface ( ModbusClientContext context,
NetInterface interface 
)

Bind the Modbus/TCP client to a particular network interface.

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]interfaceNetwork interface to be used
Returns
Error code

Definition at line 123 of file modbus_client.c.

◆ modbusClientConnect()

error_t modbusClientConnect ( ModbusClientContext context,
const IpAddr serverIpAddr,
uint16_t  serverPort 
)

Establish connection with the Modbus/TCP server.

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]serverIpAddrIP address of the server to connect to
[in]serverPortTCP port number that will be used
Returns
Error code

Definition at line 146 of file modbus_client.c.

◆ modbusClientDeinit()

void modbusClientDeinit ( ModbusClientContext context)

Release Modbus/TCP client context.

Parameters
[in]contextPointer to the Modbus/TCP client context

Definition at line 992 of file modbus_client.c.

◆ modbusClientDisconnect()

error_t modbusClientDisconnect ( ModbusClientContext context)

Disconnect from the Modbus/TCP server.

Parameters
[in]contextPointer to the Modbus/TCP client context
Returns
Error code

Definition at line 965 of file modbus_client.c.

◆ modbusClientGetExceptionCode()

error_t modbusClientGetExceptionCode ( ModbusClientContext context,
ModbusExceptionCode exceptionCode 
)

Retrieve exception code.

Parameters
[in]contextPointer to the Modbus/TCP client context
[out]exceptionCodeException code
Returns
Error code

Definition at line 944 of file modbus_client.c.

◆ modbusClientInit()

error_t modbusClientInit ( ModbusClientContext context)

Initialize Modbus/TCP client context.

Parameters
[in]contextPointer to the Modbus/TCP client context
Returns
Error code

Definition at line 48 of file modbus_client.c.

◆ modbusClientMaskWriteReg()

error_t modbusClientMaskWriteReg ( ModbusClientContext context,
uint16_t  address,
uint16_t  andMask,
uint16_t  orMask 
)

Apply AND/OR bitmask to a register.

This function code is used to modify the contents of a specified holding register using a combination of an AND mask, an OR mask, and the register's current contents. The function can be used to set or clear individual bits in the register

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressAddress of the holding register
[in]andMaskAND bitmask
[in]orMaskOR bitmask
Returns
Error code

Definition at line 811 of file modbus_client.c.

◆ modbusClientReadCoils()

error_t modbusClientReadCoils ( ModbusClientContext context,
uint16_t  address,
uint_t  quantity,
uint8_t *  value 
)

Read coils.

This function code is used to read from 1 to 2000 contiguous status of coils in a remote device. The request specifies the starting address and the number of coils

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressAddress of the first coil
[in]quantityNumber of coils
[out]valueValue of the discrete outputs
Returns
Error code

Definition at line 280 of file modbus_client.c.

◆ modbusClientReadDiscreteInputs()

error_t modbusClientReadDiscreteInputs ( ModbusClientContext context,
uint8_t  address,
uint_t  quantity,
uint8_t *  value 
)

Read discrete inputs.

This function code is used to read from 1 to 2000 contiguous status of discrete inputs in a remote device. The request specifies the starting address and the number of inputs

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressAddress of the first input
[in]quantityNumber of inputs
[out]valueValue of the discrete inputs
Returns
Error code

Definition at line 348 of file modbus_client.c.

◆ modbusClientReadHoldingRegs()

error_t modbusClientReadHoldingRegs ( ModbusClientContext context,
uint16_t  address,
uint_t  quantity,
uint16_t *  value 
)

Read holding registers.

This function code is used to read the contents of a contiguous block of holding registers in a remote device. The request specifies the starting register address and the number of registers

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressStarting register address
[in]quantityNumber of registers
[out]valueValue of the holding registers
Returns
Error code

Definition at line 416 of file modbus_client.c.

◆ modbusClientReadInputRegs()

error_t modbusClientReadInputRegs ( ModbusClientContext context,
uint16_t  address,
uint_t  quantity,
uint16_t *  value 
)

Read input registers.

This function code is used to read from 1 to 125 contiguous input registers in a remote device. The request specifies the starting register address and the number of registers

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressStarting register address
[in]quantityNumber of registers
[out]valueValue of the input registers
Returns
Error code

Definition at line 484 of file modbus_client.c.

◆ modbusClientReadWriteMultipleRegs()

error_t modbusClientReadWriteMultipleRegs ( ModbusClientContext context,
uint16_t  readAddress,
uint16_t  readQuantity,
uint16_t *  readValue,
uint16_t  writeAddress,
uint16_t  writeQuantity,
const uint16_t *  writeValue 
)

Read/write multiple registers.

This function code performs a combination of one read operation and one write operation in a single Modbus transaction. The write operation is performed before the read

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]readAddressAddress of the first holding registers to be read
[in]readQuantityNumber of holding registers to be read
[out]readValueValue of the holding registers (read operation)
[in]writeAddressAddress of the first holding registers to be written
[in]writeQuantityNumber of holding registers to be written
[in]writeValueValue of the holding registers (write operation)
Returns
Error code

Definition at line 878 of file modbus_client.c.

◆ modbusClientSetTimeout()

error_t modbusClientSetTimeout ( ModbusClientContext context,
systime_t  timeout 
)

Set timeout value for blocking operations.

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]timeoutTimeout value, in milliseconds
Returns
Error code

Definition at line 81 of file modbus_client.c.

◆ modbusClientSetUnitId()

error_t modbusClientSetUnitId ( ModbusClientContext context,
uint8_t  unitId 
)

Set unit identifier.

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]unitIdIdentifier of the remote slave
Returns
Error code

Definition at line 102 of file modbus_client.c.

◆ modbusClientWriteMultipleCoils()

error_t modbusClientWriteMultipleCoils ( ModbusClientContext context,
uint16_t  address,
uint_t  quantity,
const uint8_t *  value 
)

Write multiple coils.

This function code is used to force each coil in a sequence of coils to either ON or OFF in a remote device. The request specifies the starting address, the number of outputs and the requested ON/OFF states

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressAddress of the first coil to be forced
[in]quantityNumber of coils
[in]valueValue of the discrete outputs
Returns
Error code

Definition at line 674 of file modbus_client.c.

◆ modbusClientWriteMultipleRegs()

error_t modbusClientWriteMultipleRegs ( ModbusClientContext context,
uint16_t  address,
uint_t  quantity,
const uint16_t *  value 
)

Write multiple registers.

This function code is used to write a block of contiguous registers (1 to 123 registers) in a remote device. The request specifies the starting address, the number of registers and the requested written values

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressStarting register address
[in]quantityNumber of registers
[in]valueValue of the holding registers
Returns
Error code

Definition at line 742 of file modbus_client.c.

◆ modbusClientWriteSingleCoil()

error_t modbusClientWriteSingleCoil ( ModbusClientContext context,
uint16_t  address,
bool_t  value 
)

Write single coil.

This function code is used to write a single output to either ON or OFF in a remote device. The request specifies the address of the coil to be forced and the requested ON/OFF state

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressAddress of the coil to be forced
[in]valueValue of the discrete output
Returns
Error code

Definition at line 551 of file modbus_client.c.

◆ modbusClientWriteSingleReg()

error_t modbusClientWriteSingleReg ( ModbusClientContext context,
uint16_t  address,
uint16_t  value 
)

Write single register.

This function code is used to write a single holding register in a remote device. The request specifies the address of the register to be written and the register value

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]addressAddress of the register to be written
[in]valueRegister value
Returns
Error code

Definition at line 612 of file modbus_client.c.