1.客户端对机械臂进行控制
#include <myhead.h>
#define SER_PORT 8888
#define SER_IP "192.168.117.49"
#define CLI_PORT 6666
#define CLI_IP "192.168.117.97"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd == -1)
{
perror("socket");
return -1;
}
printf("socket success\n");
//设置端口号快速重用
int reuse = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse)) ==-1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\n");
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(CLI_PORT);
cin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect");
return -1;
}
printf("connect success\n");
char c=0;
char rbuf[5]={0xff,0x02,0x00,0,0xff};
unsigned char bbuf[5]={0xff,0x02,0x01,90,0xff};
while(1)
{
printf("请输入w/s/d/a:");
scanf("%c",&c);
getchar();
switch(c)
{
case'w':
{
rbuf[3]+=10;
send(cfd,rbuf,sizeof(rbuf),0);
break;
}
case's':
{
rbuf[3]-=10;
send(cfd,rbuf,sizeof(rbuf),0);
break;
}
case'd':
{
bbuf[3]+=10;
send(cfd,bbuf,sizeof(bbuf),0);
break;
}
case'a':
{
bbuf[3]-=10;
send(cfd,bbuf,sizeof(bbuf),0);
break;
}
default:printf("输入错误\n");
}
}
close(cfd);
return 0;
}