Qt编写的嵌入式工业控制系统

一、前言

此系统用于扭力控制,嵌入式平台,采用的开发板为335X,这个还是比较稳定的。

二、开发环境

硬件平台:335X

软件:Qt 嵌入式linux

三、实现功能

1:QSS界面换肤

2:更换主界面背景

3:中文双拼输入法

4:数据图形曲线显示

5:数据统计柱状图显示。

6:串口网络通信

四、效果图

Qt编写的嵌入式工业控制系统Qt编写的嵌入式工业控制系统Qt编写的嵌入式工业控制系统

五、核心代码

void Com_RS232_API::ReadData(){ if (com->bytesAvailable() <= 0) { return; } myHelper::Sleep(sleep); QByteArray data = com->readAll(); int dataLen = data.length(); if (dataLen <= 0) { return; } //更新最后心跳时间 lastHeartTime = QDateTime::currentDateTime(); QString buffer = myHelper::ByteArrayToAsciiStr(data); int len = buffer.length(); qDebug() << TIMEMS << \”接收数据:\” << buffer; emit ReceiveData(buffer); // //将数据加入处理队列,用线程处理数据 // mutex.lock(); // App::Buffer.append(data); // mutex.unlock(); // currentFlag=255; // sleep=50; if (buffer.startsWith(\”667\”) && currentFlag == 0) { qDebug() << TIMEMS << \”命令解析:\” << \”握手成功\” ; isOk = true; //握手成功后将心跳间隔改为500毫秒 timerHeart->setinterval(500); emit ReceiveHelloOk(); //如果未启用自动执行,则将心跳和开机交互完成置为真 if (!isAuto) { isHeart = true; allFinsh = true; } //握手成功后自动执行开机交互命令 if (isAuto) { QTimer::singleShot(10, this, SLOT(ReadPsetIDNameAll())); } } else if (buffer.startsWith(\”301PST\”) && currentFlag == 1) { if (buffer.length() < 7) { return; } qDebug() << TIMEMS << \”命令解析:\” << \”所有Pset索引返回\” ; QMap<int, QString> psetIDName; QString temp = buffer.mid(6, len – 1); QStringList list = temp.split(\”\’\”); int count = list.count() – 1; for (int i = 0; i < count; i ) { QStringList t = list.at(i).split(\”:\”); int id = t.at(0).toInt(); QString name = t.at(1); psetIDName.insert(id, name); } emit ReceivePsetIDNameAll(psetIDName); qDebug() << TIMEMS << \”命令解析:\” << \”psetIDName:\” << psetIDName; } else if (buffer.startsWith(\”001\”) && currentFlag == 2) { qDebug() << TIMEMS << \”命令解析:\” << \”授权1成功返回\” ; emit ReceiveAuthor1Ok(); //握手成功后自动执行开机交互命令 if (isAuto) { QTimer::singleShot(10, this, SLOT(Author2())); } } else if (buffer.startsWith(\”001\”) && currentFlag == 3) { qDebug() << TIMEMS << \”命令解析:\” << \”授权2成功返回\” ; emit ReceiveAuthor2Ok(); //握手成功后自动执行开机交互命令 if (isAuto) { QTimer::singleShot(10, this, SLOT(ReadPsetIDCurrent())); } } else if (buffer.startsWith(\”332\”) && currentFlag == 4) { if (buffer.length() < 6) { return; } qDebug() << TIMEMS << \”命令解析:\” << \”当前Pset索引返回\” ; int psetIDCurrent = buffer.mid(3, 3).toInt(); emit ReceivePsetIDCurrent(psetIDCurrent); qDebug() << TIMEMS << \”命令解析:\” << \”psetIDCurrent:\” << psetIDCurrent; //如果是重发命令确认的回复,则不需要继续执行开机交互命令 if (isRecheck) { isRecheck = false; return; } //握手成功后自动执行开机交互命令 if (isAuto) { QTimer::singleShot(10, this, SLOT(Cooperate())); } } else if (buffer.startsWith(\”5691\”) && currentFlag == 5) { qDebug() << TIMEMS << \”命令解析:\” << \”配合成功返回\” ; emit ReceiveCooperateOk(); //握手成功后自动执行开机交互命令 if (isAuto) { QTimer::singleShot(10, this, SLOT(ReadPsetIDAll())); } } else if (buffer.startsWith(\”5710\”) && currentFlag == 6) { qDebug() << TIMEMS << \”命令解析:\” << \”所有已经使用的Pset索引返回\” ; QList<int> psetIDAll; QStringList list = buffer.split(\”:\”); if (list.count() < 3) { return; } QStringList t = list.at(2).split(\”;\”); int count = t.count(); for (int i = 0; i < count; i ) { psetIDAll.append(t.at(i).toInt()); } emit ReceivePsetIDAll(psetIDAll); qDebug() << TIMEMS << \”命令解析:\” << \”psetIDAll:\” << psetIDAll; //启动心跳命令 isHeart = true; allFinsh = true; } else if (buffer.startsWith(\”001\”) && currentFlag == 7) { qDebug() << TIMEMS << \”命令解析:\” << \”指定Pset成功返回\” ; emit ReceiveSelectPset(); QTimer::singleShot(10, this, SLOT(ReadPsetIDCurrent())); isRecheck = true; //延时启动心跳定时器,防止结果未读成功而停止了心跳 QTimer::singleShot(500, this, SLOT(StartHeart())); } else if (buffer.startsWith(\”113P\”) && currentFlag == 8) { if (buffer.length() < 8) { return; } qDebug() << TIMEMS << \”命令解析:\” << \”Pset参数返回\” ; QMap<int, QString> config; int psetID = buffer.mid(4, 3).toInt(); QString temp = buffer.mid(7, len – 1); QStringList list = temp.split(\”\’\”); int count = list.count() – 1; for (int i = 0; i < count; i ) { QString t = list.at(i); int pID = t.mid(0, 3).toInt(); QString pValue = t.mid(4, t.length()); //如果参数号为406则需要转换为正常的日期格式 //传过来的是从1970年1月1日0时0分0秒到现在经过的秒数 if (pID == 406) { QDateTime d = myHelper::IntToDateTime(pValue.toUInt()); pValue = d.toString(\”yyyy-MM-dd HH:mm:ss\”); } config.insert(pID, pValue); } emit ReceivePsetConfig(psetID, config); qDebug() << TIMEMS << \”命令解析:\” << \”psetID:\” << psetID << \”config:\” << config; qApp->processEvents(); } else if (buffer.startsWith(\”001\”) && currentFlag == 9) { qDebug() << TIMEMS << \”命令解析:\” << \”更新Pset准备成功\” ; emit ReceiveUpdatePsetPrepareOk(); } else if (buffer.startsWith(\”001\”) && currentFlag == 10) { qDebug() << TIMEMS << \”命令解析:\” << \”更新Pset成功\” ; emit ReceiveUpdatePsetOk(); } else if (buffer.startsWith(\”00101\”) && currentFlag == 11) { qDebug() << TIMEMS << \”命令解析:\” << \”新建Pset成功\” ; emit ReceiveNewPsetOk(); } else if (buffer.startsWith(\”00101\”) && currentFlag == 12) { qDebug() << TIMEMS << \”命令解析:\” << \”删除Pset成功\” ; emit ReceiveDeletePsetOk(); } else if (buffer.startsWith(\”003\”)) { qDebug() << TIMEMS << \”命令解析:\” << \”心跳应答成功\” ; emit ReceiveHeartOk(); //003×02=003 003ACK为有新的结果通知 if (buffer.startsWith(\”003STX\”) || buffer.startsWith(\”003ACK\”)) { qDebug() << TIMEMS << \”命令解析:\” << \”有新的拧紧结果可读取\” ; //立马启动执行读取结果值 if (isAuto) { QTimer::singleShot(10, this, SLOT(ReadResultNmDeg())); //延时启动心跳定时器,防止结果未读成功而停止了心跳 QTimer::singleShot(1800, this, SLOT(StartHeart())); } } } else if (buffer.startsWith(\”113R\”)) { if (buffer.length() < 8) { return; } qDebug() << TIMEMS << \”命令解析:\” << \”拧紧数值结果返回\” ; int psetID = buffer.mid(4, 3).toInt(); double nm = 0; int nmOk = 1; int deg = 0; int degOk = 1; int id = 0; QString strDate = \”\”; QString num = \”0\”; QString temp = buffer.mid(7, len – 1); QStringList list = temp.split(\”\’\”); int count = list.count() – 1; for (int i = 0; i < count; i ) { QString t = list.at(i); int pID = t.mid(0, 3).toInt(); QString pValue = t.mid(4, t.length()); if (pID == 100) { nm = pValue.toDouble(); } else if(pID == 101) { nmOk = pValue.toInt(); } else if (pID == 102) { deg = pValue.toInt(); } else if(pID == 103) { degOk = pValue.toInt(); } else if (pID == 300) { id = pValue.toInt(); } else if (pID == 301) { strDate = pValue; } else if (pID == 302) { strDate = QString(\”%1 %2\”).arg(strDate).arg(pValue); } else if (pID == 305) { num = pValue; } } emit ReceiveResultNmDeg(psetID, id, strDate, num, nm, nmOk, deg, degOk); qDebug() << TIMEMS << \”命令解析:\” << \”psetID:\” << psetID << \”结果编号:\” << id << \”时间:\” << strDate << \”工具系列号:\” << num << \”扭矩值:\” << nm << \”扭矩结果:\” << nmOk << \”角度值:\” << deg << \”角度结果:\” << degOk; //立马读取扭矩对时间曲线结果 if (isAuto) { QTimer::singleShot(10, this, SLOT(ReadResultNmTimePlot())); //延时启动心跳定时器,防止结果未读成功而停止了心跳 QTimer::singleShot(1800, this, SLOT(StartHeart())); } qApp->processEvents(); } else if (buffer.startsWith(\”123G\”)) { if (buffer.length() < 5) { return; } qDebug() << TIMEMS << \”命令解析:\” << \”扭矩对时间曲线返回\” ; int psetID = 0; int id = 0; QString strDate = \”\”; QString num = \”0\”; double interval = 0; int fenzi = 1; int fenmu = 1; double targetNm = 0; double minNm = 0; double maxNm = 0; QList<double> result; QString temp = buffer.mid(4, len – 1); QStringList l = temp.split(\”;\”); if (l.count() < 2) { return; } //取出分号前的数值 QStringList list = l.at(0).split(\”\’\”); int count = list.count() – 1; for (int i = 0; i < count; i ) { QString t = list.at(i); int pID = t.mid(0, 3).toInt(); QString pValue = t.mid(4, t.length()); if (pID == 100) { id = pValue.toInt(); } else if (pID == 101) { strDate = pValue; } else if (pID == 102) { strDate = QString(\”%1 %2\”).arg(strDate).arg(pValue); } else if (pID == 105) { num = pValue; } else if (pID == 114) { psetID = pValue.toInt(); } else if (pID == 201) { interval = pValue.toDouble(); } else if (pID == 203) { fenzi = pValue.toInt(); } else if (pID == 204) { fenmu = pValue.toInt(); } else if (pID == 206) { targetNm = pValue.toDouble(); } else if (pID == 210) { minNm = pValue.toDouble(); } else if (pID == 211) { maxNm = pValue.toDouble(); } } //分号后的数据代表结果 QString str1 = l.at(1); QByteArray ba = myHelper::AsciiStrToByteArray(str1); QString str2 = myHelper::ByteArrayToHexStr(ba); //保存获取到的扭矩对角度数据 if (isSaveNm) { QString fileName = QString(\”%1/save/Nm_%2.txt\”).arg(App::AppPath) .arg(QDateTime::currentDateTime().toString(\”yyyy-MM-dd-HH-mm-ss\”)); QFile file(fileName); file.open(QFile::WriteOnly | QFile::Text); file.write(str2.toLatin1()); file.close(); } QStringList tempList = str2.split(\” \”); int currentCount = tempList.count(); //如果不是2的倍数则长度要减去1 if ( currentCount % 2 != 0) { currentCount = (currentCount – 1); } //逐个取出扭矩值 for (int i = 0; i < currentCount; i = i 2) { qint16 value = myHelper::StrHexToShort(QString(\”%1%2\”).arg(tempList.at(i)).arg(tempList.at(i 1))); double r = (double)(value * fenzi) / fenmu; result.append(r); } emit ReceiveResultNmTimePlot(psetID, id, strDate, num, interval, targetNm, minNm, maxNm, result); qDebug() << TIMEMS << \”命令解析:\” << \”psetID:\” << psetID << \”结果编号:\” << id << \”时间:\” << strDate << \”工具系列号:\” << num << \”间隔:\” << interval << \”目标扭矩:\” << targetNm << \”扭矩最小:\” << minNm << \”扭矩最大:\” << maxNm << \”结果集合:\” << result; //立马读取角度对时间曲线结果 if (isAuto) { QTimer::singleShot(10, this, SLOT(ReadResultDegTimePlot())); //延时启动心跳定时器,防止结果未读成功而停止了心跳 QTimer::singleShot(1800, this, SLOT(StartHeart())); } qApp->processEvents(); } else if (buffer.startsWith(\”125G\”)) { if (buffer.length() < 5) { return; } qDebug() << TIMEMS << \”命令解析:\” << \”角度对时间曲线返回\” ; int psetID = 0; int id = 0; QString strDate = \”\”; QString num = \”0\”; double interval = 0; int fenzi = 1; int fenmu = 1; double targetDeg = 0; double minDeg = 0; double maxDeg = 0; QList<double> result; QString temp = buffer.mid(4, len – 1); QStringList l = temp.split(\”;\”); if (l.count() < 2) { return; } //取出分号前的数值 QStringList list = l.at(0).split(\”\’\”); int count = list.count() – 1; for (int i = 0; i < count; i ) { QString t = list.at(i); int pID = t.mid(0, 3).toInt(); QString pValue = t.mid(4, t.length()); if (pID == 100) { id = pValue.toInt(); } else if (pID == 101) { strDate = pValue; } else if (pID == 102) { strDate = QString(\”%1 %2\”).arg(strDate).arg(pValue); } else if (pID == 105) { num = pValue; } else if (pID == 114) { psetID = pValue.toInt(); } else if (pID == 201) { interval = pValue.toDouble(); } else if (pID == 204) { fenzi = pValue.toInt(); } else if (pID == 205) { fenmu = pValue.toInt(); } else if (pID == 229) { targetDeg = pValue.toDouble(); } else if (pID == 220) { minDeg = pValue.toDouble(); } else if (pID == 221) { maxDeg = pValue.toDouble(); } } //分号后的数据代表结果 QString str1 = l.at(1); QByteArray ba = myHelper::AsciiStrToByteArray(str1); QString str2 = myHelper::ByteArrayToHexStr(ba); //保存获取到的角度对角度数据 if (isSaveDeg) { QString fileName = QString(\”%1/save/Deg_%2.txt\”).arg(App::AppPath) .arg(QDateTime::currentDateTime().toString(\”yyyy-MM-dd-HH-mm-ss\”)); QFile file(fileName); file.open(QFile::WriteOnly | QFile::Text); file.write(str2.toLatin1()); file.close(); } QStringList tempList = str2.split(\” \”); int currentCount = tempList.count(); //如果不是2的倍数则长度要减去1 if ( currentCount % 2 != 0) { currentCount = (currentCount – 1); } //逐个取出角度值 for (int i = 0; i < currentCount; i = i 2) { qint16 value = myHelper::StrHexToShort(QString(\”%1%2\”).arg(tempList.at(i)).arg(tempList.at(i 1))); double r = (double)(value * fenzi) / fenmu; //传过来的值放大了100倍,所以真实显示的值要除以100 double re = r / 100; result.append(re); } emit ReceiveResultDegTimePlot(psetID, id, strDate, num, interval, targetDeg, minDeg, maxDeg, result); qDebug() << TIMEMS << \”命令解析:\” << \”psetID:\” << psetID << \”结果编号:\” << id << \”时间:\” << strDate << \”工具系列号:\” << num << \”间隔:\” << interval << \”最终角度结果:\” << targetDeg << \”最终角度最小:\” << minDeg << \”最终角度最大:\” << maxDeg << \”结果集合:\” << result; //将心跳重新置为真 isHeart = true; qApp->processEvents(); } //currentFlag = 255; //sleep = 50;}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

(0)
上一篇 2023年4月25日 上午8:00
下一篇 2023年4月25日 上午8:10

相关推荐