思维导图
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#define IP "192.168.125.26"
#define PORT 8888
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-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);
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
char rbuf[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
unsigned char bbuf[5] = {0xff, 0x02, 0x01, 0x00, 0xff};
send(cfd, rbuf, sizeof(rbuf), 0); //初始化红色臂展
send(cfd, bbuf, sizeof(bbuf), 0); //初始化蓝色臂展
while(1)
{
char ch;
scanf("%c",&ch);
while(getchar()!=10);
switch(ch)
{
case 'w':
if(rbuf[3]<=80&&rbuf[3]>=-90)
{
rbuf[3]+=0x0A;
send(cfd, rbuf, sizeof(rbuf), 0);
break;
}
else if(rbuf[3]==90)
{
printf("红色机械臂已到达正向最大扭矩\n");
break;
}
case 's':
if(rbuf[3]>=-80&&rbuf[3]<=90)
{
rbuf[3]-=0x0A;
send(cfd, rbuf, sizeof(rbuf), 0);
break;
}
else if(rbuf[3]==-90)
{
printf("红色机械臂已到达反向最大扭矩\n");
break;
}
case 'd':
if(bbuf[3]>=0&&bbuf[3]<=170)
{
bbuf[3]+=0x0A;
send(cfd, bbuf, sizeof(bbuf), 0);
break;
}
else if(bbuf[3]==180)
{
printf("蓝色机械臂已到达正向最大扭矩\n");
break;
}
case 'a':
if(bbuf[3]>=10&&bbuf[3]<=180)
{
bbuf[3]-=0x0A;
send(cfd, bbuf, sizeof(bbuf), 0);
break;
}
else if(bbuf[3]==0)
{
printf("蓝色机械臂已到达反向最大扭矩\n");
break;
}
default:
printf("输入有误\n");
break;
}
}
close(cfd);
return 0;
}