当前位置: 首页 > news >正文

Deepin系统,中盛科技温湿度模块读温度纯c程序(备份)

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>int main() {int fd;struct termios options;// 打开串口设备fd = open("/dev/ttyMP0", O_RDWR | O_NOCTTY|O_NDELAY); //O_NDELAY:打开设备不阻塞//O_NOCTTY:防止串口成为终端设备if (fd == -1) {perror("Error opening serial port");return 1;}// 设置串口参数cfsetispeed(&options, B9600); // 设置输入波特率cfsetospeed(&options, B9600); // 设置输出波特率options.c_cflag |= (CLOCAL | CREAD); // 必要的控制标志options.c_cflag &= ~PARENB; // 禁用奇偶校验options.c_cflag &= ~CSTOPB; // 1个停止位options.c_cflag &= ~CSIZE;  // 清除当前字符大小位 与CS 连用 options.c_cflag |= CS8; // 8位数据位options.c_cflag &= ~CRTSCTS;  //禁用控制流  这句非常重要,如不加程序异常options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  //原始模式options.c_iflag &= ~(IXON | IXOFF | IXANY);         //禁用软件流options.c_oflag &= ~OPOST;                          //原始输出// 应用新的串口属性if (tcsetattr(fd, TCSANOW, &options) != 0) {perror("Error setting serial port attributes");close(fd);return 1;}//  写串口,读串口  unsigned char data[]={1,4,0,0,0,1,0x31,0xca};write(fd,data,8);unsigned char o[20];int rt=read(fd,o,20);while(rt<2){    //接收数据不正确就重发sleep(1);       //发和收必须有时间延时write(fd,data,8);rt=read(fd,o,20);}for(int a=0;a<rt;a++){printf("%x;",o[a]);}puts("");printf("温度=%0.1f\n",(o[3]*256+o[4])*0.1);// 关闭串口设备close(fd);return 0;
}

5894d05032234c78a313d4d0f05317cf.png

 

http://www.lryc.cn/news/404604.html

相关文章:

  • 文件包含漏洞: 函数,实例[pikachu_file_inclusion_local]
  • 学习计划2024下半年
  • RabbitMQ的学习和模拟实现|sqlite轻量级数据库的介绍和简单使用
  • AI批量剪辑,批量发布大模型矩阵系统搭建开发
  • SpringMVC源码深度解析(中)
  • Mojo模型动态批处理:智能预测的终极武器
  • 人、智能、机器人……
  • SpringCloud------Sentinel(微服务保护)
  • 【无标题】Elasticsearch for windows
  • Yolo-World网络模型结构及原理分析(一)——YOLO检测器
  • WEB前端06-BOM对象
  • Android11 framework 禁止三方应用开机自启动
  • Java | Leetcode Java题解之第263题丑数
  • 将AWS RDS MySQL实例从存储未加密改为加密的方案
  • nginx的配置:TLSv1 TLSv1.1 被暴露不安全
  • 揭开黑箱:目标检测中可解释性的重要性与实现
  • Mysql高价语句
  • ArcGIS Pro SDK (九)几何 6 包络
  • 单链表<数据结构 C版>
  • 监控电脑进程,避免程序在打开前就已经在运行
  • 【MySQL进阶篇】存储对象:视图、存储过程及触发器
  • 算法day05 master公式估算递归时间复杂度 归并排序 小和问题 堆排序
  • 基于jeecgboot-vue3的Flowable流程仿钉钉流程设计器-支持VForm3表单的选择与支持
  • 【刷题汇总 -- 压缩字符串(一)、chika和蜜柑、 01背包】
  • 《Exploring Aligned Complementary Image Pair for Blind Motion Deblurring》
  • vue2学习笔记9 - 通过观察vue实例中的data,理解Vue中的数据代理
  • 04 Git与远程仓库
  • 数据库之表的查询
  • String 和StringBuilder字符串操作快慢的举例比较
  • Java代码基础算法练习-竞猜卡片值-2024.07.22