嵌入式入门Day37

作业

驱动机械臂

#include <myhead.h>

#define IP "192.168.124.16"
#define SERPORT 8888

int main(int argc, const char *argv[])
{
	//创建套接字
	int oldfd = socket(AF_INET, SOCK_STREAM, 0);
	if (oldfd == -1)
	{
		perror("socket");
		return -1;
	}
	
	//连接服务器,填充服务器信息
	
	struct sockaddr_in server = {
		.sin_family = AF_INET,
		.sin_port = htons(SERPORT),
		.sin_addr.s_addr = inet_addr(IP)
	};
	if (-1 == connect(oldfd, (struct sockaddr *)&server, sizeof(server)))
	{
		perror("connect");
		return -1;
	}
	
	//初始化机械臂位置
	char red[5] = {0xff, 0x02, 0x00, 0, 0xff};
	char blue[5] = {0xff, 0x02, 0x01, 0, 0xff};
	
	//先向服务器发送初始位置
	send(oldfd, red, sizeof(red), 0);
	send(oldfd, blue, sizeof(blue), 0);

	//定义字符接收按键
	char ch;
	while (1)
	{
		//获取输入按键
		scanf("%c", &ch);
		getchar();
		//QE移动大臂,AD移动小臂
		switch (ch)
		{
			case 'q':
			case 'Q':
				red[3] -= 1;
				if (red[3] <= -90)
				{
					red[3] = -90;
				}
				send(oldfd, red, sizeof(red), 0);
				break;
			case 'e':
			case 'E':
				red[3] += 1;
				if (red[3] >= 90)
				{
					red[3] = 90;
				}
				send(oldfd, red, sizeof(red), 0);
				break;
			case 'a':
			case 'A':
				blue[3] -= 1;
				if (blue[3] <= 0)
				{
					blue[3] = 0;
				}
				send(oldfd, blue, sizeof(blue), 0);
				break;
			case 'd':
			case 'D':
					blue[3] += 1;
				if (blue[3] >= 180)
				{
					blue[3] = 180;
				}
				send(oldfd, blue, sizeof(blue), 0);
				break;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值