modbus_new_rtu


 

Function

 

Specifies the COM port to connect to the slave device to communicate with, sets the baudrate, parity, etc., and initializes it as Modbus/RTU.

 

Format

 

ctx = modbus_new_rtu( device , baud , parity , data_bit , stop_bit )

 

Parameters

 

device [ C : char * ] [ Python:ctypes.c_char_p ]

Specify the serial port (COM port) that communicates with the Modbus slave.

 

baud [ C : int ] [ Python : ctypes.c_long ]

Specify the baudrate.

 

parity [ C : char ] [ Python : ctypes.c_char ]

Specify the parity as follows.

 

Definition

Description

N

No Parity

E

Even Parity

O

Odd Parity

 

data_bit [ C : int ] [ Python : ctypes.c_int ]

Specify the data bit as 5-8.

 

stop_bit [ C : int ] [ Python : ctypes.c_int ]

Specify the stop bit as 1-2.

 

Return values

 

ctx [ C : modbus_t * ] [ Python : ctypes.POINTER(modbus_t) ]

If the function succeeds, the pointer address of the modbus_t structure is returned.

 

If the function fails, NULL is returned. The following error is stored in errno.

 

Definition

Description

EINVAL

Parameter is invalid.

ENOMEM

Memory is insufficient.

 

Remarks

 

Specifies the COM port to connect to the slave device to communicate, set the baudrate, parity, etc., and initialize it as Modbus/RTU.

 

Example

 

This is an example of specifying COM1 port, baudrate 115200bps, no parity, data bit 8 and stop bit 1.

 

C

modbus_t *ctx;

ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
 

Python

ctx = ctypes.POINTER(modbus_t)

ctx = modbus_tcp.modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1)