我正在尝试连接到PLC设备。设备配置正确,通过ModbusSlave软件成功连接并测试了PC机与PLC设备之间的通信。
我的QT代码是:
PLC = new QModbusTcpClient();
m_port = 502;
m_host = "10.100.101.2"; //PLC IP
PLC->setConnectionParameter(QModbusDevice::NetworkPortParameter, this->m_port);
PLC->setConnectionParameter(QModbusDevice::NetworkAddressParameter, this->m_host);
PLC->setTimeout(5000);
PLC->setNumberOfRetries(10);
qDebug()<<"State: " << PLC->state();
qDebug()<<"Try to connect...";
if(!PLC->connectDevice()){
qDebug()<<"Not connected to PLC: " << PLC->errorString();
}
else{
qDebug()<<"State: " << PLC->state();
while(PLC->state()!=QModbusDevice::ConnectedState){
QThread::msleep(5);
}
qDebug()<<"Connection to PLC succesed" << PLC->state();
emit PLCConnected();
}输出为:
状态: QModbusDevice::UnconnectedState尝试连接...状态: QModbusDevice::ConnectingState
发布于 2020-06-02 02:37:48
首先,PLC->connectDevice()做了什么?
其次,您不能等待同一线程中的连接,只需订阅QModbusDevice::stateChanged信号并查看状态更改
https://stackoverflow.com/questions/62132488
复制相似问题