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 [ VB.NET: String ] [ C, C++: char * ] [ C#: string ] [ Python:ctypes.c_char_p ]

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

When using Number 10 and above in the COM number, please specify in a format such as "\\\\.\\COM10".

Please refer to the following link for details.

https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN

 

baud[ VB.NET: Integer ] [ C, C++: int ] [ C#: int ] [ Python: ctypes.c_long ]

Specify the baudrate.

 

parity [ VB.NET: Sbyte ] [ C, C++: char  ] [ C#: sbyte ] [ Python: ctypes.c_char ]

Specify the parity as follows.

 

Definition

Description

N

No Parity

E

Even Parity

O

Odd Parity

 

data_bit[ VB.NET: Integer ] [ C, C++: int ] [ C#: int ] [ Python: ctypes.c_int ]

Specify the data bit as 5-8.

 

stop_bit[ VB.NET: Integer ] [ C, C++: int ] [ C#: int ] [ Python: ctypes.c_int ]

Specify the stop bit as 1-2.

 

Return values

 

ctx [ VB.NET: IntPtr ] [ C, C++: modbus_t * ] [ C#: IntPtr ] [ 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

IP address 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.

 

VB.NET

Dim ctx As IntPtr

ctx = modbus_new_rtu("COM1", 115200, 'N', 8, 1)

 

C, C++

modbus_t *ctx;

ctx = modbus_new_rtu("COM1", 115200, 'N', 8, 1);
 

C#

IntPtr ctx;

ctx = modbustcp.NewRtu("COM1", 115200, 'N', 8, 1);
 

Python

ctx = ctypes.POINTER(modbus_t)

ctx = modbus_tcp.modbus_new_rtu("COM1", 115200, 'N', 8, 1)