实例介绍
【实例截图】
【核心代码】
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QTcpSocket> #include <QHostAddress> #include <QDebug> #include <QtEndian> #include <QtMath> int MainWindow::m_count = 0; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_pTcpOrderSocket = new QTcpSocket(this); connect(m_pTcpOrderSocket, &QTcpSocket::connected, this, &MainWindow::connectSecusseful); connect(m_pTcpOrderSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(ReadError(QAbstractSocket::SocketError))); connect(m_pTcpOrderSocket,&QTcpSocket::readyRead, this, &MainWindow::readDataFromSocket); connect(m_pTcpOrderSocket, &QTcpSocket::disconnected, this, &MainWindow::disconnected); m_pTcpHighSpeedSocket = new QTcpSocket(this); connect(m_pTcpHighSpeedSocket, &QTcpSocket::connected, this, &MainWindow::highSpeedConnectSecusseful); connect(m_pTcpHighSpeedSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(highSpeedReadError(QAbstractSocket::SocketError))); connect(m_pTcpHighSpeedSocket,&QTcpSocket::readyRead, this, &MainWindow::highSpeedReadDataFromSocket); connect(m_pTcpHighSpeedSocket, &QTcpSocket::disconnected, this, &MainWindow::highSpeedDisconnected); m_pCommand = new LVJ7000_Network_Protocol; memset(m_pCommand,0,sizeof(LVJ7000_Network_Protocol)); m_reciveAllLength = 0; m_hightReciveAllLength = 0; m_startCode = 0; m_profileDataNumber = 0; m_profileDataUnit = 0; m_firstXCoordinate = 0; m_xDirectionInterval = 0; m_bigOrsmall = Small_machine; m_bigOrsmall = JudgeMachineType(); m_ptime = new QTimer(this); connect(m_ptime, SIGNAL(timeout()), this ,SLOT(timeout())); //m_ptime->start(2000); QStringList args = QApplication::arguments(); } MainWindow::~MainWindow() { m_pTcpOrderSocket->abort(); delete m_pCommand; delete ui; } void MainWindow::timeout() { qDebug() <<"1ms count = " << m_count/2; m_count = 0; } //常规通信 void MainWindow::connectSecusseful() { ui->orderPortMessage->append(QString("...connect to %1:%2").arg(ui->IP->text()).arg(ui->orderPort->text())); } void MainWindow::disconnected() { ui->orderPortMessage->append(QString("...disconnect to %1:%2").arg(ui->IP->text()).arg(ui->orderPort->text())); } void MainWindow::readDataFromSocket() { QByteArray array = m_pTcpOrderSocket->readAll(); if(!array.isEmpty()){ m_ReciveData.append(array); if(m_ReciveData.size() >= static_cast<int>(sizeof(int)) && 0 == m_reciveAllLength){ C4_TO_INT ctoi; ctoi.byte[0] = m_bigOrsmall == Small_machine ? m_ReciveData.at(0):m_ReciveData.at(3); ctoi.byte[1] = m_bigOrsmall == Small_machine ? m_ReciveData.at(1):m_ReciveData.at(2); ctoi.byte[2] = m_bigOrsmall == Small_machine ? m_ReciveData.at(2):m_ReciveData.at(1); ctoi.byte[3] = m_bigOrsmall == Small_machine ? m_ReciveData.at(3):m_ReciveData.at(0); m_reciveAllLength = ctoi.integer; m_reciveAllLength = sizeof(int); ui->orderPortMessage->append(QString("sever will send %1 byte.").arg(m_reciveAllLength)); } if( m_reciveAllLength > sizeof(int) ){ if(static_cast<unsigned int>(m_ReciveData.size()) == m_reciveAllLength){ ui->orderPortMessage->append("...read data from server."); ui->orderPortMessage->append("...there is some data from server."); int nSize = m_ReciveData.size(); ui->orderPortMessage->append(QString("all size = %1").arg(nSize)); anlysisData(m_ReciveData); m_reciveAllLength = 0; m_ReciveData.clear(); ui->sendOrderButton->setDisabled(false); } else{ ui->sendOrderButton->setDisabled(true); } } else{ ui->sendOrderButton->setDisabled(false); m_ReciveData.clear(); m_reciveAllLength = 0; } } } void MainWindow::ReadError(QAbstractSocket::SocketError error) { ui->orderPortMessage->append(QString("...some error %1").arg(error)); } //高速通信 void MainWindow::highSpeedConnectSecusseful() { ui->hightSpeedPortMessage->append(QString("...connect to %1:%2").arg(ui->IP->text()).arg(ui->hightSpeedPort->text())); } void MainWindow::highSpeedDisconnected() { ui->hightSpeedPortMessage->append(QString("...disconnect to %1:%2").arg(ui->IP->text()).arg(ui->hightSpeedPort->text())); } void MainWindow::highSpeedReadDataFromSocket() { QByteArray array = m_pTcpHighSpeedSocket->readAll(); if(!array.isEmpty()){ m_highReciveData.append(array); bool bOver = false; while (!bOver) { int n = m_highReciveData.size(); if( n >= static_cast<int>(m_iHighSpeedOneDataNum)){ QByteArray oneData = m_highReciveData.left(m_iHighSpeedOneDataNum); anlysisHighSpeedData(oneData); oneData.clear(); oneData = m_highReciveData.right(m_highReciveData.size() - m_iHighSpeedOneDataNum); m_highReciveData.clear(); m_highReciveData.append(oneData); } else{ bOver = true; } } } } void MainWindow::highSpeedReadError(QAbstractSocket::SocketError error) { ui->hightSpeedPortMessage->append(QString("...some error %1").arg(error)); } ///////////////////// void MainWindow::anlysisHighSpeedData(const QByteArray &array) { m_count ; ////just for test static bool bover = false; if(!bover){ bover = true; } else{ return; } /////// ui->hightSpeedPortMessage->append("analysis one profile data."); if(array.size() != static_cast<int>(m_iHighSpeedOneDataNum)){ ui->hightSpeedPortMessage->append("no enough data to get point information!"); return; } char flagAra = array.at(0); ui->hightSpeedPortMessage->append(QString("flage Area = %1").arg(QString::number(flagAra,16),2,'0')); C4_TO_INT ctoi; ctoi.byte[0] = m_bigOrsmall == Small_machine ? array.at(4):array.at(7); ctoi.byte[1] = m_bigOrsmall == Small_machine ? array.at(5):array.at(6); ctoi.byte[2] = m_bigOrsmall == Small_machine ? array.at(6):array.at(5); ctoi.byte[3] = m_bigOrsmall == Small_machine ? array.at(7):array.at(4); unsigned int triggerCount = ctoi.integer; ui->hightSpeedPortMessage->append(QString("triggerCount = %1").arg(triggerCount)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? array.at(8):array.at(11); ctoi.byte[1] = m_bigOrsmall == Small_machine ? array.at(9):array.at(10); ctoi.byte[2] = m_bigOrsmall == Small_machine ? array.at(10):array.at(9); ctoi.byte[3] = m_bigOrsmall == Small_machine ? array.at(11):array.at(8); unsigned int encoderCount = ctoi.integer; ui->hightSpeedPortMessage->append(QString("encoderCount = %1").arg(encoderCount)); //return; QByteArray data = array.left(2024).right(2000); ui->hightSpeedPortMessage->append("binary data."); for (int i = 0; i < 400; i) { unsigned char c1 = data.at(i*5); unsigned char c2 = data.at(i*5 1); unsigned char c3 = data.at(i*5 2); unsigned char c4 = data.at(i*5 3); unsigned char c5 = data.at(i*5 4); ui->hightSpeedPortMessage->append(QString("%1 %2 %3 %4 %5").arg(QString::number(c1,2),8,'0')\ .arg(QString::number(c2,2),8,'0')\ .arg(QString::number(c3,2),8,'0')\ .arg(QString::number(c4,2),8,'0')\ .arg(QString::number(c5,2),8,'0')); } // C4_TO_INT1 ctoi1; // for (int i = 0; i < 500; i) { // unsigned char c1 = data.at(i*4); // unsigned char c2 = data.at(i*4 1); // unsigned char c3 = data.at(i*4 2); // unsigned char c4 = data.at(i*4 3); // ctoi1.byte[0] = c1; // ctoi1.byte[1] = c2; // ctoi1.byte[2] = c3; // ctoi1.byte[3] = c4; // double d = static_cast<double>(ctoi1.integer); // double dst = d*m_profileDataUnit*0.001; // ui->hightSpeedPortMessage->append(QString::number(dst,'f',5)); // } ui->hightSpeedPortMessage->append("************************************"); } QString MainWindow::anlysisData(const QByteArray &array) { if(array.isEmpty()) return QString("there is no data!"); C4_TO_INT ctoi; //length 4 if(array.size() < 4) return QString("no enough data to get length information!"); QByteArray lengthByte = array.left(4); ctoi.byte[0] = m_bigOrsmall == Small_machine ? lengthByte.at(0):lengthByte.at(3); ctoi.byte[1] = m_bigOrsmall == Small_machine ? lengthByte.at(1):lengthByte.at(2); ctoi.byte[2] = m_bigOrsmall == Small_machine ? lengthByte.at(2):lengthByte.at(1); ctoi.byte[3] = m_bigOrsmall == Small_machine ? lengthByte.at(3):lengthByte.at(0); int length = ctoi.integer; ui->orderPortMessage->append(QString("recive all length(10) %1").arg(length)); //header 12 if(array.size() < 16) return QString("no enouth data to get header information!"); QByteArray headerByte = array.left(16).right(12); unsigned char fixed1 = headerByte.at(0); unsigned char fixed4 = headerByte.at(3); unsigned char hReturnCode = headerByte.at(4); ui->orderPortMessage->append(QString("recvie header fixed1 %1").arg(QString::number(fixed1,16),2,'0')); ui->orderPortMessage->append(QString("recvie header fixed4 %1").arg(QString::number(fixed4,16),2,'0')); ui->orderPortMessage->append(QString("recvie header return code %1").arg(QString::number(hReturnCode,16),2,'0')); QByteArray bodyLengthByte = headerByte.right(4); ctoi.byte[0] = m_bigOrsmall == Small_machine ? bodyLengthByte.at(0):bodyLengthByte.at(3); ctoi.byte[1] = m_bigOrsmall == Small_machine ? bodyLengthByte.at(1):bodyLengthByte.at(2); ctoi.byte[2] = m_bigOrsmall == Small_machine ? bodyLengthByte.at(2):bodyLengthByte.at(1); ctoi.byte[3] = m_bigOrsmall == Small_machine ? bodyLengthByte.at(3):bodyLengthByte.at(0); unsigned int bodyLength = ctoi.integer; ui->orderPortMessage->append(QString("recive body length(10) %1").arg(bodyLength)); //body 12 response data size QByteArray bodyByte = array.right(bodyLength); if(bodyByte.size() < 12) return QString("no enough data to get body information!"); unsigned char rCommandCode = bodyByte.at(0); if( m_pCommand->command_code != rCommandCode){ ui->orderPortMessage->append("send command and back command is not same,Please check!!!"); return QString(); } unsigned char bReturnCode = bodyByte.at(1); unsigned char contollerStatus = bodyByte.at(2); unsigned char activeProgramNo = bodyByte.at(8); ui->orderPortMessage->append(QString("recive body command code %1").arg(QString::number(rCommandCode,16),2,'0')); ui->orderPortMessage->append(QString("recive body return code %1").arg(QString::number(bReturnCode,16),2,'0')); ui->orderPortMessage->append(QString("recive body controller status %1").arg(QString::number(contollerStatus,16),2,'0')); ui->orderPortMessage->append(QString("recive body active program No. %1").arg(QString::number(activeProgramNo,16),2,'0')); //response data QByteArray responseData = bodyByte.right(bodyByte.size()-12); switch (rCommandCode) { case 0x49:{ // m_count ; // emit ui->sendOrderButton->clicked(); // return QString(); QByteArray responseDataFont = responseData.left(56); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(0):responseDataFont.at(3); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(1):responseDataFont.at(2); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(2):responseDataFont.at(1); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(3):responseDataFont.at(0); unsigned int currentProfileNumber = ctoi.integer; ui->orderPortMessage->append(QString("currentProfileNumber %1").arg(currentProfileNumber)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(4):responseDataFont.at(7); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(5):responseDataFont.at(6); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(6):responseDataFont.at(5); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(7):responseDataFont.at(4); unsigned int unreadProfileNumber = ctoi.integer; ui->orderPortMessage->append(QString("unreadProfileNumber %1").arg(unreadProfileNumber)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(8):responseDataFont.at(11); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(9):responseDataFont.at(10); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(10):responseDataFont.at(9); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(11):responseDataFont.at(8); unsigned int readProfileNumberThisTime = ctoi.integer; ui->orderPortMessage->append(QString("readProfileNumberThisTime %1").arg(readProfileNumberThisTime)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(16):0; ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(17):0; ctoi.byte[2] = m_bigOrsmall == Small_machine ? 0:responseDataFont.at(17); ctoi.byte[3] = m_bigOrsmall == Small_machine ? 0:responseDataFont.at(16); unsigned int xDirectionStartPoint = ctoi.integer; ui->orderPortMessage->append(QString("xDirectionStartPoint %1").arg(xDirectionStartPoint)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(18):0; ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(19):0; ctoi.byte[2] = m_bigOrsmall == Small_machine ? 0:responseDataFont.at(19); ctoi.byte[3] = m_bigOrsmall == Small_machine ? 0:responseDataFont.at(18); unsigned int xDirectionCount = ctoi.integer; ui->orderPortMessage->append(QString("xDirectionCount %1").arg(xDirectionCount)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(20):0; ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(21):0; ctoi.byte[2] = m_bigOrsmall == Small_machine ? 0:responseDataFont.at(21); ctoi.byte[3] = m_bigOrsmall == Small_machine ? 0:responseDataFont.at(20); unsigned int profileDataCount = ctoi.integer; ui->orderPortMessage->append(QString("profileDataCount %1").arg(profileDataCount)); C4_TO_INT1 ctoi1; ctoi1.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(24):responseDataFont.at(27); ctoi1.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(25):responseDataFont.at(26); ctoi1.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(26):responseDataFont.at(25); ctoi1.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(27):responseDataFont.at(24); double xFirstPos = static_cast<double>(ctoi1.integer)*0.01*0.001; ui->orderPortMessage->append(QString("xFirstPos %1 mm").arg(xFirstPos)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(28):responseDataFont.at(31); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(29):responseDataFont.at(30); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(30):responseDataFont.at(29); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(31):responseDataFont.at(28); double xDirectionInterval = static_cast<double>(ctoi.integer)*0.01*0.001; ui->orderPortMessage->append(QString("xDirectionInterval %1 mm").arg(xDirectionInterval)); char flagArea = responseData.at(32); ui->orderPortMessage->append(QString("flag area %1").arg(QString::number(flagArea,16),2,'0')); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(36):responseDataFont.at(39); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(37):responseDataFont.at(38); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(38):responseDataFont.at(37); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(39):responseDataFont.at(36); unsigned int TriggerCount = ctoi.integer; ui->orderPortMessage->append(QString("TriggerCount %1").arg(TriggerCount)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataFont.at(40):responseDataFont.at(43); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataFont.at(41):responseDataFont.at(42); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataFont.at(42):responseDataFont.at(41); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataFont.at(43):responseDataFont.at(40); unsigned int EncoderCount = ctoi.integer; ui->orderPortMessage->append(QString("EncoderCount %1").arg(EncoderCount)); if(static_cast<unsigned int>(responseData.size())-56 == profileDataCount*4 4) { QByteArray responseDataPoints = responseData.right(responseData.size()-56); if(xDirectionCount <= profileDataCount-xDirectionStartPoint){ for (unsigned int i = xDirectionStartPoint; i < xDirectionCount; i) { ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseDataPoints.at(i*4):responseDataPoints.at(i*4 3); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseDataPoints.at(i*4 1):responseDataPoints.at(i*4 2); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseDataPoints.at(i*4 2):responseDataPoints.at(i*4 1); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseDataPoints.at(i*4 3):responseDataPoints.at(i*4); int tempInteger = static_cast<int>(ctoi.integer); tempInteger = tempInteger > 9999990 ? 9999990:tempInteger; tempInteger = tempInteger < -9999990 ? -9999990:tempInteger; double dstdata = static_cast<double>(tempInteger) / 100000; double tx = i*xDirectionInterval xFirstPos; ui->orderPortMessage->append(QString("Point %1 x:%2 y:%3").arg(i).arg(QString::number(tx,'f',2)).arg(QString::number(dstdata,'f',4))); } } } else{ ui->orderPortMessage->append("no enough data to get points!!"); } break; } case 0x47:{ int responseDataSize = responseData.size(); if(responseDataSize < 28){ ui->orderPortMessage->append("no enough data to get high speed information!"); break; } ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseData.at(4):responseData.at(7); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseData.at(5):responseData.at(6); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseData.at(6):responseData.at(5); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseData.at(7):responseData.at(4); m_startCode = ctoi.integer; ui->orderPortMessage->append(QString("start code %1").arg(m_startCode)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseData.at(16):0; ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseData.at(17):0; ctoi.byte[2] = m_bigOrsmall == Small_machine ? 0:responseData.at(17); ctoi.byte[3] = m_bigOrsmall == Small_machine ? 0:responseData.at(16); m_profileDataNumber = ctoi.integer; ui->orderPortMessage->append(QString("profileDataNumber %1").arg(m_profileDataNumber)); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseData.at(18):0; ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseData.at(19):0; ctoi.byte[2] = m_bigOrsmall == Small_machine ? 0:responseData.at(19); ctoi.byte[3] = m_bigOrsmall == Small_machine ? 0:responseData.at(18); m_profileDataUnit = static_cast<double>(ctoi.integer)*0.01; ui->orderPortMessage->append(QString("profileDataUnit %1 um").arg(QString::number(m_profileDataUnit,'f',4))); C4_TO_INT1 ctoi1; ctoi1.byte[0] = m_bigOrsmall == Small_machine ? responseData.at(20):responseData.at(23); ctoi1.byte[1] = m_bigOrsmall == Small_machine ? responseData.at(21):responseData.at(22); ctoi1.byte[2] = m_bigOrsmall == Small_machine ? responseData.at(22):responseData.at(21); ctoi1.byte[3] = m_bigOrsmall == Small_machine ? responseData.at(23):responseData.at(20); m_firstXCoordinate = static_cast<double>(ctoi1.integer)*0.01*0.001; ui->orderPortMessage->append(QString("firstXCoordinate %1 mm").arg(QString::number(m_firstXCoordinate,'f',7))); ctoi.byte[0] = m_bigOrsmall == Small_machine ? responseData.at(24):responseData.at(27); ctoi.byte[1] = m_bigOrsmall == Small_machine ? responseData.at(25):responseData.at(26); ctoi.byte[2] = m_bigOrsmall == Small_machine ? responseData.at(26):responseData.at(25); ctoi.byte[3] = m_bigOrsmall == Small_machine ? responseData.at(27):responseData.at(24); m_xDirectionInterval = static_cast<double>(ctoi.integer)*0.01*0.001; ui->orderPortMessage->append(QString("xDirectionInterval %1 mm").arg(QString::number(m_xDirectionInterval,'f',7))); m_iHighSpeedOneDataNum = m_profileDataNumber*20/8 28; break; } case 0xA0:{ } break; case 0x48:{ } break; default: break; } return QString(); } //连接 lvj7000 命令端口 void MainWindow::on_connectButton_clicked() { m_pTcpOrderSocket->abort(); m_pTcpOrderSocket->connectToHost(QHostAddress(ui->IP->text()), ui->orderPort->text().toInt()); } //断开 lvj7000 的连接 void MainWindow::on_disconnectButton_clicked() { m_pTcpOrderSocket->disconnectFromHost(); m_pTcpHighSpeedSocket->disconnectFromHost(); } //发送命令 void MainWindow::on_sendOrderButton_clicked() { if(m_pTcpOrderSocket->state() == QAbstractSocket::UnconnectedState) return; //analysis command buildOrderMessage(ui->order->text().toInt(Q_NULLPTR,16)); //send command data to server char *pData = reinterpret_cast<char*>(m_pCommand); qint64 backNum = m_pTcpOrderSocket->write((char*)pData,m_pCommand->all_length sizeof(int)); //show command data ui->orderPortMessage->append("...write data to server."); ui->orderPortMessage->append(QString("...write data size: %1").arg(backNum)); ui->orderPortMessage->append(QByteArray(pData,sizeof(LVJ7000_Network_Protocol)).toHex()); ui->orderPortMessage->append(QString("all length(10): %1").arg(QString::number(m_pCommand->all_length,10))); ui->orderPortMessage->append(QString("fixed 1: %1").arg(QString::number(m_pCommand->header_fixed1,16),2,'0')); ui->orderPortMessage->append(QString("fixed 2: %1").arg(QString::number(m_pCommand->header_fixed3,16),2,'0')); ui->orderPortMessage->append(QString("body size(10): %1").arg(QString::number(m_pCommand->command_body_length,10))); ui->orderPortMessage->append(QString("command code: %1").arg(QString::number(m_pCommand->command_code,16),2,'0')); } void MainWindow::buildOrderMessage(int command) { m_pCommand->header_fixed1 = 0x01; m_pCommand->header_fixed3 = 0xF0; C4_TO_INT ctoi; switch (command) { case 0x49: //命令模式获取轮廓 m_pCommand->all_length = sizeof(LVJ7000_Network_Protocol)-sizeof(unsigned int); m_pCommand->command_body_length = 0x14; m_pCommand->command_code = command; m_pCommand->command_data_fixed15 = 0x20; m_pCommand->command_data_fixed16 = 0x03; break; case 0x47://准备高速通信 m_pCommand->all_length = sizeof(LVJ7000_Network_Protocol)-sizeof(unsigned int)-12; m_pCommand->command_data_fixed1 = 0x02;//从下一个数据 m_pCommand->command_code = command; m_pCommand->command_body_length = 0x08; ui->orderPortMessage->append("...prepare high speed network!!"); break; case 0xA0://开始高速通信获取轮廓 //连接 lvj7000 高速通信端口 if(m_startCode == 0) { ui->orderPortMessage->append("no start code"); break; } m_pTcpHighSpeedSocket->abort(); m_pTcpHighSpeedSocket->connectToHost(QHostAddress(ui->IP->text()), ui->hightSpeedPort->text().toInt()); m_pCommand->all_length = sizeof(LVJ7000_Network_Protocol)-sizeof(unsigned int)-8; m_pCommand->command_code = command; m_pCommand->command_body_length = 0x0C; m_pCommand->command_data_fixed1 = 0x47; ctoi.integer = m_startCode; m_pCommand->command_data_fixed5 = ctoi.byte[0]; m_pCommand->command_data_fixed6 = ctoi.byte[1]; m_pCommand->command_data_fixed7 = ctoi.byte[2]; m_pCommand->command_data_fixed8 = ctoi.byte[3]; ui->orderPortMessage->append("...begin high speed network!!!"); break; case 0x48://停止高速通信 if(m_startCode == 0) { ui->orderPortMessage->append("no start code"); break; } m_pCommand->all_length = sizeof(LVJ7000_Network_Protocol)-sizeof(unsigned int)-16; m_pCommand->command_code = command; m_pCommand->command_body_length = 0x04; m_pTcpHighSpeedSocket->disconnectFromHost();//最后关闭连接 ui->orderPortMessage->append("...stop high speed network!!!"); break; default: break; } }
标签: 3d
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论