单片机 > 单片机程序设计 > 详情

用qt编写上位机与stm32通信

发布时间:2020-08-25 发布时间:
|



本文采用的的开发环境是VS2010+Qt5.5.1版本,所有程序不是通过Qt Creator编译的,如果有需要可以介绍VS2010和Qt环境的搭建和简单的使用。


QSerialPort

QSerialPort这个类是从QT5.1开始引入的,之前都是通过QIODevice自己定义串口类,从而实现串口程序的开发。现在引入这个类了,将会非常方便的开发串口程序。为了使用这个类,需要在工程目录和附加依赖项中加入include的路径,以及链接库的路径,以及链接库的名称:


项目—>属性—> C++ —> 常规 —>C:QtQt5.5.15.5msvc2010includeQtSerialPort项目—>属性—> 输入++ —> 附加依赖项—>Qt5SerialPort.lib(如果是Debug版本,则是Qt5SerialPortd.lib)版本源文件或者头文件需要加入#include 串口的信息可以通过QSerialPortInfo类获得,通过这个类,你可以正确的确定你要开启的串口,同时可以获得串口的描述性信息以及厂家信息。串口有三种打开模式,即ReadOnly,WriteOnly,以及ReadWrite。同时可以设置其停止位,波特率,数据位,校验方式以及流控,对应的函数方式分别为:setStopBits(),setBaudRates(),setDataBits(),setParity(),以及setFlowControl()。 


串口数据的写入是通过writeData(const char * data, qint64 maxSize)以及write(const char * data, qint64 maxSize),前者是protected属性,只能在子类中访问,而后者则是public属性。在串口打开并且具有可写属性,即可通过write函数写入数据。


串口数据的读入是通过 readData(char * data, qint64 maxSize) ,read(qint64 maxSize)实现的,如果需要一次性读完所有的数据,则可以通过readAll()全部读取串口缓冲区中的数据。


串口内部的缓冲区大小可以通过:setReadBufferSize(qint64 size)实现。当设定缓冲区大小时,串口只能接收size大小的数据流,因此存在数据丢书的可能。当设置为0的时候,并不是指的缓冲区大小为0,而是无穷大,这样就可以保存数据的全部接收完整。这是缓冲区大小的默认属性。


是否有新的数据读入缓冲区是通过readReady()信号来来确定的。这是通过时间驱动的方式来确定是否有数据可以读入。此外还有waitForReadyRead()来等待轮询是否有数据到达串口,但是这是一种阻塞性读入,个人觉得不是太好,所以写串口的时候采用的是事件驱动的方式来确定是够有数据可以读入。


QSerialPortInfo

Use the static functions to generate a list of QSerialPortInfo objects. Each QSerialPortInfo object in the list represents a single serial port and can be queried for the port name, system location, description, and manufacturer. The QSerialPortInfo class can also be used as an input parameter for the setPort() method of the QSerialPort class.


可能会用得比较多的函数是description(),manufacturer(),以及serialNumber()。从而得到描述性信息,比如通信端口。USB转串口等描述串口的信息、串口的生产商信息以及串口名,在电脑上表现为COM~等。


如何获取电脑上所有的串口

void CameraSerialPort::getSerialPortID()

{

     serialInfo=new QSerialPortInfo();

     serialList=serialInfo->availablePorts();

    int nSerialnum=serialList.length();

    for(int i=0;iaddItem(serialName);

    }


    QString currentPort=serialPortAssitant.serialPortcomboBox->currentText();

    portToOpen=currentPort;

    QString  portStatus=currentPort+" closed";

    serialPortAssitant.status->statusInfo->setText(portStatus.toUpper());

    QPalette font_palette;

    font_palette.setColor(QPalette::WindowText,Qt::red);

    serialPortAssitant.status->statusInfo->setPalette(font_palette);


因为直接从自己的项目文件拷过来的源码,这里稍微介绍一下属性:


1、变量的定义,在头文件中,这里没有贴出来,截取定义如下:


QSerialPortInfo* serialInfo;

QListserialList;

1

2

2、QListavailablePorts() 返回的是一个关于QSerialPortInfo的列表,在数据结构QList中存储。 

3、serialPortcomBox是一个QComboBox,下拉列表。


4、最后几行是用来显示串口的状态信息,达到的效果如图: 

QString sendMsg=serialPortAssitant.sendLine->text();

    QByteArray temp=sendMsg.toLatin1();

    if(IsSendByHex)

    {

        temp=HexStrToByteArray(sendMsg);

    }

    char *sendContent=temp.data();

    qint64 sendedNum=serialPort->write(sendContent,temp.count());


    //---------------判断发送数据是否成功----------------------//

    if(sendedNum==-1)

    {

        errorValue=serialPort->error();

        if(IsShowCurrentTime)

        {

            errorInfo=" ";

            currentTime=QDateTime::currentDateTime();

            timeinfo=currentTime.toString("yyyy__MM__d__hh:mm:ss");

            errorInfo=QString::fromLocal8Bit("错误提示信息   ");

            errorInfo+=timeinfo;

            errorInfo+="n";

        }

        else

        {

            errorInfo=" ";

            errorInfo=QString::fromLocal8Bit("错误提示信息   ");

            errorInfo+="n";

        }

        serialPortAssitant.ReciveWidget->append(errorInfo+getValueContent(errorValue));

        return;

    }

    //-------------显示发送数据-----------------------//


    //temp的size的依据是是否以16进制发送

    sendCount+=temp.count();

    serialPortAssitant.status->TxByte->setText(QString::number(sendCount));

    QString showSendMsg;

    if(IsShowSendMsg)

    {

        if(IsShowCurrentTime)

        {


            currentTime=QDateTime::currentDateTime();

            timeinfo=currentTime.toString("yyyy__MM__d__hh:mm:ss");

            showSendMsg+=QString::fromLocal8Bit("发送数据 : ");

            showSendMsg+=timeinfo;

            showSendMsg+="n";

            //判断显示16进制还是ACSII字符

            if(IsSendByHex)

                showSendMsg+=ByteArrayToHexStr(temp);

            else

                showSendMsg+=temp;

        }   

        else

        {

            showSendMsg=QString::fromLocal8Bit("发送数据 : ");

            if(IsSendByHex)

                showSendMsg+=ByteArrayToHexStr(temp);

            else

                showSendMsg+=temp;


        }   

        serialPortAssitant.ReciveWidget->append(showSendMsg);


    }   

    IsWrittenSuccessed=true;

}


void CameraSerialPort::sendData()

{

    if(!IsSerialPortOpen)

    {

            if(serialPort!=NULL)

            {

                serialPort->close();

            }

            serialPort=new QSerialPort(portToOpen);

            if(serialPort==NULL)

            {

                errorValue=serialPort->error();

                QString errorContent=getValueContent(errorValue);

                if(IsShowCurrentTime)

                {

                        errorInfo=" ";

curr


关键字:qt编写  上位机  stm32通信 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
PIC单片机基础知识之二