项目,,,

2023-12-21 01:26:10

机械臂

include <myhead.h>
#define PORT     8888
#define IP  "192.168.125.160"
int main(int argc, const char *argv[])
{
	int cfd=-1;
	if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	socklen_t len1=sizeof(sin);
	if(connect(cfd,(struct sockaddr*)&sin,len1)==-1)
	{
		perror("connect error");
		return -1;
	}
	puts("连接成功");
	char rbuf[5]={0xff,0x02,0x00,0x00,0xff};
	unsigned char bbuf[5]={0xff,0x02,0x01,0x00,0xff};
	char jj=0;
	while(1)
	{
		jj=getchar();
		getchar();
		if(jj=='q'||jj=='Q')
			break;
		switch(jj)
		{
		case 'W':
		case 'w':
			{
				rbuf[3]+=10;
				if(rbuf[3]>=90)
				{
					rbuf[3]=90;
				}
				send(cfd,rbuf,sizeof(rbuf),0);
				break;		
			}
		case 'S':
		case 's':
			{
				rbuf[3]-=10;
				if(rbuf[3]<=-90)
				{
					rbuf[3]=-90;
				}
				send(cfd,rbuf,sizeof(rbuf),0);
				break;
			}
		case 'A':
		case 'a':
			{
				bbuf[3]+=10;
				if(bbuf[3]>=180)
				{
					bbuf[3]=180;
				}
				send(cfd,bbuf,sizeof(bbuf),0);
				break;
			}
		case 'D':
		case 'd':
			{
				bbuf[3]-=10;
				if(bbuf[3]<=0)
				{
					bbuf[3]=0;
				}
				send(cfd,bbuf,sizeof(bbuf),0);
				break;
			}
		default:
			{
				printf("请重新输入\n");
				break;
			}
		}
	}
	close(cfd);
	return 0;
}

文章来源:https://blog.csdn.net/m0_64059184/article/details/135071441
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。