UDP sendto和recvfrom在不同的端口上

时间:2013-04-02 00:39:28

标签: c sockets udp modbus

我试图在决定尝试与工作中的某些设备交谈后,围绕套接字如何工作。基本细节是设备使用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;
}

1 个答案:

答案 0 :(得分:2)

  

数据表说设备侦听端口2001并回复到端口2000.

这意味着您需要在端口2000上创建套接字并将bind()创建到本地IP,然后在端口2001上创建设备IP的sendto()。您显示的代码不是调用{{1总而言之,bind()没有本地端口可以接收数据。