使用Qt串口实时显示温湿度数据

之前用Qt做了一个简单的串口助手,前几天做了一个可以实时显示温湿度数据的串口应用,今天周末总结一下。
实时显示温湿度
效果:
1、实现的效果如上图所示,可以实时显示温湿度以及系统的当前时间;
2、串口的波特率可选,其它设置项固定,数据位8、停止位1、检验及流控无;

测试:
1、我使用了两种方式进行测试,第一个是通过短接串口线的接收和发送,在软件中定时发送固定格式的数据帧进行测试,我测试了大概4个小时,系统没有问题;
2、第二个就是用单片机通过串口向终端发送固定格式的数据帧,这个我也测试了大概大概两个小时,系统没出现问题。

重点内容:
重点是对于接收的数据的处理,要约定好发送的数据的数据帧得格式,我使用的是简单的数据帧:6个字节构成一帧数据,如“ST125E”,第一个字符代表帧头,第二个代表数据类型(温度数据),第三个代表数据正负,第四和第五个代表数据,第六个为帧尾。无校验位。接收到数据后对这些数据进行进行判断处理,无错误时进行数据显示,刷新周期为1s。

//接收的数据处理
void Widget::readData()
{
QByteArray buf;
buf=serialport->readAll();
receiverBuf.append(buf);
qDebug()<<“receiverBuf:”<<receiverBuf;

if(receiverBuf.size()==6)
{
    //温度的数据
    if(receiverBuf.at(0)=='S'&&receiverBuf.at(1)=='T'&&receiverBuf.at(5)=='E')
    {
        if(receiverBuf.at(2)=='1')
        {
            //正温度值
            qDebug()<<"温度:+"<<receiverBuf.at(3)<<receiverBuf.at(4)<<"C";                QString str="+";
            str.append(receiverBuf.at(3));
            str.append(receiverBuf.at(4));
            str.append('C');
            ui->lcd_Temp->display(str);
        }
        else if(receiverBuf.at(2)=='0')
        {
            //负温度值
            qDebug()<<"温度:-"<<receiverBuf.at(3)<<receiverBuf.at(4)<<"C";
            QString str="-";
            str.append(receiverBuf.at(3));
            str.append(receiverBuf.at(4));
            str.append('C');
            ui->lcd_Temp->display(str);
        }
        else
        {
            //错误处理
            qDebug()<<"温度数据错误!";
        }
        //清空缓存
        receiverBuf.clear();
    }
    //湿度的数据
    else if(receiverBuf.at(0)=='S'&&receiverBuf.at(1)=='H'&&receiverBuf.at(5)=='E')
    {
        if(receiverBuf.at(2)=='1')
        {
            //正湿度值
            qDebug()<<"湿度:"<<receiverBuf.at(3)<<receiverBuf.at(4)<<"%";
            QString str;
            str.append(receiverBuf.at(3));
            str.append(receiverBuf.at(4));
            str.append('%');
            ui->lcd_humi->display(str);
        }
        else
        {
            //错误处理
            qDebug()<<"湿度数据错误";
        }
        //清空缓存
        receiverBuf.clear();
    }
    else
    {
        //清空缓存
        receiverBuf.clear();
    }
}
if(receiverBuf.size()>6)
{
    //数据错误
    qDebug()<<"数据格式错误";
}

}

//定时器自动更新发送
void Widget::timeUpdate()
{

sendCounter++;
if(sendCounter>10)
{
    sendCounter=0;
}
else
{
        if(sendCounter%3==0)
        {
            serialport->write("ST125E");
        }
        else if(sendCounter%3==1)
        {
            serialport->write("ST020E");
        }
        else if(sendCounter==2)
        {
            serialport->write("SH150E");
        }
}

QString str;
str=dt->currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
qDebug()<<"DateTime:"<<str;

ui->lcd_dt->display(str);

}

大概就这么多,之后尝试使用绘制实时温湿度曲线。

  • 22
    点赞
  • 203
    收藏
    觉得还不错? 一键收藏
  • 34
    评论
要用Qt绘制温湿度折线图,可以使用Qt自带的绘图类QPainter和QPainterPath,具体步骤如下: 1. 创建QWidget或QFrame控件作为绘制图形的容器,重写其paintEvent方法,在该方法中进行绘制操作。 2. 在paintEvent方法中创建QPainter对象,设置画笔和画刷的属性,如颜色、线宽等。 3. 创建QPainterPath对象,通过addRect、addLine等方法绘制折线图的线条和坐标轴。 4. 通过QPainter的drawPath方法将QPainterPath对象绘制出来。 下面是一个简单的示例代码,用于绘制温湿度折线图: ``` void MyWidget::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿 painter.setPen(QPen(Qt::black, 2)); // 设置画笔颜色和线宽 // 绘制坐标轴 painter.drawLine(50, 20, 50, 200); // y轴 painter.drawLine(50, 200, 300, 200); // x轴 // 绘制温度折线 QPainterPath tempPath; tempPath.moveTo(50, 200 - 5 * m_temperatures[0]); // 第一个点 for(int i = 1; i < m_temperatures.size(); ++i) { tempPath.lineTo(50 + i * 10, 200 - 5 * m_temperatures[i]); // 连线 } painter.setPen(QPen(Qt::red, 2)); // 设置画笔颜色和线宽 painter.drawPath(tempPath); // 绘制湿度折线 QPainterPath humidPath; humidPath.moveTo(50, 200 - 5 * m_humidities[0]); // 第一个点 for(int i = 1; i < m_humidities.size(); ++i) { humidPath.lineTo(50 + i * 10, 200 - 5 * m_humidities[i]); // 连线 } painter.setPen(QPen(Qt::blue, 2)); // 设置画笔颜色和线宽 painter.drawPath(humidPath); } ``` 其中,m_temperatures和m_humidities是存储温度湿度数据的容器,可以通过外部接口或者文件读取等方式获取数据。在绘制折线时,通过遍历数据容器,计算出每个点的坐标,然后使用QPainterPath的moveTo和lineTo方法将点连接成线。
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值