找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2669|回复: 1
打印 上一主题 下一主题
收起左侧

去年做的基于QT的温湿度监测系统,这是在QT端运行的完整代码

[复制链接]
跳转到指定楼层
楼主
ID:718394 发表于 2020-3-29 21:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "widget.h"
  2. #include "ui_widget.h"

  3. #include <QHostAddress>

  4. #define WNDTITLE "智能家居温湿度监测系统"

  5. Widget::Widget(QWidget *parent) :
  6.     QWidget(parent),
  7.     ui(new Ui::Widget)
  8. {
  9.     ui->setupUi(this);

  10.     //分配一个套接字对象
  11.     s = new QTcpSocket(this);

  12.     //关联信号槽
  13.     connect(s, SIGNAL(connected()),
  14.             this, SLOT(slotConnected()));
  15.     connect(s, SIGNAL(disconnected()),
  16.             this, SLOT(slotDisconnected()));
  17.     connect(s, SIGNAL(error(QAbstractSocket::SocketError)),
  18.             this, SLOT(slotError()));
  19.     connect(s, SIGNAL(readyRead()),
  20.             this, SLOT(slotReadyRead()));

  21.     //设置窗口标题
  22.     setWindowTitle(WNDTITLE);
  23. }

  24. Widget::~Widget()
  25. {
  26.     delete ui;
  27. }

  28. void Widget::slotConnected()
  29. {
  30.     //修改按钮显示的文本:设置按钮标题
  31.     ui->pushButton->setText("连接成功");

  32.     //修改窗口标题
  33.     setWindowTitle(QString(WNDTITLE)+"-连接成功");

  34. }

  35. void Widget::slotDisconnected()
  36. {
  37.     //按钮变亮:按钮使能
  38.     ui->pushButton->setEnabled(true);

  39.     //修改按钮显示的文本:设置按钮标题
  40.     ui->pushButton->setText("连接");

  41.     //修改窗口标题
  42.     setWindowTitle(QString(WNDTITLE)+"-"+s->errorString());
  43. }

  44. void Widget::slotError()
  45. {
  46.     //按钮变亮:按钮使能
  47.     ui->pushButton->setEnabled(true);
  48.     //修改按钮显示的文本:设置按钮标题
  49.     ui->pushButton->setText("连接");

  50.     //修改窗口标题
  51.     setWindowTitle(QString(WNDTITLE)+"-"+s->errorString());
  52. }

  53. void Widget::slotReadyRead()
  54. {
  55.     //服务器每秒发4字节
  56.     //温度值: 1字节(整数位).0字节(小数位)
  57.     //温度值: 3字节(整数位).2字节(小数位)
  58.     if(s->bytesAvailable() >= 4){ //判断套接字缓存是否大于等于4bytes
  59.         //定义4字节数组
  60.         char buffer[4];
  61.         //从套接字缓存读出4字节
  62.         s->read(buffer, 4);

  63.         QString temp;
  64.         //格式化字符串:显示温度值
  65.         temp.sprintf("温度值:%d.%d", buffer[1], buffer[0]);
  66.         ui->tempLB->setText(temp);

  67.         QString hum;
  68.         //格式化字符串:显示湿度值
  69.         hum.sprintf("湿度值:%d.%d", buffer[3], buffer[2]);
  70.         ui->humLB->setText(hum);
  71.     }
  72. }

  73. void Widget::on_pushButton_clicked()
  74. {
  75.     //定义一个字符串对象
  76.     QString ip;
  77.     //把行编辑器上的文本保存到ip
  78.     ip = ui->lineEdit->text();
  79.     s->connectToHost(QHostAddress(ip), 59999);

  80.     //按钮变灰:按钮失效
  81.     ui->pushButton->setEnabled(false);
  82.     //修改按钮显示的文本:设置按钮标题
  83.     ui->pushButton->setText("连接中...");

  84.     //修改窗口标题
  85.     setWindowTitle(QString(WNDTITLE)+"-连接中...");
  86. }

  87. 透传服务器main函数:
  88. #include <stdio.h>
  89. #include <string.h>
  90. #include <sys/socket.h>
  91. #include <netinet/in.h>
  92. #include <arpa/inet.h>
  93. #include <unistd.h>
  94. #include "serial.h"
  95. int init_server(ushort port, int num)
  96. {
  97.         int s = socket(AF_INET, SOCK_STREAM, 0);
  98.         if(0 > s){
  99.                 perror("socket");
  100.                 return -1;
  101.         }
  102.         struct sockaddr_in addr = {
  103.                 .sin_family        = AF_INET,
  104.                 .sin_port        = htons(port),
  105.                 .sin_addr        = {
  106.                         .s_addr = INADDR_ANY,
  107.                 },
  108.         };
  109.         socklen_t len = 16;

  110.         if(0 > bind(s, (struct sockaddr *)&addr, len)){
  111.                 perror("bind");
  112.                 goto ERR_STEP;
  113.         }
  114.         if(0 > listen(s, num)){
  115.                 perror("listen");
  116.                 goto ERR_STEP;
  117.         }
  118.         return s;
  119. ERR_STEP:
  120.         close(s);
  121.         return -1;
  122. }
  123. int main()
  124. {
  125.         int fd = init_serial("/dev/ttyUSB0", B115200, 'n', 8, 1);
  126.         if(0 > fd){
  127.                 return -1;
  128.         }
  129.         printf("init M0 device done.\n");

  130.         int s = init_server(59999, 10);
  131.         if(0 > s){
  132.                 return -1;
  133.         }
  134.         printf("Wait for a client ...\n");

  135.         int rws = accept(s, NULL, NULL);
  136.         if(0 > rws){
  137.                 perror("accept");
  138.                 return -1;
  139.         while(1){
  140.                 char buf[36];       
  141.                 int len = 36;
  142.                 char *p = buf;
  143.                 while(len){
  144.                         int size = recv_serial(fd, p, len, 300000);
  145.                         if(0 > size){
  146.                                 return -1;
  147.                         }
  148.                         len -= size;
  149.                         p += size;
  150.                 }
  151.                 printf("temp: %d.%d\n", buf[5], buf[4]);
  152.                 printf(" hum: %d.%d\n", buf[7], buf[6]);
  153.                 if(4 != write(rws, buf+4, 4)){
  154.                         printf("snd temp & hum fail.\n");
  155.                         return -1;
  156.                 }
  157.         }
  158.         return 0;
  159. }

复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:664036 发表于 2020-5-30 21:02 | 只看该作者
请问可以发一下完整压缩包吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表