需求:

1. 要试试从串口读取数据

2. 将读到的数据从网口发送出去

3.开机启动

4. 没有界面

 

 

第一部分

配置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();
}

2.初始化服务端

 

 

void Serial_To_Internet::initialize_Tcp_Server()
{
    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any,8888);
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(accept_Connect()));

}
</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()));
}


3当串口接收到到数据时,发送

 

 

void Serial_To_Internet::read_SerialPort()
{
    QByteArray temp;
    temp = my_SerialPort->readAll();
    if(my_Socket)
    {
        my_Socket->write(temp);

    }
    ui->textEdit->append(temp);
    temp.clear();
}