qt creator 4.11.1
qt5.14.1
设置连接超时时间,连接失败自动退出
waitForConnected(500);
设置断开连接触发关闭连接函数
connect(socket, &QTcpSocket::disconnected, this, &Dialog::socket_Disconnected);
经过如上操作后,实现异常连接自动识别断开和初次连接不上自动断开不死连。
pro里面添加
QT += network
DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QTcpServer>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
void ReadData();
void socket_Disconnected();
void on_pushButton_connect_clicked();
void on_pushButton_disconnect_clicked();
private:
Ui::Dialog *ui;
uint16_t ModbusCRC16(QByteArray senddata);
QTcpSocket* socket;//创建socket指针
};
#endif // DIALOG_H
DIALOG.app
Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
/
socket = new QTcpSocket(this);
ui->pushButton_disconnect->setEnabled(false);//按钮使能
ui->pushButton_connect->setEnabled(true);//按钮使能
ui->lineEdit_ip->setText("192.168.0.178");
ui->lineEdit_port->setText("4196");
}
Dialog::~Dialog()
{
socket->disconnectFromHost();
delete this->socket;//回收内存
delete ui;
}
void Dialog::on_pushButton_connect_clicked()
{
QString ip = ui->lineEdit_ip->text();//获取ip
int port = ui->lineEdit_port->text().toInt();//获取端口数据
socket->disconnectFromHost();
//取消已有的连接
socket->abort();
//连接服务器
socket->connectToHost(ip, port);
bool isconnect = socket->waitForConnected(500);//等待直到连接成功
//如果连接成功
if (isconnect)
{
Control_Hidden(10);
ui->pushButton_disconnect->setEnabled(true);//按钮使能
ui->pushButton_connect->setEnabled(false);//按钮使能
ui->lineEdit_ip->setEnabled(false);//
ui->lineEdit_port->setEnabled(false);//
//接收缓冲区(服务器)信息
connect(socket, &QTcpSocket::readyRead, this, &Dialog::ReadData);
connect(socket, &QTcpSocket::disconnected, this, &Dialog::socket_Disconnected);
ui->label_message->setText("连接成功!!");
}
else
{
ui->label_message->setText("连接超时");
}
}
void Dialog::on_pushButton_disconnect_clicked()
{
socket_Disconnected();
}
void Dialog::socket_Disconnected()
{
socket->disconnectFromHost();
ui->pushButton_connect->setEnabled(true);//按钮使能
ui->pushButton_disconnect->setEnabled(false);//按钮使能
ui->lineEdit_ip->setEnabled(true);//
ui->lineEdit_port->setEnabled(true);//
ui->label_message->setText("not connected !!");
Control_Hidden(100);
}
//接收缓冲区信息函数
void Dialog::ReadData()
{
QByteArray buf = socket->readAll();
int i,j,len,ret = 0;
len = buf.length();
/
if(len > 0)
{
QByteArray buf_dis;
uint8_t temp[len];
QString str;
buf_dis = buf;
ui->label_message->setText(buf_dis.toHex(' ').toUpper());
for(i = 0;i < len;i ++)
{
temp[i] = buf.at(i);
}
}
uint16_t Dialog::ModbusCRC16(QByteArray senddata)
{
int len=senddata.size();
uint16_t wcrc=0XFFFF;//预置16位crc寄存器,初值全部为1
uint8_t temp;//定义中间变量
int i=0,j=0;//定义计数
for(i=0;i<len;i++)//循环计算每个数据
{
temp=senddata.at(i);
wcrc^=temp;
for(j=0;j<8;j++){
//判断右移出的是不是1,如果是1则与多项式进行异或。
if(wcrc&0X0001){
wcrc>>=1;//先将数据右移一位
wcrc^=0XA001;//与上面的多项式进行异或
}
else//如果不是1,则直接移出
wcrc>>=1;//直接移出
}
}
temp=wcrc;//crc的值
return wcrc;
}
void Dialog::send_order(int g)
{
uint16_t crc;
QString str;
uint16_t DA;
QByteArray data;
str = "AB CD 00 00 00 02 04 50 60";
data = QByteArray::fromHex(str.toLatin1());
DA = ui->lineEdit_fullDA_set->text().toInt();
data.append((0xff00 & DA) >> 8);
data.append(0xff & DA);
crc = ModbusCRC16(data);
data.append(0xff & crc);
data.append((0xff00 & crc) >> 8);
socket->write(data);
//判断是否写入成功
bool iswrite = socket->waitForBytesWritten();
if (iswrite)
{
count_sucess ++;
}
else
{
count_err++;
}
ui->label_message_1->setText("");
}