应用说明:
本上位机程序是我在做锂电池产线项目的时候开发的,用于采集设备数据以及实现设备自动控制,下位机采用基恩士PLC,超声波机采用上海一家的超声波焊接机,实现电芯极耳的自动焊接,上位在设备焊接过程中记录焊接数据,同时监控设备异常。
主要功能有:@1.设备监控;@2.PLC TCP/IP通讯;@3超声波焊接机通讯;@4扫码枪通讯;@5.用户登录方可有操作权限;@6数据采集;@7PLC控制;@8数据持久化等
1.其主界面见下图:
2.参数设置界面如下:
3.IO界面见下图:
4.手动操作界面如下所示:
5.MES对接界面:
由于程序文件较大,所以这里只给出一些示例程序:
1.主程序如下:
`#include "messystem.h"
include "appinit.h"
include
include
include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
endif
//加载样式表
QFile file(":/qss/psblack.css");
if (file.open(QFile::ReadOnly)) {
QString qss = QLatin1String(file.readAll());
QString paletteColor = qss.mid(20, 7);
qApp->setPalette(QPalette(QColor(paletteColor)));
qApp->setStyleSheet(qss);
file.close();
}
a.setFont(QFont("Microsoft Yahei", 9));
AppInit::Instance()->start();
UIDemo01 w;
w.show();
return a.exec();
}`
2.扫码枪程序
codereader.h文件
`#ifndef CODEREADER_H
define CODEREADER_H
include
include
include
class CodeReader : public QObject
{
Q_OBJECT
public:
explicit CodeReader(QObject *parent = 0);
~CodeReader();
void connectCodeReader(const QString &host,const QString port);
void disconnectCodeReader();
void SendCmd(const QString cmd);
QString readCode();
bool getStstus() const;
void setStstus(bool value);
int getLength() const;
void setLength(int value);
QString getCmd() const;
void setCmd(const QString &value);
QString getCodedata() const;
void setCodedata(const QString &value);
signals:
void receivedData();
void SendLog(QString str);
public slots:
void doConnectClient();
void doDisconnectClient();
void doClientStateChange(QAbstractSocket::SocketState ss);
void doClientReadReady();
void doClientSocketError(QAbstractSocket::SocketError e);
private:
QTcpSocket * client;//扫码枪连接
bool ststus;//扫码枪连接状态
QString cmd;//扫码枪触发指令
int length;//条码长度
QString codedata;//条码
void Init();//初始化扫码枪
};
endif // CODEREADER_H`
codereader.cpp文件
`#include "codereader.h"
CodeReader::CodeReader(QObject *parent) : QObject(parent)
{
Init();
}
CodeReader::~CodeReader()
{
client->close();
delete client;
}
void CodeReader::connectCodeReader(const QString &host, const QString port)
{
if(host.isEmpty() || port.isEmpty())
{
QMessageBox::warning(qobject_cast<QDialog > (this->parent()),"error",tr("用户名和密码能不能为空"));
return;
}
client->connectToHost(host,port.toInt());
}
/*
- @brief CodeReader::disconnectCodeReader
- 断开扫码枪的连接
*/
void CodeReader::disconnectCodeReader()
{
client->disconnectFromHost();
}
void CodeReader::SendCmd(const QString cmd)
{
QByteArray bacmd = cmd.toLocal8Bit();
client->write(bacmd);
}
bool CodeReader::getStstus() const
{
return ststus;
}
void CodeReader::setStstus(bool value)
{
ststus = value;
}
int CodeReader::getLength() const
{
return length;
}
void CodeReader::setLength(int value)
{
length = value;
}
QString CodeReader::getCmd() const
{
return cmd;
}
void CodeReader::setCmd(const QString &value)
{
cmd = value;
}
/**
-
@brief CodeReader::doConnectClient
-
处理连接上槽函数
/
void CodeReader::doConnectClient()
{
setStstus(true);
emit SendLog(tr("扫码枪已连接!"));
}
/* -
@brief CodeReader::doDisconnectClient
-
处理断开连接槽函数
/
void CodeReader::doDisconnectClient()
{
setStstus(false);
emit SendLog(tr("扫码枪连接断开!"));
}
/* -
@brief CodeReader::doClientStateChange
-
@param ss
-
处理连接状态改变槽函数
*/
void CodeReader::doClientStateChange(QAbstractSocket::SocketState ss)
{if(ss==QAbstractSocket::QAbstractSocket::UnconnectedState)
{
setStstus(false);
}
else
{
setStstus(true);
}
}
/**
-
@brief CodeReader::doClientReadReady
-
处理接收数据槽函数
*/
void CodeReader::doClientReadReady()
{QByteArray badata = client->readAll();
QString datacode = QString(badata);
datacode = datacode.trimmed();
if(datacode.length()>1)
{
//emit SendLog(tr("读取到数据"));
setCodedata(datacode);
//emit SendLog(datacode);
emit receivedData();
}
}
/**
- @brief CodeReader::doClientSocketError
- @param e
- 处理socket错误槽函数
*/
void CodeReader::doClientSocketError(QAbstractSocket::SocketError e)
{
emit SendLog(tr("扫码枪连接发生错误!"));
// switch (e) {
// case QAbstractSocket::ConnectionRefusedError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The connection was refused by the peer (or timed out)");
// break;
// case QAbstractSocket::RemoteHostClosedError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The remote host closed the connection");
// break;
// case QAbstractSocket::HostNotFoundError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The host address was not found.");
// break;
// case QAbstractSocket::SocketAccessError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The socket operation failed because the application lacked the required privileges");
// break;
// case QAbstractSocket::SocketTimeoutError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The socket operation timed out.");
// break;
// case QAbstractSocket::DatagramTooLargeError:
// 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).");
// break;
// case QAbstractSocket::NetworkError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","An error occurred with the network (e.g., the network cable was accidentally plugged out).");
// break;
// case QAbstractSocket::AddressInUseError:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","The address specified to QAbstractSocket::bind() is already in use and was set to be exclusive.");
// break;
// default:
// QMessageBox::warning(qobject_cast<QDialog *> (this->parent()),"error","An unidentified error occurred.");
// break;
// }
}
QString CodeReader::getCodedata() const
{
return codedata;
}
void CodeReader::setCodedata(const QString &value)
{
codedata = value;
}
/**
- @brief CodeReader::Init
- init codereader
*/
void CodeReader::Init()
{
ststus = false;
client = new QTcpSocket(this);
connect(client,SIGNAL(connected()),this,SLOT(doConnectClient()));
connect(client,SIGNAL(disconnected()),this,SLOT(doDisconnectClient()));
connect(client,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(doClientStateChange(QAbstractSocket::SocketState)));
connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(doClientSocketError(QAbstractSocket::SocketError)));
connect(client,SIGNAL(readyRead()),this,SLOT(doClientReadReady()));
}`
3.工程目录如下:
由于代码量较大,这里就不一一列出来了,有需求源码的朋友可以加我微信:x241602.