思维导图
机械臂
#include <my_head.h>
#define SER_PORT 8888
#define SER_IP "192.168.122.123"
int main(int argc, const char *argv[])
{
int cfd=-1;
cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
printf("cfd=%d\n",cfd);
//2.绑定(可选)
/* struct socketaddr_in cin;
cin.sin_family= AF_INET;
cin.sin_port= htons(CIN_PORT);
cin.sin_addr.s_addr=inet_addr(CIN_IP);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
*/
//3.1填充服务器地址消息结构体
struct sockaddr_in sin;
sin.sin_family= AF_INET;
sin.sin_port= htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
//3.2链接服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
char red[]={0xff,0x02,0x00,0,0xff};
unsigned char blue[]={0xff,0x02,0x01,90,0xff};
//4.收发数据
while(1)
{
char flag;
printf("请输入要操作0|1>>>");
scanf(" %c",&flag);
getchar();
switch(flag)
{
case 'w':
{
red[3]+=5;
if(red[3]>=90)
{
red[3]=90;
}
send(cfd,red,sizeof(red),0); }
break;
case 's':
{
red[3]-=5;
if(red[3]<=-90)
{
red[3]=-90;
}
send(cfd,red,sizeof(red),0);
}
break;
case 'a':
{
blue[3]+=5;
if(blue[3]>=180)
{
blue[3]=180;
}
send(cfd,blue,sizeof(blue),0);
}
break;
case 'd':
{
blue[3]-=5;
if(blue[3]<=0)
{
blue[3]=0;
}
send(cfd,blue,sizeof(blue),0);
}
break;
}
}
//5.关闭套接字
close(cfd);
return 0;
}