核心思想
取指->译码->执行
因为是使用串口输入,所以依旧可以使用这种思想
译码函数(与前两篇相同)
提高代码复用性,并且可以屏蔽无用指令,虽然最后还是发的ascll码形式。
客户端程序(删的只剩框架)
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int decoding(char *code)
{
return -1;
}
int main(int argc,char **argv)
{
char code[4] = {0};
int cmd = 0x01;
int s_fd;
struct sockaddr_in s_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&s_addr.sin_addr);
socklen_t len = sizeof(struct sockaddr_in);
if(connect(s_fd,(struct sockaddr *)&s_addr,len) == -1){
perror("connect");
exit(-1);
}
while(1)
{
printf(">");
memset(code,'\0',4);
gets(code);
cmd = decoding(code);
if(cmd == -1){
printf("cmd not exist\n");
continue;
}
write(s_fd,code,4);
if(cmd == 0xff){
close(s_fd);
printf("client exit\n");
exit(1);
}
}
return 0;
}
本文介绍了一种基于串口通信的客户端程序设计,通过取指、译码、执行的核心思想,实现对输入指令的高效处理。文章详细展示了客户端程序的代码框架,包括套接字的创建、连接服务器的过程,以及指令解析函数的使用。此外,还讨论了如何通过提高代码复用性和屏蔽无用指令来优化程序性能。

被折叠的 条评论
为什么被折叠?



