离子色谱仪实验[嵌入式Linux项目]-Qt开发日志
环境搭建
先说一下搭建以下环境的原因,开发流程如图。
ARM生产环境
实验设备为 FriendlyARM 的 CortexA9 Smart4418 实验箱。配套资源网址 为:http://wiki.friendlyarm.com/wiki/index.php/Smart4418/zh
OS:FriendlyCore,是一个没有 X-windows 环境,基于 Ubuntucore 构建的系统,使用 Qt-Embedded 作为图形界面的轻量级系统,兼容 Ubuntu 系统软件源,非常适合于企业用户用作产品的基础 OS。
PC开发环境
开发在虚拟机下的Ubuntu16系统,安装了Qt5、Qt Creator以及Qt5交叉编译链工具。
Qt5安装
sudo apt-get install qt5Qt Creator安装
Windows上安装Qt Creator,编写运行成功后,传至Linux上进行交叉编译,将目标程序放ARM上直接与运行。
Qt5交叉编译链
FriendlyARM交叉编译链arm-linux-gcc下载地址:https://github.com/friendlyarm/prebuilts
如何交叉编译(以编译QtE-Demo项目为例):
mkdir build && cd build/usr/local/Trolltech/Qt-5.10.0-nexell32-sdk/bin/qmake ../QtE-Demo.promake在ARM机上运行,如提示:EGL library doesn't support Emulator extensions
输入如下命令解决:
export QT_QPA_EGLFS_INTEGRATION=noneARM网络设置
ARM连接电脑热点
查看Wifi设备
nmcli dev开启Wifi设备
nmcli r wifi on扫描附近Wifi热点
nmcli dev wifi连接到指定Wifi热点
nmcli dev wifi connect "SSID" password "PASSWORD" ifname wlan0查看IP地址
ifconfig第一次连接后,以后每次开机,ARM都会自动连接之前的Wifi。
通过Win10网络设置也可以看到已连接设备的IP。
SSH远程访问ARM
拿到设备IP后,使用SSH协议远程访问ARM,使用SFTP协议上传下载程序。
工具:MobaXterm
ARM串口读写
GPIO引脚控制LED
linux下对/sys/class/gpio中的gpio的控制 (转)
配置GPIO43与GPIO44,其分别控制两个LED灯的亮灭。
cd /sys/class/gpioecho 43 > exportecho 44 > exportcd gpio43echo high > directionecho low > directionecho 1 > valueecho 0 > valuecd gpio44...测试Demo:
//初始化引脚int fd;char buf[5];char gpioExportPath[30] = "/sys/class/gpio/export";//配置GPIOfd = open(gpioExportPath, O_WRONLY);if (fd < 0) { printf("gpioExportPath:%stopen failed", gpioExportPath); exit(1);}sprintf(buf, "%2d",this->ledNo);write(fd, buf, 2);close(fd);//设置GPIO方向char gpioDirPath[40];sprintf(gpioDirPath, "/sys/class/gpio/gpio%d/direction", this->ledNo);fd = open(gpioDirPath, O_WRONLY);if (fd < 0) { printf("gpioDirPath:%stopen failed", gpioDirPath); exit(1);}sprintf(buf, "high");write(fd, buf, 3);close(fd);//控制亮灭void updateLedStatus(int ledNo){ int fd; char buf[2]; char path[30]; sprintf(path, "/sys/class/gpio/gpio%d/value" , ledNo); fd = open(path, O_WRONLY); if (fd < 0) { printf("updateLedStatus failed"); exit(1); } if (ledStatus) { buf[0] = '1'; } else { buf[0] = '0'; } write(fd, buf, 1); close(fd);}AD读取
测试Demo
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>void delay(){int i,j;for (i = 0; i < 200; i++)for (j = 0; j < 100; j++);}int main(){int fd, len, i;char buf[20];for (i = 0; i < 5; i++){fd = open("/sys/devices/platform/c0000000.soc/c0053000.adc/iio:device0/in_voltage7_raw", 0);if (fd < 0) exit(1);len = read(fd, buf, sizeof buf - 1);if (len > 0){buf[len] = '';printf("%s", buf);}delay();}close(fd);return 0;}蜂鸣器
http://wiki.friendlyarm.com/wiki/index.php/Matrix_-_Buzzer/zh
https://www.eefocus.com/toradex/blog/17-05/420816_04520.html
cd /sys/class/pwm/pwmchip0echo 0 > exportcd pwm0echo 400000000 > period #周期,单位nsecho 100000000 > duty_cycle #设置占空比25%echo 1 > enable #开启echo 0 > enable #关闭需求建模
用例图
活动图

程序编码
服务端打算用Qt编写,开发流程是:1. 在Win10上使用IDE开发 2. 在虚拟机Linux上交叉编译 3. 在嵌入式Linux上运行程序查看效果。
界面设计
根据详细设计的功能,规划界面内容。
界面上展示的包括:LED控制、色谱图模块、LCD数值显示模块、网络连接展示模块。
后端隐含的功能模块包括:网络连接模块、网络传输模块、网络数据解析模块、GPIO读写模块、AD读取模块、蜂鸣器控制模块。
LED控制模块
先来实现LED控制功能。
设计一个LED控制类。
ledcontrol.h
#ifndef LEDCONTROL_H#define LEDCONTROL_H/** * @brief LED控制类 */class LEDControl{public: LEDControl(int no); void initGPIO(); //初始化GPIO引脚 void setLedStatus(bool status); //设置LED状态private: void updateLedStatus(); //更新GPIO引脚状态,控制LED亮灭public: int ledNo; //LED编号 bool ledStatus; //LED状态};#endif // LEDCONTROL_Hledcontrol.cpp
#include "ledcontrol.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <QDebug>/** * @brief LED控制类 */LEDControl::LEDControl(int no){ this->ledNo = no; initGPIO(); //初始化LED对应的GPIO引脚 this->setLedStatus(true); //默认高电平,灭}/** * @brief 初始化LED对应的GPIO引脚 */void LEDControl::initGPIO(){ int fd; char buf[5]; char gpioExportPath[30] = "/sys/class/gpio/export"; //配置GPIO fd = open(gpioExportPath, O_WRONLY); if (fd < 0) { printf("gpioExportPath:%stopen failed", gpioExportPath); exit(1); } sprintf(buf, "%2d",this->ledNo); write(fd, buf, 2); close(fd); //设置GPIO方向 char gpioDirPath[40]; sprintf(gpioDirPath, "/sys/class/gpio/gpio%d/direction", this->ledNo); fd = open(gpioDirPath, O_WRONLY); if (fd < 0) { printf("gpioDirPath:%stopen failed", gpioDirPath); exit(1); } sprintf(buf, "high"); write(fd, buf, 3); close(fd);}/** * @brief 设置LED状态 false亮, true灭 */void LEDControl::setLedStatus(bool status){ this->ledStatus = status; updateLedStatus(); qDebug() << "led" << this->ledNo << "t status:" << this->ledStatus;}/** * @brief 更新LED状态 */void LEDControl::updateLedStatus(){ int fd; char buf[2]; char path[30]; sprintf(path, "/sys/class/gpio/gpio%d/value" , this->ledNo); fd = open(path, O_WRONLY); if (fd < 0) { printf("updateLedStatus failed"); exit(1); } if (this->ledStatus) { buf[0] = '1'; } else { buf[0] = '0'; } write(fd, buf, 1); close(fd);}AD读取模块
adreader.h
#ifndef ADREADER_H#define ADREADER_H/** * @brief AD读取类 * 读取电压模拟量 */class ADReader{public: ADReader(); int readVol(); //电压模拟量读取private: char buf[20]; //AD读取缓冲区 int readLen; //当次读取数据长度 int adFileDesc; //AD设备文件描述符};#endif // ADREADER_Hadreader.cpp
#include "adreader.h"#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>/** * @brief AD读取类 * 读取电压模拟量 */ADReader::ADReader(){}/** * @brief 读取电压模拟量 * @return */int ADReader::readVol(){ //Win环境下测试用 //if (true) //{ // return 800; //} //打开AD设备文件 this->adFileDesc = open("/sys/devices/platform/c0000000.soc/c0053000.adc/iio:device0/in_voltage7_raw", 0); if (this->adFileDesc < 0) { printf("adFile open failed"); exit(1); } //读取电压模拟量到buf this->readLen = read(this->adFileDesc, this->buf, sizeof this->buf - 1); if (this->readLen > 0) { this->buf[this->readLen] = ''; } close(this->adFileDesc); return atoi(buf); //转化为int返回}蜂鸣器控制模块
对于服务端,如果读取的AD值超出安全范围,则控制蜂鸣器发出警报,同时告诉客户端警报开启。
buzzer.h
#ifndef BUZZER_H#define BUZZER_H/** * @brief 蜂鸣器 */class Buzzer{public: Buzzer(); void setEnable(int enable_);private: void initPWM(); void updateEnable(); int enable; //1开启,0关闭};#endif // BUZZER_Hbuzzer.cpp
#include "buzzer.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <QDebug>/** * @brief 蜂鸣器 */Buzzer::Buzzer(){ initPWM();}/** * @brief 初始化PWM */void Buzzer::initPWM(){ int fd; char buf[15]; char exportPath[35] = "/sys/class/pwm/pwmchip0/export"; //配置PWM fd = open(exportPath, O_WRONLY); if (fd < 0) { printf("pwmExportPath:%stopen failed", exportPath); exit(1); } buf[0] = '0'; write(fd, buf, 1); close(fd); //设置PWM周期 char periodPath[45] = "/sys/class/pwm/pwmchip0/pwm0/period"; fd = open(periodPath, O_WRONLY); if (fd < 0) { printf("pwmPeriodPath:%stopen failed", periodPath); exit(1); } sprintf(buf, "400000000"); write(fd, buf, 9); close(fd); //设置PWM占空比 char dutyCyclePath[55] = "/sys/class/pwm/pwmchip0/pwm0/duty_cycle"; fd = open(dutyCyclePath, O_WRONLY); if (fd < 0) { printf("pwmDutyCyclePath:%stopen failed", dutyCyclePath); exit(1); } sprintf(buf, "100000000"); write(fd, buf, 9); close(fd);}/** * @brief 更新蜂鸣器状态 */void Buzzer::updateEnable(){ int fd; char buf[2]; char path[45] = "/sys/class/pwm/pwmchip0/pwm0/enable"; fd = open(path, O_WRONLY); if (fd < 0) { printf("updatePWMEnable failed"); exit(1); } buf[0] = '0' + this->enable; write(fd, buf, 1); close(fd);}/** * @brief 设置蜂鸣器开关状态 * @param enable */void Buzzer::setEnable(int enable_){ if(this->enable == enable_) return; //防止重复写 this->enable = enable_; updateEnable(); qDebug() << "PWM: " << this->enable;}网络模块
基于TCPSocket进行网络通讯。
设计流程:
服务端网络模块
使用QT封装的TCP网络通讯类:QTcpServer与QTcpSocket。
使用Qt建立一个TCP服务端分为以下几步:
- 设置监听端口
- 绑定newConnection信号与处理该信号的槽函数
Sample:
//设置监听端口tcpserver->listen(QHostAddress::Any, this->serverPort.toInt());//新客户端连接connect(tcpserver, &QTcpServer::newConnection, this, &MainWidget::newConnect);在newConnect槽函数处理TCP客户端的连接请求。
处理流程:
测试TCP服务端
确定接收与发送功能无误。
通讯协议制定
制定TCP服务端与客户端之间的通讯协议。
数据首部:#
客户端网络模块
客户端创建一个TCP连接的流程:
Sample:
//init TCP Serverthis->tcpClient = new QTcpSocket(this); //实例化tcpClientthis->tcpClient->abort(); //取消原有连接connect(tcpClient, SIGNAL(readyRead()), this, SLOT(ReadData()));/** * @brief 连接按钮点击事件 */void MainWidget::on_btn_conn_clicked(){ if(ui->btn_conn->text()=="连接") { tcpClient->connectToHost(ui->edit_ip->text(), ui->edit_port->text().toInt()); if (tcpClient->waitForConnected(1000)) // 连接成功则进入if{} { ui->btn_conn->setText("断开"); ui->btn_send->setEnabled(true); ui->btn_led1->setEnabled(true); ui->btn_led2->setEnabled(true); } } else { tcpClient->disconnectFromHost(); if (tcpClient->state() == QAbstractSocket::UnconnectedState || tcpClient->waitForDisconnected(1000)) //已断开连接则进入if{} { ui->btn_conn->setText("连接"); ui->btn_send->setEnabled(false); } }}消息解析模块
消息解析流程:
离子色谱图绘制模块
QCustomPlot:https://www.qcustomplot.com/
使用的QCustomPlot绘图窗口部件。
优点是没有过多的依赖库,使用方便,将头文件与源文件添加至项目工程即可。
//init PlotSendTimeInterval = 500; //发送时间间隔//开始绘制setupRealtimeDataDemo(ui->plot);ui->plot->replot();初始化Plot
流程:
远程报警模块
对于服务端,如果读取的AD值超出安全范围,则控制蜂鸣器发出警报,同时告诉客户端警报开启。
客户端实现报警就是播放一段音频。
在QT项目中添加一个资源文件,存放warning.wav报警音频文件。
在项目pro文件,添加multimedia多媒体配置
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

