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
Previous article:Getting Started with PIC Microcontrollers (A Must-Read for Beginners Learning PIC) - Based on PIC16F886
Next article:PIC microcontroller pointer
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- How to configure the timer?
- How to deal with the prompt "Transient time point calculation did not converge" during Multisim simulation
- What are the configurations of the computer that ranks fourth in the world?
- Hahaha
- The development board is connected to the Internet via a shared network
- Thonny Python Editor upgraded to 4.0.0 Beta1
- control
- 【CH579M-R1】+W25Q16 storage module reading and writing
- Is this the worst impedance processing you have ever encountered?
- PCB panelization