lovejobs

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

应用说明:
本上位机程序是我在做锂电池产线项目的时候开发的,用于采集设备数据以及实现设备自动控制,下位机采用基恩士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.工程目录如下:

 

 

由于代码量较大,这里就不一一列出来了,源码下载地址https://download.csdn.net/download/xipengbozai/16546764

 

 

posted on 2021-02-28 20:39  lovejobs  阅读(764)  评论(0编辑  收藏  举报