控制机械臂
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#define IP "192.168.245.174"
#define Port 8888
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
fprintf(stderr,"line:%d ",__LINE__);
perror("socket");
return -1;
}
printf("创建流式套接字成功 sfd=%d __%d__\n",sfd,__LINE__);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(Port);
sin.sin_addr.s_addr = inet_addr(IP);
if(connect(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
fprintf(stderr, "line:%d ",__LINE__);
perror("bind");
return -1;
}
int fd = open("/dev/input/event1",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
struct input_event event;
char buf[5]={0xff,0x02,0x01,90,0xff};
char cnt[2];
ssize_t res;
while(1)
{
if(read(fd,&event,sizeof(event)) < 0)
{
perror("read");
return -1;
}
switch(event.code*event.value)
{
case 30:
if(buf[2] == 0x00) {
buf[3] = (buf[3] > -90) ? buf[3]-5 : -90 ;
cnt[0]=buf[3];
}
if(buf[2] == 0x01) {
buf[3] = (buf[3] > 0) ? buf[3]-5 : 0 ;
cnt[1]=buf[3];
}
break;
case 32:
if(buf[2] == 0x00) {
buf[3] = (buf[3] < 90) ? buf[3]+5 : 90 ;
cnt[0]=buf[3];
}
if(buf[2] == 0x01) {
buf[3] = (buf[3] < 180) ? buf[3]+5 : 180 ;
cnt[1]=buf[3];
}
break;
case 17:
buf[2]=0x00;
buf[3]=cnt[0];
break;
case 31:
buf[2]=0x01;
buf[3]=cnt[1];
break;
default:
continue;
}
ssize_t sen=send(sfd,buf,5,MSG_DONTWAIT);
if(0>sen)
{
fprintf(stderr, "line:%d ",__LINE__);
perror("send");
}
}
close(sfd);
return 0;
}