4.23作业

本文详细介绍了如何使用C语言通过网络控制机械臂,包括创建流式套接字、绑定服务器地址、读取输入设备事件并发送指令给机械臂。
摘要由CSDN通过智能技术生成

控制机械臂

#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__);

    //填充服务器的地址信息结构体,真实的地址信息结构体根据地址族制定
    //AF_INT ------> man 7 IP
    struct sockaddr_in sin;
    sin.sin_family      = AF_INET;          //必须填AF_INT;
    sin.sin_port        = htons(Port);      //端口号的网络字节序  1024~49151
    sin.sin_addr.s_addr = inet_addr(IP);    //服务器要运行的主机的IP的网络地址                                     
                                            //ifconfig查找本机的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:        //  'A'
                    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:        //  'D'
                    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:        //  'W'
                    buf[2]=0x00;
                    buf[3]=cnt[0];
                    break;
            case 31:        //  'S'
                    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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值