• TCP机械臂控制


    通过w(红色臂角度增大)s(红色臂角度减小)d(蓝色臂角度增大)a(蓝色臂角度减小)按键控制机械臂

    注意:关闭计算机的杀毒软件,电脑管家,防火墙

    1)基于TCP服务器的机械臂,端口号是8888, ip是Windows的ip;

    查看Windows的IP:按住Windows+r 按键,输入cmd , 输入ipconfig

    2)点击软件中的开启监听;

    3)机械臂需要发送16进制数,共5个字节,协议如下

    0xff 0x02 x y 0xff 0xff:起始结束协议,固定的; 0x02:控制机械手臂协议,固定的; x:指定要操作的机械臂 0x00 红色摆臂 0x01 蓝色摆臂 y:指定角度

    1. #include
    2. #include
    3. #define SER_PORT 8888 // 服务器端口号
    4. #define SER_IP "192.168.118.58" // 服务器IP
    5. #define CLI_PORT 9000 // 客户端端口号
    6. #define CLI_IP "192.168.250.100" // 客户端IP
    7. int main(int argc, const char *argv[])
    8. {
    9. // 1、创建用于连接的客户端套接字
    10. int cfd = socket(AF_INET, SOCK_STREAM, 0);
    11. if (cfd == -1)
    12. {
    13. perror("socket error");
    14. return -1;
    15. }
    16. printf("socket success cfd = %d\n", cfd); // 3
    17. // 设置端口号快速重用
    18. int reuse = 1;
    19. if (setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
    20. {
    21. perror("setsockopt error");
    22. return -1;
    23. }
    24. printf("端口号快速重用成功\n");
    25. // 2、绑定端口号和ip地址(非必须)
    26. // 2.1 填充客户端地址信息结构体
    27. struct sockaddr_in cin;
    28. cin.sin_family = AF_INET;
    29. cin.sin_port = htons(CLI_PORT);
    30. cin.sin_addr.s_addr = inet_addr(CLI_IP);
    31. /*2.2 绑定端口号和IP
    32. if(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) == -1)
    33. {
    34. perror("bind error");
    35. return -1;
    36. }
    37. printf("bind success\n");
    38. */
    39. // 3、连接服务器
    40. // 3.1 填充要连接服务器的地址信息结构体
    41. struct sockaddr_in sin;
    42. sin.sin_family = AF_INET; // 地址族
    43. sin.sin_port = htons(SER_PORT); // 服务器端口号
    44. sin.sin_addr.s_addr = inet_addr(SER_IP); // 服务器的IP地址
    45. // 3.2 连接服务器
    46. if (connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
    47. {
    48. perror("connect error");
    49. return -1;
    50. }
    51. printf("连接成功!\n");
    52. // 定义控制机械臂的容器
    53. char buf_red[5] = {0xff, 0x02, 0x00, 0x00, 0xff}; // 红色臂容器
    54. unsigned char buf_blue[5] = {0xff, 0x02, 0x01, 0x00, 0xff}; // 蓝色臂容器
    55. // 将数据发送给服务器以初始化
    56. send(cfd, buf_red, sizeof(buf_red), 0);
    57. sleep(1);
    58. send(cfd, buf_blue, sizeof(buf_blue), 0);
    59. // 定义一个读取数据的结构体变量
    60. struct input_event ie;
    61. // 打开 /dev/input/event1文件
    62. int fd = open("/dev/input/event1", O_RDONLY);
    63. if (fd == -1)
    64. {
    65. perror("open error");
    66. return -1;
    67. }
    68. // 4、收发数据
    69. char ctrl = 0; // 用户输入的控制键
    70. while (1)
    71. {
    72. // scanf("%c", &ctrl); //用户输入键
    73. // getchar();
    74. // 从文件中读取当前文件中的数据
    75. // read(fd, &ie, sizeof(ie));
    76. scanf("%c", &ctrl);
    77. while (getchar() != '\n')
    78. ;
    79. // 输出从键盘事件文件中读取的数据
    80. // printf("ie.type = %d, ie.code = %d, ie.value = %d\n", ie.type, ie.code, ie.value);
    81. // 对输入的字符进行多分支选择
    82. switch (ctrl)
    83. {
    84. case 'w':
    85. {
    86. // 红色臂角度增大
    87. buf_red[3] += 2;
    88. if (buf_red[3] >= 90)
    89. {
    90. buf_red[3] = 90;
    91. }
    92. // 将数据发送给服务器
    93. send(cfd, buf_red, sizeof(buf_red), 0);
    94. }
    95. break;
    96. case 's':
    97. {
    98. // s键按下
    99. buf_red[3] -= 2;
    100. if (buf_red[3] <= -90)
    101. buf_red[3] = -90;
    102. // 将数据发送给服务器
    103. send(cfd, buf_red, sizeof(buf_red), 0);
    104. }
    105. break;
    106. case 'a':
    107. {
    108. // A键按下
    109. buf_blue[3] += 2;
    110. if (buf_blue[3] >= 180)
    111. buf_blue[3] = 180;
    112. // 将数据发送给服务器
    113. send(cfd, buf_blue, sizeof(buf_blue), 0);
    114. }
    115. break;
    116. case 'd':
    117. {
    118. // D键按下
    119. buf_blue[3] -= 2;
    120. if (buf_blue[3] <= 0)
    121. buf_blue[3] = 0;
    122. // 将数据发送给服务器
    123. send(cfd, buf_blue, sizeof(buf_blue), 0);
    124. }
    125. break;
    126. case 16:
    127. goto END;
    128. }
    129. }
    130. END:
    131. // 5、关闭套接字
    132. close(cfd);
    133. return 0;
    134. }

    效果:

  • 相关阅读:
    Idea借助Maven插件生成项目骨架archetype
    网络基础-RIP协议
    Unity地面交互效果——5、角色足迹的制作
    java进出口食品安全信息管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    【机器学习算法】神经网络与深度学习-9 递归神经网络
    【Android取证篇】华为设备跳出“允许USB调试“界面方法的不同方法
    剖析虚幻渲染体系(14)- 延展篇:现代渲染引擎演变史Part 4(结果期)
    秋招挂麻了,就差去送外卖了,10w字Java八股啃完,春招必拿下
    iNFTnews|NFT和艺术有何区别?
    使用openPlayer,根据坐标加载覆盖物
  • 原文地址:https://blog.csdn.net/weixin_50022690/article/details/136748637