LABWINDOWS CVI进行Modbus Tcp通信

2023-12-14 08:32:45
int  client_socket;
int connectPlc(char *ip, int port)
{
	unsigned char response[1024];
	int register_value = 0;
	int closePortErr = 0;
	int openPortErr = 0;
	int commPortErr = 0;
	WORD wVersionRequested;
	WSADATA wsaData;
	struct sockaddr_in server_addr;

	wVersionRequested = MAKEWORD(2, 1);
	openPortErr = WSAStartup( wVersionRequested, &wsaData);

	client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (client_socket < 0)
	{
		return 0;
	}

	memset(&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	server_addr.sin_port = htons(502);

	openPortErr = connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));

	if (openPortErr)
	{
		WSACleanup();
		return -1;
	}
	else
	{
		return 0; 	
	}
	return 0;
}

int sendPlc()
{
	unsigned char request[12] =
	{
		0x00, 0x01, // 事务标识符
		0x00, 0x00, // 协议标识符
	

文章来源:https://blog.csdn.net/qq_35439171/article/details/134986174
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。