5.18 TCP机械臂模拟

cpp 复制代码
#include <netinet/tcp.h>//包含TCP选项的头文件
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>//读取输入事件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.75"
#define CLI_PORT 6666
#define CLI_IP "192.168.125.114"


int main(int argc, const char *argv[])
{
	int cfd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == cfd){
		perror("socket");
		return 1;
	}
	printf("cfd = %d\n", cfd);

	struct sockaddr_in cin = {
		.sin_family = AF_INET,
		//注释下行启用动态端口号
		.sin_port = htons(CLI_PORT),
		.sin_addr = { inet_addr(CLI_IP) },
	};
	if(-1 == bind(cfd, (struct sockaddr*)&cin, sizeof(cin))){
		perror("bind");
		return 1;
	}
	puts("bind success");

	struct sockaddr_in sin = {
		.sin_family = AF_INET,
		.sin_port = htons(SER_PORT),
		.sin_addr = { .s_addr = inet_addr(SER_IP) },
	};	
	if(-1 == connect(cfd, (struct sockaddr*)&sin, sizeof(sin))){
		perror("connect");
		return 1;
	}
	puts("connect success");
	//设置TCP push位
	int val = 1;
	//TCP_NODELAY表示Nagle算法(导致粘包现象),默认置0(不关闭),置1即可关闭
	if(-1 == setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val))){
		perror("setsockopt");
		return 1;
	}
	/*val = 0;
	if(-1 == setsockopt(cfd, IPPROTO_TCP, TCP_CORK, &val, sizeof(val))){
		perror("setsockopt");
		return 1;
	}*/
	//设置端口快速重用
	printf("val = %d\n", val);
	if(-1 == setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))){
		perror("setsockopt");
		return 1;
	}


	//定义指令格式结构体
	//注意字节对齐填充导致的问题,且大于1B的数据类型会自动补0
	//均会导致协议解析失败
	struct {//5B
		char c1[3];//3B
		union{
			char arm_red;
			unsigned char arm_blue;
		}c2;//1B
		char c3;//1B
	}cmd = { 0xff, 0x02, 0x00, 0x00, 0xff };

	//机械臂初始化
	send(cfd, &cmd, sizeof(cmd), 0);
	cmd.c1[2] = 0x01;
	usleep(50000);
	send(cfd, &cmd, sizeof(cmd), 0);

	char ang_r = 0x00;
	unsigned char ang_b = 0x00;
	//打开/dev/input/event1
	int fd = open("/dev/input/event1", O_RDONLY);
	if(-1 == fd){
		perror("open");
		return 1;
	}
	//准备读取数据的结构体变量
	struct input_event input;
#include <linux/input.h>
/* 结构体形式如下:
 * struct input_event {
		struct timeval time;
		__u16 type;
		__u16 code;
		__s32 value;
 };
*/
	// system("stty raw -echo");
	while(1){
		//char ch = getchar();
		read(fd, &input, sizeof(input));
		//switch(ch){
		switch(input.value * input.code){//code按下为0,弹起为1,长按为2
		//case 'w'://红增 				 //w:17 a:30 s:31 d:32
		case 17*2:
			if(ang_r < 0x5A)//最大90度
				ang_r += 0x02;
			cmd.c2.arm_red = ang_r;
			cmd.c1[2] = 0x00;
			break;
		//case 's'://红减
		case 31*2:
			if(ang_r > -0x5A)
				ang_r -= 0x02;
			cmd.c2.arm_red = ang_r;
			cmd.c1[2] = 0x00;
			break;
		//case 'd'://蓝增
		case 32*2:
			if(ang_b < 0xB4)
				ang_b += 0x02;
			cmd.c2.arm_blue = ang_b;
			cmd.c1[2] = 0x01;
			break;
		//case 'a'://蓝减
		case 30*2:
			if(ang_b > 0x00)
				ang_b -= 0x02;
			cmd.c2.arm_blue = ang_b;
			cmd.c1[2] = 0x01;
			break;
		default:
			continue;
			break;
		}
		send(cfd, &cmd, sizeof(cmd), 0);
	}
	// system("stty -raw echo");
	close(cfd);
	close(fd);
	return 0;
}
相关推荐
螺蛳粉 螺蛳粉2 分钟前
第二篇:keepalived VRRP 原理篇:协议与 VIP 漂移机制深入解析
网络·智能路由器·负载均衡·keepalived·高可用
盟接之桥20 分钟前
AI助手:你的7×24小时智能管家——让异常预警无处不在
大数据·网络·数据库·人工智能·制造·ai编程
2603_969579282 小时前
内网数据自动备份工具:隔离网络下的数据保护方案
网络
逗脑IDE2 小时前
零基础学ESP32:RFID无线射频卡——让ESP32拥有“刷卡“能力!
开发语言·网络·人工智能·python·esp32·硬件开发
cws2004012 小时前
监控摄像头选型指南
运维·网络·监控
和裕3 小时前
定制纸箱刀模费全解析:费用定义与可减免合作场景
大数据·运维·网络·人工智能·算法
剑锋所指,所向披靡!3 小时前
HTTP服务器的基本相关概念
服务器·网络协议·http
troy1283 小时前
智能体沙箱:如何为 Agent 模型搭建安全试验场,降低幻觉带来的损失
网络·安全
LHR_friendship3 小时前
HCIP课程全部内容
网络·笔记·学习·hcip·华为ensp
Chengbei114 小时前
拒绝无效洞!蚂蚁Src榜一Clown 2.0 SRC SKill 第二代带状态闭环的白盒 & 黑盒审计 Skill
网络·人工智能·安全·web安全·网络安全·自动化·安全架构