0
我试图总结我的周围插座决定试着跟一些设备在工作后是如何工作的负责人。基本的细节是设备通过UDP使用ModBus RTU协议。数据表说,该设备侦听端口2001,并回复给端口2000年试图找到一些示例代码,我发现一个Modbus库后,并设法得到它与一些模拟器,其中发送和接收工作,似乎是在同港口。但是,我似乎无法让它与我的设备一起工作。我认为这个问题与港口有关。我到处都找过,似乎不能露面任何有用的东西(也许我只是没有足够好的理解)。这是我正在使用的代码,有没有人有任何指针。UDP的sendto和recvfrom在不同的端口
//------------------------------------------------------------------------------
// Copyright (C) 2010, Raditex AB
// All rights reserved.
//
// FreeSCADA
// http://www.FreeSCADA.com
// [email protected]
//
//------------------------------------------------------------------------------
#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;
}
谢谢,我会研究如何做到这一点。 – madpenguin8