本文主要是介绍028——从GUI->Client->Server->driver实现对SR04的控制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1、修改GUI
2、修改数据处理和发送缓冲区的帧
3、修改server中对SR04的处理
4、添加SR04的dirver_handle
5、验证
6、遇到问题及解决方法
7、 项目管理操作
1、修改GUI
2、修改数据处理和发送缓冲区的帧
添加对SR04按键事件处理
添加对接收数据的处理
3、修改server中对SR04的处理
printf("SR04!!!\n");if ('g' == cmd[4]){opt = sr04_handle(&get_data);if (NOERROR == opt){sprintf(buf,"@002g%03d", get_data);tx_buffer = buf;}else{tx_buffer = "@002e";}}printf(">>>>>>%s\n",tx_buffer); if (send(acceptfd, tx_buffer, strlen(tx_buffer), 0) < 0){perror("send failed"); }
4、添加SR04的dirver_handle
/*
*author : xintianyu
*function : Handle sr501 Settings
*date : 2024-4-18
-----------------------
author date modify*/
int sr04_handle(int *data)
{char *device = "/dev/CEBSS_sr04";int ret = NOERROR;static int fd;struct pollfd fds[1];int val;/* 打开文件 */fd = open(device, O_RDWR);if (fd == -1){printf("can not open file %s\n", device);return ERROR;}ioctl(fd, CMD_TRIG);printf("I am goning to read distance: \n");fds[0].fd = fd;fds[0].events = POLLIN;if (1 == poll(fds, 1, 5000)){if (read(fd, &val, 4) == 4){printf("get distance: %d cm\n", val*17/1000000);*data = val*17/1000000;}elseprintf("get distance err\n");}else{printf("get distance poll timeout/err\n");ret = ERROR;}if (*data > 450){ret = ERROR;}close(fd);return ret;
}
5、验证
6、遇到问题及解决方法
/home/book/program/cebss/server/src/driver_handle.c:144:2: error: implicit declaration of function ‘ioctl’ [-Werror=implicit-function-declaration]
ioctl(fd, CMD_TRIG);
^~~~~
cc1: all warnings being treated as errors
添加ioctl的头文件
/home/book/program/cebss/server/src/tcp.c:239:25: error: ‘tx_buffer’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
sprintf(tx_buffer,"@001g%d", get_data);
sprintf函数的buf必须是初始化过的内存比如数组或者malloc的地址
遇到其它问题可直接写导评论区或者加群反馈
7、 项目管理操作
这篇关于028——从GUI->Client->Server->driver实现对SR04的控制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!