本文主要是介绍LABWINDOWS CVI进行Modbus Tcp通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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, // 协议标识符
这篇关于LABWINDOWS CVI进行Modbus Tcp通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!