第一部分
配置Qt Pro文件 需要Qt += serialport network
第二部分 -串口初始化
void Serial_To_Internet::initialize_SerialPort() { foreach (const QSerialPortInfo &infor, QSerialPortInfo::availablePorts()) { QSerialPort serial; serial.setPort(infor); if(serial.open(QIODevice::ReadWrite)) { ui->comboBox->addItem(infor.portName()); ui->textEdit->append(tr("检测到端口列表")); ui->textEdit->append(infor.portName()); ui->textEdit->append(infor.description()); ui->textEdit->append(infor.manufacturer()); serial.close(); } } ui->comboBox_2->setCurrentIndex(0); my_SerialPort = new QSerialPort(); my_SerialPort->setPortName("COM2"); my_SerialPort->open(QIODevice::ReadWrite); my_SerialPort->setBaudRate(9600); my_SerialPort->setDataBits(QSerialPort::Data8); my_SerialPort->setParity(QSerialPort::NoParity); my_SerialPort->setStopBits(QSerialPort::OneStop); my_SerialPort->setFlowControl(QSerialPort::NoFlowControl); connect(my_SerialPort,SIGNAL(readyRead()),this,SLOT(read_SerialPort())); }
第三部分-设置开机启动
void Serial_To_Internet::set_Auto_Start(bool is_auto_start) { QString application_name = QApplication::applicationName(); QSettings *settings = new QSettings(REG_RUN,QSettings::NativeFormat); if(is_auto_start) { QString application_path = QApplication::applicationFilePath(); settings->setValue(application_name,application_path.replace("/","\\")); } else { settings->remove(application_name); } delete settings; }
第四部分 设置最小化
oid Serial_To_Internet::onSystemTrayIconClisked(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::Trigger: { if(this->isHidden()) { this->show(); this->setWindowState(Qt::WindowActive); this->activateWindow(); } else { this->hide(); } } break; case QSystemTrayIcon::DoubleClick: default: break; } } void Serial_To_Internet::changeEvent(QEvent *e) { if((e->type() == QEvent::WindowStateChange)&&this->isMinimized()) { this->hide(); } }
第五部分 网络部分
1. 设置静态IP
void Serial_To_Internet::set_Static_Ip() { QProcess process(0); process.start("netsh interface ip set address 本地连接 static 192.168.2.63 255.255.255.0 192.168.2.254"); process.waitForStarted(); process.waitForFinished(); }
</pre><pre code_snippet_id="1610400" snippet_file_name="blog_20160315_7_4027708" name="code" class="cpp">void Serial_To_Internet::accept_Connect() { qDebug()<<"ASdsasa"; my_Socket = tcpServer->nextPendingConnection(); connect(my_Socket,SIGNAL(readyRead()),this,SLOT(read_Date_From_Internet())); }