|
IP打印机控制
头文件 #include “QTcpSocket” #include ”QHostAddress“ 源文件 void MainWindow::TcpPrint(QString IP,QString command) { QHostAddress testIP; if (!testIP.setAddress(IP)) { qDebug()<<"Invalid IP Address."; return; } QTcpSocket *tcpSocket = new QTcpSocket(); tcpSocket->connectToHost(QHostAddress( QString::fromStdString(IP.toStdString()) ), 9100); qDebug()<<"Connect to :"+IP+" Status:"< // State: 2; 2 QAbstractSocket::ConnectingState, 3 (QAbstractSocket::ConnectedState) const int Timeout=5*1000; if(!tcpSocket->waitForConnected(Timeout)) return; if (tcpSocket->state()==QAbstractSocket::ConnectedState) { qDebug()<<"Connected:"+IP+" Status:"< tcpSocket->write(code->fromUnicode(command)); tcpSocket->waitForBytesWritten(3000);// after waitForBytesWritten() ,QTcpSocket send data } else { qDebug()<< IP +" NOT connected!"; } delete tcpSocket; } |