Files
eUi/include/simpleTools/simpleSiemensTcp.hpp
2025-08-25 15:53:05 +08:00

110 lines
4.6 KiB
C++

#include <QObject>
#include <qtcpsocket.h>
class simpleSiemensTcp : public QTcpSocket
{
Q_OBJECT
public:
simpleSiemensTcp(QObject* parent = nullptr) : QTcpSocket(parent) {
connect(this, &QTcpSocket::connected, this, &simpleSiemensTcp::onConnected);
connect(this, &QTcpSocket::disconnected, this, &simpleSiemensTcp::onDisconnected);
connect(this, &QTcpSocket::readyRead, this, &simpleSiemensTcp::onReadyRead);
//connect(this, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred),
// this, &simpleSiemensTcp::onErrorOccurred);
connectToServer("192.168.10.1", 102);
waitForConnected();
//"03 00 00 16 11 E0 00 00 00 01 00 C0 01 0A C1 02 01 02 C2 02 01 00 "TC
//"03 00 00 19 02 F0 80 32 01 00 00 04 00 00 08 00 00 F0 00 00 01 00 01 01 E0" catch
write(QByteArray::fromHex("03 00 00 16 11 E0 00 00 00 01 00 C0 01 0A C1 02 01 02 C2 02 01 00"));
waitForBytesWritten();
write(QByteArray::fromHex("03 00 00 1B 02 F0 80 32 03 00 00 04 00 00 08 00 00 00 00 F0 00 00 01 00 01 00 F0"));
waitForBytesWritten();
writeDM100FALSE();
}
public slots:
void writeDM100TRUE()
{
//"03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0E 00 05 05 01 12 0A 10 01 00 01 00 00 83 00 03 20 00 03 00 01 01 "TC
//"03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 01" catch
//"03 00 00 24 02 F0 80 32 01 00 00 00 05 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 01"
write(QByteArray::fromHex("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 01"));
waitForBytesWritten();
//write(QByteArray::fromHex("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 01"));
//waitForBytesWritten();
//write(QByteArray::fromHex("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 01"));
//waitForBytesWritten();
}
void writeDM100FALSE()
{
//"03 00 00 24 02 F0 80 32 01 00 00 00 03 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 00"
write(QByteArray::fromHex("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 00"));
waitForBytesWritten();
//write(QByteArray::fromHex("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 00"));
//waitForBytesWritten();
//write(QByteArray::fromHex("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0e 00 05 05 01 12 0a 10 01 00 01 00 00 83 00 03 20 00 03 00 01 00"));
//waitForBytesWritten();
}
void connectToServer(const QString& host, quint16 port) {
connectToHost(host, port);
}
void sendMessage(const QString& message) {
if (state() == QAbstractSocket::ConnectedState) {
write(message.toUtf8());
}
}
signals:
void messageReceived(const QString& message);
private slots:
void onConnected() {
qDebug() << "Connected to server";
}
void onDisconnected() {
qDebug() << "Disconnected from server";
}
void onReadyRead() {
QByteArray data = readAll();
emit messageReceived(QString::fromUtf8(data));
}
void onErrorOccurred(QAbstractSocket::SocketError socketError) {
qDebug() << "Error occurred:" << socketError << errorString();
}
};
//class simpleSiemensTcp :public QThread
//{
// Q_OBJECT
// public:
// QTcpSocket* socket;
// simpleSiemensTcp()
// {
// socket = new QTcpSocket();
// QObject::connect(socket, &QTcpSocket::connected, [&]() {
// //std::cout << "Connected to server." << std::endl;
// //socket->write("hello"); // 发送 "hello"
// //socket->flush(); // 确保数据被发送
// //socket->disconnectFromHost(); // 断开连接
// });
//
// QObject::connect(socket, &QTcpSocket::disconnected, [&]() {
// std::cout << "Disconnected from server." << std::endl;
// });
//
//
// socket->connectToHost(QHostAddress("192.168.50.214"), 102);
// socket->waitForConnected();
// socket->write("hello"); // 发送 "hello"
// socket->flush(); // 确保数据被发送
// socket->waitForBytesWritten();
// }
// void run()
// {
// //while(1)
//
// }
//};