文章目录
- 一、思维导图
- 二、在虚拟机写程序实现 poll 端功能,编写客户端实现和 Slave 通信
- 1> 单线圈数据的设置
- 2> 单线圈数据的读取
- 3> 多线圈数据的设置
- 4> 多寄存器数据的读取
- 5> 多寄存器数据的设置
- 6> 单寄存器数据的读取
- 三、牛客网刷题
一、思维导图

二、在虚拟机写程序实现 poll 端功能,编写客户端实现和 Slave 通信
1> 单线圈数据的设置
#include <myhead.h>
int main(int argc, const char *argv[])
{unsigned char buf[128] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x05, 0x00, 0x09, 0xFF, 0x00};int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock == -1){printf("%d ", __LINE__);perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(502);sin.sin_addr.s_addr = inet_addr("192.168.108.128"); unsigned char replyBuf[128] = "";while (1){if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接服务器成功\n");if (send(sock, buf, sizeof(buf), 0) == -1){perror("send error");return -1;}bzero(replyBuf, sizeof(replyBuf));int res = recv(sock, replyBuf, sizeof(replyBuf), 0);for (int i = 0; i < res; i++){printf("%#02x ", replyBuf[i]);}printf("\n");printf("res = %d\n", res);sleep(1);}close(sock);return 0;
}
2> 单线圈数据的读取
#include <myhead.h>
int main(int argc, const char *argv[])
{unsigned char buf[128] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x00, 0x08, 0x00, 0x01};int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock == -1){printf("%d ", __LINE__);perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(502);sin.sin_addr.s_addr = inet_addr("192.168.108.128"); unsigned char replyBuf[128] = "";while (1){if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接服务器成功\n");if (send(sock, buf, sizeof(buf), 0) == -1){perror("send error");return -1;}bzero(replyBuf, sizeof(replyBuf));int res = recv(sock, replyBuf, sizeof(replyBuf), 0);for (int i = 0; i < res; i++){printf("%#02x ", replyBuf[i]);}printf("\n");printf("res = %d\n", res);sleep(1);}close(sock);return 0;
}
3> 多线圈数据的设置
#include <myhead.h>
int main(int argc, const char *argv[])
{unsigned char buf[128] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x0F, 0x00, 0x01, 0x00, 0x02,0x01,0x0F};int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock == -1){printf("%d ", __LINE__);perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(502);sin.sin_addr.s_addr = inet_addr("192.168.108.128"); unsigned char replyBuf[128] = "";while (1){if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接服务器成功\n");if (send(sock, buf, sizeof(buf), 0) == -1){perror("send error");return -1;}bzero(replyBuf, sizeof(replyBuf));int res = recv(sock, replyBuf, sizeof(replyBuf), 0);for (int i = 0; i < res; i++){printf("%#02x ", replyBuf[i]);}printf("\n");printf("res = %d\n", res);sleep(1);}close(sock);return 0;
}
4> 多寄存器数据的读取
#include <myhead.h>
int main(int argc, const char *argv[])
{unsigned char buf[128] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x02};int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1){printf("%d ", __LINE__);perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(502);sin.sin_addr.s_addr = inet_addr("192.168.108.128"); unsigned char replyBuf[128] = "";while (1){if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接服务器成功\n");if (send(sock, buf, sizeof(buf), 0) == -1){perror("send error");return -1;}bzero(replyBuf, sizeof(replyBuf));int res = recv(sock, replyBuf, sizeof(replyBuf), 0);for (int i = 0; i < res; i++){printf("%#02x ", replyBuf[i]);}printf("\n");printf("res = %d\n", res);sleep(1);}close(sock);return 0;
}
5> 多寄存器数据的设置
#include <myhead.h>
int main(int argc, const char *argv[])
{unsigned char buf[128] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x01, 0x10, 0x00, 0x01, 0x00, 0x01,0x02,0x00,0x0F};int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock == -1){printf("%d ", __LINE__);perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(502);sin.sin_addr.s_addr = inet_addr("192.168.108.128"); unsigned char replyBuf[128] = "";while (1){if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接服务器成功\n");if (send(sock, buf, sizeof(buf), 0) == -1){perror("send error");return -1;}bzero(replyBuf, sizeof(replyBuf));int res = recv(sock, replyBuf, sizeof(replyBuf), 0);for (int i = 0; i < res; i++){printf("%#02x ", replyBuf[i]);}printf("\n");printf("res = %d\n", res);sleep(1);}close(sock);return 0;
}
6> 单寄存器数据的读取
#include <myhead.h>
int main(int argc, const char *argv[])
{unsigned char buf[128] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x04, 0x00, 0x01, 0x00, 0x02};int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1){printf("%d ", __LINE__);perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(502);sin.sin_addr.s_addr = inet_addr("192.168.108.128"); unsigned char replyBuf[128] = "";while (1){if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接服务器成功\n");if (send(sock, buf, sizeof(buf), 0) == -1){perror("send error");return -1;}bzero(replyBuf, sizeof(replyBuf));int res = recv(sock, replyBuf, sizeof(replyBuf), 0);for (int i = 0; i < res; i++){printf("%#02x ", replyBuf[i]);}printf("\n");printf("res = %d\n", res);sleep(1);}close(sock);return 0;
}
三、牛客网刷题
