Modbus Master -slave通讯

问题描述 投票:3回答:1

要求:通过Modbus从Serial port-1收集数据,在Serial port-2上镜像相同。

我能够在主服务器和从服务器之间进行通信,但无法将数据镜像到串行端口2.请建议我如何实现这一点。

代码:

Master : 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>   /* File Control Definitions           */
#include <termios.h> /* POSIX Terminal Control Definitions */
#include <unistd.h>  /* UNIX Standard Definitions      */
#include <errno.h>   /* ERROR Number Definitions           */
#include <sys/ioctl.h>
#include "modbus.h"

int main()
{
    uint8_t req[MODBUS_RTU_MAX_ADU_LENGTH];// request buffer
    int len;// length of the request/response
    printf("Modbus server example.\n");

    //Create a new RTU context with proper serial parameters (in this example,
    //device name /dev/ttyS0, baud rate 9600, no parity bit, 8 data bits, 1 stop bit)
    modbus_t *ctx = modbus_new_rtu("/dev/ttyUSB3", 9600, 'N', 8, 1);
    if (!ctx) {
        fprintf(stderr, "Failed to create the context: %s\n", modbus_strerror(errno));
        exit(1);
    }

    if (modbus_connect(ctx) == -1) {
        fprintf(stderr, "Unable to connect: %s\n", modbus_strerror(errno));
        modbus_free(ctx);
        exit(1);
    }
    else
    {
        printf("Modbus server connected successfully\n");
    }

    //Set the Modbus address of the remote slave (to 3)
    int rc=modbus_set_slave(ctx, 3);
    printf("Modbus set remote slave return code : %d\n",rc);


    uint16_t reg[5];// will store read registers values

    //Read 5 holding registers starting from address 10
    int num = modbus_read_registers(ctx, 10, 5, reg);
    printf("Result of modbus_read_registers : %d\n",num);
    for(int i=10;i<=15;i++)
    {
         printf("reg [%d] : %X\t",i,reg[i]);
    }
    printf("\n");
        if (num != 5) 
    {// number of read registers is not the one expected
        fprintf(stderr, "Failed to read: %s\n", modbus_strerror(errno));
    }



    modbus_close(ctx);
    modbus_free(ctx);
}

================================================== ==============奴隶:

#include <stdio.h>
#include <fcntl.h>   /* File Control Definitions           */
#include <termios.h> /* POSIX Terminal Control Definitions */
#include <unistd.h>  /* UNIX Standard Definitions      */
#include <errno.h>   /* ERROR Number Definitions           */
#include <sys/ioctl.h>
#include "modbus/modbus.h"
int main()
{

    int len1=-1;
    printf("Modbus slave example");
    //Prepare a Modbus mapping with 30 holding registers
    //(plus no output coil, one input coil and two input registers)
    //This will also automatically set the value of each register to 0
    modbus_mapping_t *mapping = modbus_mapping_new(0, 1, 30, 2);
    if (!mapping) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
        exit(1);
    }
    else
    {
        printf("Mapping allocated successfully.\n");
    }


    //Example: set register 12 to integer value 623
    mapping->tab_registers[12] = 623;


    modbus_t *ctx = modbus_new_rtu("/dev/ttyHSL1", 9600, 'N', 8, 1);
    if (!ctx) {
        fprintf(stderr, "Failed to create the context: %s\n", modbus_strerror(errno));
        exit(1);
    }

    //Set the Modbus address of this slave (to 3)
   int rc= modbus_set_slave(ctx, 3);
   printf("Result code of Modbus set slave address :%d\n",rc);


    if (modbus_connect(ctx) == -1) {
        fprintf(stderr, "Unable to connect: %s\n", modbus_strerror(errno));
        modbus_free(ctx);
        exit(1);
    }
    else
    {
        printf("Modbus slave connected successfully\n");
    }


    uint8_t req[MODBUS_RTU_MAX_ADU_LENGTH];// request buffer
    int len;// length of the request/response

    while(1) {
        len = modbus_receive(ctx, req);
        printf("Modbus receive len : %d\n",len);
        if (len == -1)
            break;

        len1 = modbus_reply(ctx, req, len, mapping);
        printf("req: %X\n",*req);
        printf("len : %d\n",len);
        printf("mapping->tab_registers[12] : %d\n",mapping->tab_registers[12]);

        printf("Result Modbus reply len : %d\n",len1);
        if (len == -1) break;
    }
    printf("Exit the loop: %s\n", modbus_strerror(errno));

    modbus_mapping_free(mapping);
    modbus_close(ctx);
    modbus_free(ctx);
}

o / p:大师:

shilpa@RT:~/Desktop/Modbus_master/Modbus_masterComponent/src$ sudo ./Modbus_masterComponent 
Modbus server example.
Modbus server connected successfully
Modbus set remote slave return code : 0
Result of modbus_read_registers : 5
reg [10] : 0    reg [11] : 0    reg [12] : AAC6 reg [13] : CDB5 reg [14] : 7F6D reg [15] : 0

奴隶 :

root@swi-mdm9x15:~# /legato/systems/current/apps/Modbus_slave/read-only/bin/modbus_slave 
Modbus slave exampleMapping allocated successfully.
Result code of Modbus set slave address :0
Modbus slave connected successfully
Modbus receive len : 8
req: 3
len : 8
mapping->tab_registers[12] : 623
Result Modbus reply len : 15
c embedded modbus rs485
1个回答
0
投票

您不能像在for循环中那样简单地从10到15索引5个插槽数组。没错。它的索引将从0到4。

uint16_t reg[5];// will store read registers values

//Read 5 holding registers starting from address 10
int num = modbus_read_registers(ctx, 10, 5, reg);
printf("Result of modbus_read_registers : %d\n",num);
for(int i=10;i<=15;i++)
{
     printf("reg [%d] : %X\t",i,reg[i]);
}

For循环应该是这样的。并且%X用于以十六进制打印事物。如果你想看到存储在主机侧以十进制形式存储在从机侧的相同623(十进制),则使用%d,如下所示。

for(int i=0;i<5;i++)
{
     printf("reg [%d] : %d\t",i+10, reg[i]);
}
© www.soinside.com 2019 - 2024. All rights reserved.