项目,,,

机械臂

include 
#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;
}

项目,,,_第1张图片

你可能感兴趣的:(c语言)