3.15作业机械臂

#include <sys/types.h>         
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>    
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>


#define SER_PORT 8888
#define SER_IP "192.168.117.47"

int main()
{
    int cfd = -1;
    cfd = socket(AF_INET,SOCK_STREAM,0);

    if(cfd == -1)
    {
        perror("socket");
        return 1;
    }

    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;
    }

    unsigned char str1[5] = {0xff,0x02,0x01,0x00,0xff};    //0x00   -  0xb4
    unsigned char str2[5] = {0xff,0x02,0x00,0x00,0xff};      //红色    0x00 -  0x60   0-90度  
                                                            //0xff  - 0xa6   0  ~    -90   度

                                                //   0xa6     0xff   0x00    0x60
                                                //       -90       0       90


    //char str3[5] = {0xff,0x02,0x00,0xff,0xff};
    struct termios oldt, newt;  
    tcgetattr(STDIN_FILENO, &oldt);  
    newt = oldt;  
    newt.c_lflag &= (~ICANON);  
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);  

    // int flags;
    // flags = fcntl(STDIN_FILENO,F_GETFL,0);
    // fcntl(STDIN_FILENO,F_SETFL,flags|O_NONBLOCK);    


    while(1)
    {
        char c = getchar();
        if(c == 'w')
        {
            str1[3]++;
            if(str1[3]>=0xb3)
                str1[3]=0xb3;
            //usleep(5000);
            write(cfd,str1,5);    
            printf("-w-\n%#x",str1[3]);
        }

        else if(c == 's')
        {
            str1[3]--;
            if(str1[3]<=0x01)
                str1[3] = 0x01;
            //usleep(5000);
            write(cfd,str1,5);    
            printf("-s-\n%#x",str1[3]);
        }

        else if(c == 'a')
        {
            str2[3]++;
            if(str2[3]>=0x60 && str2[3] <0xa6 )
                str2[3]=0x60;
            //usleep(5000);
            write(cfd,str2,5);   

            printf("-a-\n%#x",str2[3]);   
        }

        else if (c== 'd')
        {
            str2[3]--;
            if(str2[3]<=0xa6  && str2[3] > 0x60)
                str2[3]=0xa6;
            //usleep(5000);
            write(cfd,str2,5);   
            printf("-d-\n%#x",str2[3]);           
        }
        //printf("已发送\n");
    }

    return 0;
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值