基於QT的全自動超聲波焊接機上位機追溯系統(已經在裝置上應用)
阿新 • • 發佈:2021-02-28
應用說明:
本上位機程式是我在做鋰電池產線專案的時候開發的,用於採集裝置資料以及實現裝置自動控制,下位機採用基恩士PLC,超聲波機採用上海一家的超聲波焊接機,實現電芯極耳的自動焊接,上位在裝置焊接過程中記錄焊接資料,同時監控裝置異常。
主要功能有:@1.裝置監控;@2.PLC TCP/IP通訊;@3超聲波焊接機通訊;@4掃碼槍通訊;@5.使用者登入方可有操作許可權;@6資料採集;@7PLC控制;@8資料持久化等
1.其主介面見下圖:
2.引數設定介面如下:
3.IO介面見下圖:
4.手動操作介面如下所示:
5.MES對接介面:
由於程式檔案較大,所以這裡只給出一些示例程式:
1.主程式如下:
1 include "messystem.h" 2 #include "appinit.h" 3 #include <QApplication> 4 #include <QTextCodec> 5 #include <QFile> 6 7 int main(int argc, char *argv[]) 8 { 9 QApplication a(argc, argv); 10 11 #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) 12 #if _MSC_VER 13 QTextCodec *codec = QTextCodec::codecForName("gbk"); 14 #else 15 QTextCodec *codec = QTextCodec::codecForName("utf-8"); 16 #endif 17 QTextCodec::setCodecForLocale(codec); 18 QTextCodec::setCodecForCStrings(codec); 19 QTextCodec::setCodecForTr(codec); 20 #else 21 QTextCodec *codec = QTextCodec::codecForName("utf-8"); 22 QTextCodec::setCodecForLocale(codec); 23 #endif 24 25 //載入樣式表 26 QFile file(":/qss/psblack.css"); 27 if (file.open(QFile::ReadOnly)) { 28 QString qss = QLatin1String(file.readAll()); 29 QString paletteColor = qss.mid(20, 7); 30 qApp->setPalette(QPalette(QColor(paletteColor))); 31 qApp->setStyleSheet(qss); 32 file.close(); 33 } 34 35 a.setFont(QFont("Microsoft Yahei", 9)); 36 AppInit::Instance()->start(); 37 38 UIDemo01 w; 39 w.show(); 40 41 return a.exec(); 42 }
2.掃碼槍程式
codereader.h檔案
1 #ifndef CODEREADER_H 2 #define CODEREADER_H 3 4 #include <QObject> 5 #include<QTcpSocket> 6 #include<QMessageBox> 7 8 class CodeReader : public QObject 9 { 10 Q_OBJECT 11 public: 12 explicit CodeReader(QObject *parent = 0); 13 ~CodeReader(); 14 15 void connectCodeReader(const QString &host,const QString port); 16 void disconnectCodeReader(); 17 18 void SendCmd(const QString cmd); 19 20 QString readCode(); 21 22 bool getStstus() const; 23 void setStstus(bool value); 24 25 int getLength() const; 26 void setLength(int value); 27 28 QString getCmd() const; 29 void setCmd(const QString &value); 30 31 QString getCodedata() const; 32 void setCodedata(const QString &value); 33 34 signals: 35 void receivedData(); 36 37 void SendLog(QString str); 38 39 public slots: 40 void doConnectClient(); 41 void doDisconnectClient(); 42 void doClientStateChange(QAbstractSocket::SocketState ss); 43 void doClientReadReady(); 44 void doClientSocketError(QAbstractSocket::SocketError e); 45 46 private: 47 QTcpSocket * client;//掃碼槍連線 48 49 bool ststus;//掃碼槍連線狀態 50 51 QString cmd;//掃碼槍觸發指令 52 53 int length;//條碼長度 54 55 QString codedata;//條碼 56 57 58 void Init();//初始化掃碼槍 59 60 61 62 63 64 65 }; 66 67 #endif // CODEREADER_H
codereader.cpp檔案
1 #include "codereader.h" 2 3 4 CodeReader::CodeReader(QObject *parent) : QObject(parent) 5 { 6 Init(); 7 8 } 9 10 CodeReader::~CodeReader() 11 { 12 client->close(); 13 delete client; 14 } 15 16 void CodeReader::connectCodeReader(const QString &host, const QString port) 17 { 18 if(host.isEmpty() || port.isEmpty()) 19 { 20 QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error",tr("使用者名稱和密碼能不能為空")); 21 return; 22 } 23 client->connectToHost(host,port.toInt()); 24 } 25 /** 26 * @brief CodeReader::disconnectCodeReader 27 * 斷開掃碼槍的連線 28 */ 29 void CodeReader::disconnectCodeReader() 30 { 31 client->disconnectFromHost(); 32 } 33 34 void CodeReader::SendCmd(const QString cmd) 35 { 36 QByteArray bacmd = cmd.toLocal8Bit(); 37 client->write(bacmd); 38 } 39 bool CodeReader::getStstus() const 40 { 41 return ststus; 42 } 43 44 void CodeReader::setStstus(bool value) 45 { 46 ststus = value; 47 } 48 int CodeReader::getLength() const 49 { 50 return length; 51 } 52 53 void CodeReader::setLength(int value) 54 { 55 length = value; 56 } 57 QString CodeReader::getCmd() const 58 { 59 return cmd; 60 } 61 62 void CodeReader::setCmd(const QString &value) 63 { 64 cmd = value; 65 } 66 /** 67 * @brief CodeReader::doConnectClient 68 * 處理連線上槽函式 69 */ 70 void CodeReader::doConnectClient() 71 { 72 setStstus(true); 73 emit SendLog(tr("掃碼槍已連線!")); 74 } 75 /** 76 * @brief CodeReader::doDisconnectClient 77 * 處理斷開連線槽函式 78 */ 79 void CodeReader::doDisconnectClient() 80 { 81 setStstus(false); 82 emit SendLog(tr("掃碼槍連線斷開!")); 83 } 84 /** 85 * @brief CodeReader::doClientStateChange 86 * @param ss 87 * 處理連線狀態改變槽函式 88 */ 89 void CodeReader::doClientStateChange(QAbstractSocket::SocketState ss) 90 { 91 92 if(ss==QAbstractSocket::QAbstractSocket::UnconnectedState) 93 { 94 setStstus(false); 95 } 96 else 97 { 98 setStstus(true); 99 } 100 101 } 102 /** 103 * @brief CodeReader::doClientReadReady 104 * 處理接收資料槽函式 105 */ 106 void CodeReader::doClientReadReady() 107 { 108 109 QByteArray badata = client->readAll(); 110 QString datacode = QString(badata); 111 datacode = datacode.trimmed(); 112 if(datacode.length()>1) 113 { 114 //emit SendLog(tr("讀取到資料")); 115 setCodedata(datacode); 116 //emit SendLog(datacode); 117 emit receivedData(); 118 } 119 120 121 } 122 /** 123 * @brief CodeReader::doClientSocketError 124 * @param e 125 * 處理socket錯誤槽函式 126 */ 127 void CodeReader::doClientSocketError(QAbstractSocket::SocketError e) 128 { 129 emit SendLog(tr("掃碼槍連線發生錯誤!")); 130 // switch (e) { 131 // case QAbstractSocket::ConnectionRefusedError: 132 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The connection was refused by the peer (or timed out)"); 133 // break; 134 // case QAbstractSocket::RemoteHostClosedError: 135 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The remote host closed the connection"); 136 // break; 137 // case QAbstractSocket::HostNotFoundError: 138 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The host address was not found."); 139 // break; 140 // case QAbstractSocket::SocketAccessError: 141 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The socket operation failed because the application lacked the required privileges"); 142 // break; 143 // case QAbstractSocket::SocketTimeoutError: 144 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The socket operation timed out."); 145 // break; 146 // case QAbstractSocket::DatagramTooLargeError: 147 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The datagram was larger than the operating system's limit (which can be as low as 8192 bytes)."); 148 // break; 149 // case QAbstractSocket::NetworkError: 150 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","An error occurred with the network (e.g., the network cable was accidentally plugged out)."); 151 // break; 152 // case QAbstractSocket::AddressInUseError: 153 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The address specified to QAbstractSocket::bind() is already in use and was set to be exclusive."); 154 // break; 155 // default: 156 // QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","An unidentified error occurred."); 157 // break; 158 // } 159 } 160 QString CodeReader::getCodedata() const 161 { 162 return codedata; 163 } 164 165 void CodeReader::setCodedata(const QString &value) 166 { 167 codedata = value; 168 } 169 170 /** 171 * @brief CodeReader::Init 172 * init codereader 173 */ 174 void CodeReader::Init() 175 { 176 ststus = false; 177 client = new QTcpSocket(this); 178 connect(client,SIGNAL(connected()),this,SLOT(doConnectClient())); 179 connect(client,SIGNAL(disconnected()),this,SLOT(doDisconnectClient())); 180 connect(client,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(doClientStateChange(QAbstractSocket::SocketState))); 181 connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(doClientSocketError(QAbstractSocket::SocketError))); 182 connect(client,SIGNAL(readyRead()),this,SLOT(doClientReadReady())); 183 184 }
3.工程目錄如下:
由於程式碼量較大,這裡就不一一列出來了,有需求原始碼的朋友可以加我微信:x241