基于TCP通信控制红色机械臂和蓝色机械臂的运作
1、项目要求:通过w(红色臂角度增大)s(红色臂角度减小)d(蓝色臂角度增大)a(蓝色臂角度减小)
#include <myhead.h>
#define IP "192.168.60.106"
#define PORT 8000
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 cin;
cin.sin_family =AF_INET;
cin.sin_port =htons(PORT);
cin.sin_addr.s_addr =inet_addr(IP);
//连接服务器
if(connect(oldfd,(struct sockaddr *)&cin,sizeof(cin))==-1)
{
perror("connect");
return -1;
}
//定义机械臂结构体
//红色机械臂
char rbuf[5]={0xff,0x02,0x00,0xA6,0xff};
//蓝色机械臂
char bbuf[5]={0xff,0x02,0x01,0x00,0xff};
//发送红色机械臂初始数据
send(oldfd,rbuf,sizeof(rbuf),0);
sleep(1);
//发送蓝色机械臂初始数据
send(oldfd,bbuf,sizeof(bbuf),0);
printf("设置初始数据成功\n");
char ch;
while(1)
{
scanf("%c",&ch);//输入状态符
while(getchar()!='\n');//吸收回车
switch(ch)
{
case 'W':
case 'w':
{
rbuf[3]+=5;
if(rbuf[3]>=90)
{
rbuf[3]=90;
}
send(oldfd,rbuf,sizeof(rbuf),0);
}
break;
case 'S':
case 's':
{
rbuf[3]-=5;
if(rbuf[3]<=-90)
{
rbuf[3]=-90;
}
send(oldfd,rbuf,sizeof(rbuf),0);
}
break;
case 'A':
case 'a':
{
bbuf[3]-=5;
if(bbuf[3]<=0)
{
bbuf[3]=0;
}
send(oldfd,bbuf,sizeof(bbuf),0);
}
break;
case 'D':
case 'd':
{
bbuf[3]+=5;
if(bbuf[3]>=180)
{
bbuf[3]=180;
}
send(oldfd,bbuf,sizeof(bbuf),0);
}
break;
case 'Q':
case 'q':
goto END;
}
}
END:
close(oldfd);
return 0;
}