Understanding of Modbus RTU Program development

Source: Internet
Author: User

Modbus is a good serial communication protocol, in which RTU protocol is most commonly used, through the Modbus RTU, we can easily read and write serial information.

Download Libmodbus from official website and observe modbus.h that the key structure is:

typedef struct {
int nb_bits;
int nb_input_bits;
int nb_input_registers;
int nb_registers;
uint8_t *tab_bits;
uint8_t *tab_input_bits;
uint16_t *tab_input_registers;
uint16_t *tab_registers;
} modbus_mapping_t;

Several of the main configuration functions in modbus-rtu.h use the struct.

The general steps of the RTU operation are:

1, with the specified serial port to create a new connection, Windows system, the default serial port for the Com1,linux system is the default serial port is/DEV/TTYS0;

2, set the frequency, bit, check digit, such as price;

3, set the time-out period;

4, read or write operation, pay attention to first set Slave:modbus_set_slave (M_modbus, 0);

5. Close the connection and cancel the device binding.

The main operating functions are:

EXPORT int modbus_read_bits (modbus_t *ctx, int addr, int nb, uint8_t *dest); //Read serial data
EXPORT int modbus_read_input_bits (modbus_t *ctx, int addr, int nb, uint8_t *dest); //Read input port serial data
EXPORT int Modbus_write_bit (modbus_t *ctx, int coil_addr, int status); //Specify location write operations
EXPORT int modbus_write_bits (modbus_t *ctx, int addr, int nb, const uint8_t *data); //write multiple ports

A simple example:

int main (int argc, char * argv[])
{

modbus_t * M_modbus;
int i, addr, act;
int ret = 0;
M_modbus = Modbus_new_rtu ("/dev/ttys0", 9600, ' N ', 8, 1);Baud rate 9600, bit 8, check bit 1, no price
Modbus_set_debug (M_modbus, 0);Set to 1 to see debug information
struct Timeval response_timeout;
Response_timeout.tv_sec = 1;
response_timeout.tv_usec = 0;
Modbus_set_response_timeout (m_modbus,&response_timeout);Set wait delay time
if (M_modbus && modbus_connect (m_modbus) = =-1) {
printf ("\nsorry,fail to connect ttys0,please check your device ' s state.\n");
}
else {
printf ("Connected to dev!\n");
connected = 1;
}

if (M_modbus = = NULL)
{
printf ("No Connection,check your device!\n");
Return
}
/* The code in the comments is to read 4 port data from the serial ports
uint8_t Dest[4]; Setup memory for data
uint16_t * dest16 = (uint16_t *) dest;
memset (dest, 0, 4);
*/
Modbus_set_slave (M_modbus, 0);
/* The comment code for this paragraph assigns a value to the specified port
ret = modbus_read_bits (M_modbus, 0, 4, dest);
printf ("%d\n", ret);
for (i = 0; i < 4; i++)
{
printf ("\n%d:%4d", I,dest[i]);
}
for (i = 0; i < 4; i++)
{
ret = Modbus_write_bit (M_modbus, I, 1);
if (ret)
{
printf ("Write Data successfully!") return value:%d\n ", ret);
}
Else
{
printf ("Write data failed, return value:%d", ret);
}
Sleep (2);
}
*/
Disconnect Connection
if (M_modbus) {
Modbus_close (M_modbus);
Modbus_free (M_modbus);
M_modbus = NULL;
}
printf ("Disconnect succeed!\n");
return 0;
}

This information is derived from personal summary, if reproduced, please indicate the source, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.