最近选用有人科技的以太网转RS485模块做项目,设备真漂亮,国货之光。调通了通讯的代码,发到网上供大家参考,多多交流。
以下分别是配套的头文件与源文件:
/***************************************************************
* Copyright(C) 2023,Company All Rights Reserved (1)版权说明
*
* @file : Net2RS485.cpp (2) 文件名
*
* @brief : 有人科技的网络转串口的通信类 (3) 文件描述
*
* @version : 1.0 (4) 版本信息
*
* @author : 潘智斌 (5) 创建作者
*
* @date : 2023/12/27 星期四
*
* Details :
***************************************************************/
#ifndef _NET2RS485_H
#define _NET2RS485_H
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <iostream>
#include <WinSock.h>
#include <sys/types.h>
//#include <string>
#include "DataDefine.h"
using namespace std;
#pragma comment(lib,"ws2_32.lib")
#define RETURN_OK 1
#define RETURN_ERR 0
class Net2RS485
{
private:
WORD wVersion;
WSADATA wdSockData;
SOCKET clientSock;
LidarPara para;
public:
//打开数据通道
int Open(string &ip, int &port);
//关闭数据通道
int Close();
//发送数据的接口
int Send(char *data, int len);
//接收数据的接口
int Recv(char *data, int &len);
};
#endif
/***************************************************************
* Copyright(C) 2023,Company All Rights Reserved (1)版权说明
*
* @file : Net2RS485.cpp (2) 文件名
*
* @brief : 有人科技的网络转串口的通信类 (3) 文件描述
*
* @version : 1.0 (4) 版本信息
*
* @author : 潘智斌 (5) 创建作者
*
* @date : 2023/12/27 星期三
*
* Details :
***************************************************************/
#include "Net2RS485.h"
int Net2RS485::Open(string &ip, int &port)
{
wVersion = MAKEWORD(2, 2);
int ret = WSAStartup(wVersion, &wdSockData); //启动 网络
if (0 != ret)
{
printf("WSAStartup() failed : error() = %d\n", WSAGetLastError());
system("pause");
return -1;
}
if (2 != HIBYTE(wdSockData.wVersion) || 2 != LOBYTE(wdSockData.wVersion)) //校验版本
{
printf("version error\n");
WSACleanup();
system("pause");
return RETURN_ERR;
}
clientSock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == clientSock)
{
printf("socket() failed , error() = %d\n", WSAGetLastError());
WSACleanup();
system("pause");
return RETURN_ERR;
}
SOCKADDR_IN clientAddr;
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons(port);
clientAddr.sin_addr.s_addr = inet_addr(ip.data());
ret = connect(clientSock, (sockaddr*)&clientAddr, sizeof(clientAddr));
if (SOCKET_ERROR == ret)
{
printf("connect() failed, error : %d\n", WSAGetLastError());
closesocket(clientSock);
WSACleanup();
//system("pause");
return RETURN_ERR;
}
return RETURN_OK;
}
int Net2RS485::Close()
{
closesocket(clientSock);
WSACleanup();
return RETURN_OK;
}
int Net2RS485::Send(char *data, int len)
{
int result = send(clientSock, data, len, 0);
if (SOCKET_ERROR == result)
{
printf("send() failed, error = %d\n", WSAGetLastError());
}
return RETURN_OK;
}
int Net2RS485::Recv(char *data, int &len)
{
fd_set fdRead;
FD_ZERO(&fdRead); //初始化
FD_SET(clientSock, &fdRead);
timeval tv = { 1, };
int nRet = select(fdRead.fd_count, &fdRead, NULL, NULL, &tv);
if (nRet == 0 || nRet == SOCKET_ERROR)
{
return RETURN_ERR;
}
//判断socket是否是可以读数据了
if (FD_ISSET(clientSock, &fdRead))
{
len = recv(clientSock, data, 100, 0);
if (len == 0 || len == SOCKET_ERROR)
{
printf("接受数据失败 \r\n");
return RETURN_ERR;
}
}
return RETURN_OK;
}