0

在决定尝试与工作中的某些设备交谈后,我正试图围绕套接字的工作原理。基本细节是设备使用基于 UDP 的 ModBus RTU 协议。数据表说设备在端口 2001 上侦听并回复端口 2000。在尝试找到一些示例代码后,我找到了一个 modbus 库并设法让它与一些发送和接收似乎相同的模拟器一起工作港口。但是,我似乎无法让它与我的设备一起使用。我认为问题与端口有关。我看了一遍,似乎找不到任何有用的东西(也许我只是没有足够好的理解)。这是我正在使用的代码,有没有人有任何指针。

//------------------------------------------------------------------------------
// Copyright (C) 2010, Raditex AB
// All rights reserved.
//
// FreeSCADA
// http://www.FreeSCADA.com
// freescada@freescada.com
//
//------------------------------------------------------------------------------

#include "modbus.h"
#include "modbus-udp.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

//------------------------------------------------------------------------------
//
//
//------------------------------------------------------------------------------
int
modbus_udp_close(modbus_udp_handle_t *handle)
{
    if (handle == NULL)
        return -1;

    close(handle->sock);

    return 0;
}

//------------------------------------------------------------------------------
//
//
//------------------------------------------------------------------------------
int
modbus_udp_init(char *host, int port, modbus_udp_handle_t *handle, int delay)
{
    struct timeval timeout;

    if (handle == NULL)
        return -1;

    if ((handle->sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        snprintf(modbus_error_str, sizeof(modbus_error_str), "%s: couldn't get socket: %s",
                 __PRETTY_FUNCTION__, strerror(errno));
        return -1;
    }

    timeout.tv_sec  = delay;
    timeout.tv_usec = 0;

    if (setsockopt(handle->sock, SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(struct timeval)) == -1)
    {
        snprintf(modbus_error_str, sizeof(modbus_error_str), "%s: couldn't set receive timeout: %s.",
                 __PRETTY_FUNCTION__, strerror(errno));
        return -1;
    }

    /*
     if ((flags = fcntl(handle->sock, F_GETFL, 0)) == -1)
     {
     snprintf(modbus_error_str, sizeof(modbus_error_str),
     "%s: couldn't get fd option non-blocking: F_GETFL.", __PRETTY_FUNCTION__);
     return NULL;
     }

     if (fcntl(io->sock, F_SETFL, flags|O_NONBLOCK) == -1)
     {
     snprintf(modbus_error_str, sizeof(modbus_error_str),
     "%s: couldn't set option non-blocking: F_SETFL.", __PRETTY_FUNCTION__);
     return NULL;
     }
     */

    handle->saddr.sin_family = AF_INET;

    if ((handle->addr = gethostbyname(host)) == NULL)
    {
        snprintf(modbus_error_str, sizeof(modbus_error_str), "%s: couldn't get host: %s: %s",
                 __PRETTY_FUNCTION__, strerror(errno), host);
        return -1;
    }


    bcopy((char *) handle->addr->h_addr,
          (char *)&handle->saddr.sin_addr,
          handle->addr->h_length);

    handle->saddr.sin_port = htons(port);

    return 0;
}


//------------------------------------------------------------------------------
//
//
//------------------------------------------------------------------------------
int
modbus_udp_send(modbus_udp_handle_t *handle, modbus_packet_t *pkt)
{
    char buff[256];
    int len;

    if (pkt == NULL)
        return -1;

    len = modbus_packet_pack(pkt, buff, sizeof(buff));

    if (sendto(handle->sock, buff, len, 0, (struct sockaddr *)&handle->saddr, sizeof(handle->saddr)) != len)
    {
        snprintf(modbus_error_str, sizeof(modbus_error_str),
                 "%s: failed to send modbus UDP packet", __PRETTY_FUNCTION__);
        return -1;
    }

    return 0;
}

//------------------------------------------------------------------------------
//
//
//------------------------------------------------------------------------------
int
modbus_udp_recv(modbus_udp_handle_t *handle, modbus_packet_t *pkt)
{
    socklen_t fromlen;
    struct sockaddr_in caller;
    char buff[256];
    int len;

    // read UDP data
    fromlen = sizeof (caller);
    if ((len = recvfrom(handle->sock, buff, sizeof(buff), 0, (struct sockaddr *)&caller, &fromlen)) > 0)
    {
        return modbus_packet_parse(pkt, buff, len);
    }

    return -1;
}
4

1 回答 1

2

数据表说设备在端口 2001 上侦听并回复端口 2000。

这意味着您需要创建一个套接字并将bind()其连接到端口 2000 上的本地 IP,然后sendto()在端口 2001 上创建设备的 IP。您显示的代码根本没有调用bind(),因此没有本地端口用于recvfrom()接收数据。

于 2013-04-02T01:17:50.917 回答