Realizing the communication between PIC microcontroller and QT host computer based on RS232 serial port

Publisher:HeavenlySunsetLatest update time:2020-01-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: The host computer sends a message to the slave computer through the RS232 serial port. The slave computer receives the message and performs two actions:


(1) LED light changes.


(2) Return the message to the host computer through the RS232 serial port;


After receiving the message, the host computer displays the content on the UI interface.


Windows7 64-bit environment complete source code and source program package download: https://download.csdn.net/download/robin_xx/10791050


The main code is pasted below:


PIC microcontroller lower machine source program:


#include

__CONFIG(HS&WDTDIS&LVPDIS);

unsigned char i=0;

//void interrupt ISR(void);

void interrupt usart(void);

void Init(void); //initialization

unsigned char* sendData="";

 

const unsigned char led10[]=//8 LED running lights code table

    {

        0B00000000,

        0B00000001,

        0B00000010,

        0B00000100,

        0B00001000,

        0B00010000,

        0B00100000,

        0B01000000,

        0B10000000,

        0B11111111

    };

void main(void)

{

 

    Init();

    while(1)

    {

        if (i==10)

            i=0;

        PORTB=led10[i];

    }

}

 

 

/*void interrupt ISR(void)//

{

    if(INTF==1)//button interrupt

    {

        INTF=0; // clear interrupt flag

        i+=1; //Each time an interrupt occurs, the running light starts lighting up from the beginning

    }

}*/

 

void interrupt usart(void)

{

    if(RCIE&&RCIF) //Judge whether it is a serial port receiving interrupt

    {

        i+=1;

        while(!TRMT); //Wait for acceptance

        sendData=RCREG; 

        //i=sendData;

    }

    if(sendData!="")

    {    

        RCIE=0; //Turn off the receive interrupt enable bit before sending

        TXREG = sendData; //Send the received data back

        while(!TXIF); //Wait for sending to complete

        RCIE = 1; //Send completed, enable receive interrupt enable bit

        sendData="";

    }

}

 

void Init(void)

{

    TRISB=0B00000000; //control 8 LEDs, output port

    PORTB=0B00000000;//

 

    // Initialize port program

    TRISC7 = 1; //RX port is set to input valid

    TRISC6 = 0; //TX port is set to output valid

 

    //SPBRG = 0X19; //Set the baud rate to 9600BPS

    SPBRG = 0XC; //Set the baud rate to 19200BPS

 

    //TXSTA = 0x04; //Asynchronous communication - prohibit sending data - high speed mode

    TXSTA = 0X24; // Enable serial port transmission and select high-speed baud rate

 

    //RCSTA = 0xA0; //Serial port enable - receive single character - disable continuous reception

    RCSTA = 0X90; // Enable serial port operation and continuous reception

 

    RCIE=0X1; //Enable receive interrupt

    GIE=0X1; //Open global interrupt

    PEIE=0X1; //Enable external interrupt

}

QT host computer source program:


mainwindow.h


#ifndef MAINWINDOW_H

#define MAINWINDOW_H

 

#include

#include

#include

 

namespace Ui {

class MainWindow;

}

 

class MainWindow : public QMainWindow

{

    Q_OBJECT

 

public:

    QSerialPort *serial;

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

 

 

private slots:

 

    void on_pushButton_send_clicked();

 

    void on_pushButton_openPort_clicked();

 

    void read_Com();

 

    void on_pushButton_ClearRec_clicked();

 

 

private:

    Ui::MainWindow *ui;

};

 

#endif // MAINWINDOW_H

 


mainwindow.cpp

 


#include "mainwindow.h"

#include "ui_mainwindow.h"

 

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);

 

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())

    {

        QSerialPort serial;

        serial.setPort(info);

        //Judge whether the serial port can be opened

        if (serial.open(QIODevice::ReadWrite))

        {

            QString portStr = info.portName();

            ui->comboBox_portList->addItem(portStr);

            serial.close();

        }

     }

 

    ui->comboBox_portList->setCurrentIndex(0);

    ui->comboBox_BPS->addItem("7200");

    ui->comboBox_BPS->addItem("9600");

    ui->comboBox_BPS->addItem("14400");

    ui->comboBox_BPS->addItem("19200");

    ui->comboBox_BPS->addItem("38400");

    ui->comboBox_BPS->addItem("57600");

 

    ui->comboBox_BPS->setCurrentIndex(3);

 

    on_pushButton_openPort_clicked();

    on_pushButton_openPort_clicked();

 

}

 

MainWindow::~MainWindow()

{

    delete ui;

}

 

 

//Read the received information

 void MainWindow::read_Com()

 {

     QByteArray temp=serial->readAll();

     if(!temp.isEmpty()) //If the read data is not empty

     {

         ui->textBrowser_RecList->insertPlainText(temp);

     }

 }

 

 

//Send button slot function

void MainWindow::on_pushButton_send_clicked()

{

    QString str=ui->lineEdit_sendText->text(); //Get string from LineEdit

    if(str!="")

    {

        ui->textBrowser_RecList->insertPlainText("n");

        serial->write(str.toLatin1()); //Write data to the serial port in ASCII code

        //ui->lineEdit_sendText->clear();

    }

}

 

void MainWindow::on_pushButton_openPort_clicked()

{

    if(ui->comboBox_portList->isEnabled())

     {

        ui->pushButton_openPort->setText("ClosePort"); //After pressing "OpenPort", the button displays "ClosePort"

        ui->comboBox_portList->setDisabled(true); //After pressing "OpenPort", it is forbidden to modify the COM port

 

        serial = new QSerialPort;

        //Set the serial port name

        serial->setPortName(ui->comboBox_portList->currentText());

 

        ui->textBrowser_RecList->insertPlainText("nNowPort:"+ui->comboBox_portList->currentText());

        //Method to automatically find available serial ports

 

        //Close the serial port first, then open it to ensure that the serial port is not occupied by other functions.

        //serial->close();

 

        //Open the serial port

        serial->open(QIODevice::ReadWrite);

 

        //Set the baud rate

        serial->setBaudRate(ui->comboBox_BPS->currentText().toInt());

        ui->comboBox_BPS->setDisabled(true);

 

         //Set the number of data bits (8)

         serial->setDataBits(QSerialPort::Data8);

 

        //Set the check bit (such as 0)

         serial->setParity(QSerialPort::NoParity);

 

        //Set the stop bit (such as 1)

         serial->setStopBits(QSerialPort::OneStop);

 

        //Set flow control (none)

         serial->setFlowControl(QSerialPort::NoFlowControl);

 

         //Bind the serial port's readyRead() signal to the read_Com() slot function

         connect(serial,SIGNAL(readyRead()),this,SLOT(read_Com()));

    }

    else

    {

        ui->pushButton_openPort->setText("OpenPort"); //After pressing "ClosePort", the button displays "OpenPort"

        ui->comboBox_portList->setEnabled(true); //After pressing "ClosePort", the COM port can be modified

        ui->comboBox_BPS->setEnabled(true);

        serial->close(); //Close the serial port

    }

}

 

 

void MainWindow::on_pushButton_ClearRec_clicked()

{

    ui->textBrowser_RecList->clear();

}

main.cpp


#include "mainwindow.h"

#include

 

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    MainWindow w;

    w.show();

 

    return a.exec();

}

UI interface:

Windows7 64-bit environment complete source code and source program package download: https://download.csdn.net/download/robin_xx/10791050

Reference address:Realizing the communication between PIC microcontroller and QT host computer based on RS232 serial port

Previous article:Getting Started with PIC Microcontrollers (A Must-Read for Beginners Learning PIC) - Based on PIC16F886
Next article:PIC microcontroller pointer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号