基于小熊派光強傳感器BH1750狀態(tài)機驅動項目再度升級(帶上位機曲線顯示)
上次我們基于小熊派光強傳感器項目實現(xiàn)了光強讀取并在LCD上顯示,文章鏈接如下:
基于小熊派光強傳感器BH1750狀態(tài)機驅動項目升級(帶LCD屏顯示)
這一節(jié),我們再次對這個項目升級下,配個帶可縮放曲線的上位機讀取光強進行顯示吧!
本節(jié),你將了解工作中上位機和MCU的是如何來配合使用的。
在工作中,我們常常需要對一些傳感器的某些數(shù)值進行長時間的測試和觀察,以了解傳感器的性能,在電子工程里,我們經(jīng)常聽到的測試曲線莫過于電池充放電曲線了,通過電池充放電曲線,我們很容易可以知道電池在實際使用過程中滿電和饋電的狀態(tài)以及電池的使用周期等等,今晚,我們就讓光強通過曲線顯示出來,用QT+QCustomPlot來實現(xiàn),最終效果如下視頻操作所示:
一、QCustomPlot簡介
以下是QCustomPlot的官網(wǎng):
https://www.qcustomplot.com/
QCustomPlot是一個小型的Qt畫圖標類,支持繪制靜態(tài)曲線、動態(tài)曲線、多重坐標曲線,柱狀圖,蠟燭圖等。只需要在項目中加入頭文件qcustomplot.h和qcustomplot.cpp文件,然后使一個widget提升為QCustomPlot類,即可使用。
二、更改上節(jié)的MCU端程序
這次,我們選用串口和上位機進行通信,所以我們需要設計一個傳感器和上位機通信的協(xié)議,協(xié)議如下:
序號 光強值 \r\n
當序號大于等于65535時,自動清0。
修改上節(jié)的main函數(shù)為如下:
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/*流水號*/
int serial_number = 0;
char display_buf[20] = {0};
char procol_buf[20] = {0};
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
MX_SPI2_Init();
/* USER CODE BEGIN 2 */
/*串口初始化后加這個延時,防止后面的printf打印亂碼*/
HAL_Delay(200);
LCD_Init();
LCD_Clear(BLACK);//清屏為黑色
LCD_ShowString(5, 10, 240, 32, 32, "BearPi LuxTest");//顯示字符串,字體大小32*32
Init_BH750();
timer_init(&lsensor.timer1, lsensor.timeout_cb, 1, 1);
timer_start(&lsensor.timer1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
ReadBH1750(LUX_1_MODE);
if(1 == lsensor.Conver_completed)
{
++serial_number ;
if(65535 == serial_number)
serial_number = 0 ;
sprintf(display_buf, "%d%d%d%dLux", lsensor.Lux / 1000 % 100, lsensor.Lux / 100 % 10, lsensor.Lux / 10 % 10, lsensor.Lux % 10);
LCD_ShowString(80, 50 + 24 + 32, 240, 32, 32, display_buf); //顯示字符串,字體大小32*32
if(lsensor.Lux > LIGHT_SENSOR_THREHOLD)
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
sprintf(procol_buf, "%d%d%d%d%d %d%d%d%d%d",
serial_number / 10000,
serial_number / 1000 % 100 % 10,
serial_number / 100 % 10,
serial_number / 10 % 10,
serial_number % 10,
lsensor.Lux / 10000,
lsensor.Lux / 1000 % 100 % 10,
lsensor.Lux / 100 % 10,
lsensor.Lux / 10 % 10,
lsensor.Lux % 10
);
printf("%s \r\n", procol_buf);
}
timer_loop();
}
/* USER CODE END 3 */
}
下載后,打開串口調(diào)試助手可以看到:
三、光強曲線顯示上位機應用開發(fā)
下面我先用QT畫出一個簡單的界面(已經(jīng)將QCustomPlot用上了),如下:
這里我們需要使用QT5的串口庫,還有QCustomPlot庫,所以在.pro中需要添加對應的庫:
#-------------------------------------------------
#
# Project created by QtCreator 2020-04-13T20:46:41
#
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
TARGET = BearPi_QT
TEMPLATE = app
RC_FILE += app.rc
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
qcustomplot.cpp
HEADERS += \
mainwindow.h \
qcustomplot.h
FORMS += \
mainwindow.ui
在mainwindow.h中,需要添加頭文件及變量還有相關的普通函數(shù)和槽函數(shù)的定義:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDebug>
#include <QMainWindow>
#include <QSerialPort> //提供訪問串口的功能
#include <QSerialPortInfo> //提供系統(tǒng)中存在的串口的信息
#include "qcustomplot.h" //包含Qcustomplot庫
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
/*初始化串口參數(shù)*/
void Init_Serial_Para();
/*傳感器數(shù)據(jù)畫圖前的處理*/
void sensor_data_preprocess(int x, int y) ;
/*畫曲線函數(shù)*/
void Sensor_Draw_Plot(QCustomPlot *customPlot, QVector<double> x, QVector<double> y);
private:
Ui::MainWindow *ui;
QSerialPort *Serial ;
long last_index ;
QVector<double> display_x;
QVector<double> display_y;
QString Serial_Port , Serial_Bauardate,Serial_PariteyBit,Serial_DataBit,Serial_StopBit ;
private slots:
void Update_Serial_Port(QString Currnet_Select);
void Update_Serial_Baurdrate(QString Currnet_Select);
void Update_Serial_ParityBit(QString Currnet_Select);
void Update_Serial_DataBit(QString Currnet_Select);
void Update_Serial_StopBit(QString Currnet_Select);
void on_Connect_Device_clicked();
void on_Disconnect_Device_clicked();
void cmd_recv_process();
};
#endif // MAINWINDOW_H
在mainwindow.cpp太長我就不全貼出來了,解讀一下核心的實現(xiàn)思路:
1、用戶配置完串口參數(shù),然后 連接信號與槽:
connect(this->Serial,SIGNAL(readyRead()),this,SLOT(cmd_recv_process()));
2、當上位機接受到MCU端發(fā)來的數(shù)據(jù)時,會觸發(fā)readyRead()信號,進而調(diào)用cmd_recv_process()槽函數(shù),實現(xiàn)如下:
/*數(shù)據(jù)接收*/
void MainWindow::cmd_recv_process()
{
QString data ;
QStringList list ;
/*光強傳感器序號*/
QString Sensor_Serial_Number ;
/*光強傳感器光強值*/
QString Sensor_light_Value ;
long int s1 = 0, v1 = 0;
last_index =0;
if(Serial->canReadLine())
{
QByteArray temp = Serial->readAll();
if(!temp.isEmpty())
{
data = temp ;
list = data.split(" ");
if(3 == list.length())
{
Sensor_Serial_Number = list[0];
Sensor_light_Value = list[1];
/*取到對應參數(shù)后立刻開始畫圖*/
s1 = Sensor_Serial_Number.toInt();
v1 = Sensor_light_Value.toInt();
ui->light_sensor_value->setText(QString::number(v1));
if (last_index == 0 || (last_index < s1 && s1 - last_index >=1 && s1 - last_index <= 10 ))
{
last_index = s1;
sensor_data_preprocess(s1,v1);
}
}
}
}
}
這里會判斷是否滿足讀取一行數(shù)據(jù)的條件,如果滿足則會讀取一行數(shù)據(jù),接下來對數(shù)據(jù)進行分割,取出傳感器上報的序號、光強兩個字段,序號作為曲線圖的橫坐標,光強作為曲線圖的縱坐標進行顯示。
例程下載
鏈接:https://pan.baidu.com/s/1ujo0TE3pS-1RFVvylnCyKQ
提取碼:48jp
復制這段內(nèi)容后打開百度網(wǎng)盤手機App,操作更方便哦
公眾號粉絲福利時刻
這里我給大家申請到了福利,本公眾號讀者購買小熊派開發(fā)板可享受9折優(yōu)惠,有需要購買小熊派的朋友,淘寶搜索即可,跟客戶說你是公眾號:嵌入式云IOT技術圈 的粉絲,立享9折優(yōu)惠!
往期精彩
網(wǎng)紅物聯(lián)網(wǎng)開發(fā)板小熊派使用評測
超輕量級網(wǎng)紅軟件定時器multi_timer(51+stm32雙平臺實戰(zhàn))
基于小熊派光強傳感器BH1750實踐(multi_timer+狀態(tài)機工程應用)
基于小熊派光強傳感器BH1750狀態(tài)機驅動項目升級(帶LCD屏顯示)
若覺得本次分享的文章對您有幫助,隨手點[在看]
并轉發(fā)分享,也是對我的支持。
免責聲明:本文內(nèi)容由21ic獲得授權后發(fā)布,版權歸原作者所有,本平臺僅提供信息存儲服務。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!