作业
驱动机械臂
#include <myhead.h>
#define IP "192.168.124.16"
#define SERPORT 8888
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 server = {
.sin_family = AF_INET,
.sin_port = htons(SERPORT),
.sin_addr.s_addr = inet_addr(IP)
};
if (-1 == connect(oldfd, (struct sockaddr *)&server, sizeof(server)))
{
perror("connect");
return -1;
}
//初始化机械臂位置
char red[5] = {0xff, 0x02, 0x00, 0, 0xff};
char blue[5] = {0xff, 0x02, 0x01, 0, 0xff};
//先向服务器发送初始位置
send(oldfd, red, sizeof(red), 0);
send(oldfd, blue, sizeof(blue), 0);
//定义字符接收按键
char ch;
while (1)
{
//获取输入按键
scanf("%c", &ch);
getchar();
//QE移动大臂,AD移动小臂
switch (ch)
{
case 'q':
case 'Q':
red[3] -= 1;
if (red[3] <= -90)
{
red[3] = -90;
}
send(oldfd, red, sizeof(red), 0);
break;
case 'e':
case 'E':
red[3] += 1;
if (red[3] >= 90)
{
red[3] = 90;
}
send(oldfd, red, sizeof(red), 0);
break;
case 'a':
case 'A':
blue[3] -= 1;
if (blue[3] <= 0)
{
blue[3] = 0;
}
send(oldfd, blue, sizeof(blue), 0);
break;
case 'd':
case 'D':
blue[3] += 1;
if (blue[3] >= 180)
{
blue[3] = 180;
}
send(oldfd, blue, sizeof(blue), 0);
break;
}
}
return 0;
}