When learning QT, a simple serial host computer can no longer be written, which is used to control two LEDs on the microcontroller. Assume that only the status is on and off. One can adjust the brightness. The host computer interface is as follows:
The drop-down values of baud rate, data bit, and stop bit are added to the designer. The remaining functions are basically implemented by the Code. The protocol used for communication is also casually written. Simple and casual. The pictures are hand-painted by the younger brother.
Paste the following code (* ^__ ^ *)
# Include "mainwindow. H "# include" ui_main1_1_h "# include <qtserialport> mainwindow: mainwindow (qwidget * parent): qmainwindow (parent), UI (new UI: mainwindow) {UI-> setupui (this); PIC = tr (": // resource/ .jpg"); // place an image UI-> lightlabel-> setpixmap (PIC ); UI-> rtextedit-> setreadonly (false); UI-> stextedit-> setreadonly (false); UI-> onbutton-> setenabled (true ); // turn off the light UI by default-> offbutton-> setenabled (false); UI-> Lig Htdial-> setenabled (false); Serial = new qserialport (this); openflag = false; // connect (ui-> lightdial, signal (valuechanged (INT) is not enabled on the serial port )), this, slot (on_light_chance (); usartui ();} mainwindow ::~ Mainwindow () {Delete UI;} void mainwindow: usartui () // complete serial configuration UI {UI-> paritybox-> additem (qstringliteral ("NONE"), qserialport :: noparity); UI-> paritybox-> additem (qstringliteral ("even check"), qserialport: evenparity ); UI-> paritybox-> additem (qstringliteral ("odd check"), qserialport: oddparity); UI-> paritybox-> additem (qstringliteral ("mark 1"), qserialport:: markparity); UI-> paritybox-> additem (qstringliteral ("Space 0"), qse Rialport: spaceparity); UI-> baundratebox-> setitemdata (0, qserialport: baud4800); UI-> Custom-> setitemdata (1, qserialport: baud9600 ); UI-> baundratebox-> setitemdata (2, qserialport: baud19200); UI-> baundratebox-> setitemdata (3, qserialport: baud38400 ); UI-> baundratebox-> setitemdata (4, qserialport: baud57600); UI-> baundratebox-> setitemdata (5, qserialport: baud115200 ); UI-> databitbox-> setitemdata (0, qseri Alport: data5); UI-> databitbox-> setitemdata (1, qserialport: data6); UI-> databitbox-> setitemdata (2, qserialport: data7 ); UI-> databitbox-> setitemdata (3, qserialport: data8); UI-> stopbitbox-> setitemdata (0, qserialport: Onestop ); UI-> stopbitbox-> setitemdata (1, qserialport: oneandhalfstop); UI-> stopbitbox-> setitemdata (2, qserialport: twostop); portinfo ();} void mainwindow:: portinfo () {UI-> unamebox-> clear (); f Oreach (const qserialportinfo & info, qserialportinfo: availableports () {qstringlist; List <info. portname () <info. description () <info. manufacturer () <info. systemlocation () <(info. vendoridentifier ()? Qstring: Number (info. vendoridentifier (), 16): qstring () <(info. productidentifier ()? Qstring: Number (info. productidentifier (), 16): qstring (); UI-> unamebox-> additem (list. first (), list) ;}/// slot function void mainwindow: on_startbutton_clicked () // open the serial port {If (! Openflag) // run the {currentconfig. name = UI-> unamebox-> currenttext (); currentconfig. baudrate = static_cast <qserialport: baudrate> (ui-> baundratebox-> itemdata (ui-> baundratebox-> currentindex ()). toint (); currentconfig. databits = static_cast <qserialport: databits> (ui-> databitbox-> itemdata (ui-> databitbox-> currentindex ()). toint (); currentconfig. stopbits = static_cast <qserialport: stopbits> (ui-> stopbi Tbox-> itemdata (ui-> stopbitbox-> currentindex ()). toint (); currentconfig. parity = static_cast <qserialport: parity> (ui-> paritybox-> itemdata (ui-> paritybox-> currentindex ()). toint (); Serial-> setportname (currentconfig. name); If (Serial-> open (qiodevice: readwrite) {If (Serial-> setbaudrate (currentconfig. baudrate) & serial-> setdatabits (currentconfig. databits) & serial-> setstopbits (currentconfig. stopbit S) & serial-> setparity (currentconfig. parity) {openflag = true; UI-> startbutton-> settext (TR ("Close serial port");} else {serial-> close (); openflag = false; UI-> startbutton-> settext (TR ("open serial port") ;}} else {openflag = false; UI-> startbutton-> settext (TR ("open serial port") ;}} else {openflag = false; UI-> startbutton-> settext (TR ("open serial port"); Serial-> close ();}/* UI-> stextedit-> settext (TR ("check bit: % 1 \ n baud rate: % 2 \ n data bit: % 3 \ n stop bit: % 4 "). arg (ui-> paritybox-> itemdata (ui-> paritybox-> currentindex ()). tostring ()). arg (ui-> baundratebox-> itemdata (ui-> baundratebox-> currentindex ()). tostring ()). arg (ui-> databitbox-> itemdata (ui-> databitbox-> currentindex ()). tostring ()). arg (ui-> stopbitbox-> itemdata (ui-> stopbitbox-> currentindex ()). tostring (); */} void mainwindow: on_offbutton_clicked () // turn off the light {qbytearray data; qstring pdata; data. resize (3 ); Data [0] = 0xf0; data [1] = 0x0f; data [2] = 0x80; UI-> offbutton-> setenabled (false ); UI-> onbutton-> setenabled (true); UI-> lightdial-> setvalue (1); UI-> lightdial-> setenabled (false); pdata = data. tohex (). toupper (); int size = pdata. size (); For (INT I = size; I> 0; I --) {pdata. insert (I * 2, "") ;}ui-> stextedit-> insertplaintext (pdata); // send data serial-> write (data);} void mainwindow :: on_onbutton_clicked () // Turn on 0xf0 0x0f 0x81 {qbytearray data; qstring pdata; data. resize (3); Data [0] = 0xf0; data [1] = 0x0f; data [2] = 0x81; UI-> onbutton-> setenabled (false ); UI-> offbutton-> setenabled (true); UI-> lightdial-> setenabled (true); pdata = data. tohex (). toupper (); int size = pdata. size ()/2; for (INT I = size; I> 0; I --) {pdata. insert (I * 2, "") ;}ui-> stextedit-> insertplaintext (pdata); // send data serial-> write (dat A);} void mainwindow: on_light_chance () {qbytearray data; qstring pdata; data. resize (5); Data [0] = 0xf0; data [1] = 0x0f; data [2] = 0xc0; data [3] = 0x01; data [4] = UI-> lightdial-> value (); pdata = data. tohex (). toupper (); int size = pdata. size ()/2; for (INT I = size; I> 0; I --) {pdata. insert (I * 2, "") ;}ui-> stextedit-> insertplaintext (pdata); Serial-> write (data);} void mainwindow: on_freshbutton_c Licked () // refresh status {qbytearray RDATA = serial-> readall (); qstring pdata = RDATA. tohex (). toupper (); // convert to hex format and uppercase int size = pdata. size ()/2; for (INT I = size; I> 0; I --) // insert a space so that the format is xx {pdata. insert (I * 2, "");} ui-> rtextedit-> insertplaintext (pdata); int I = 0; If (RDATA. at (I) & 0xf0 = 0xf0) {I ++; If (RDATA. at (I) & 0x0f = 0x0f) {While (RDATA. at (I )! = '\ 0') // The parsing command {If (RDATA. at (I) & 0x80 = 0x80) currentstatus. lightonoff = false; If (RDATA. at (I) & 0x81 = 0x81) currentstatus. lightonoff = true; If (RDATA. at (I) & 0xc0 = 0xc0) {I = I + 2; currentstatus. lightvalue = RDATA. at (I) ;} I ++ ;}} if (currentstatus. lightonoff) PIC = tr (": // resource/lights on .jpg"); else PIC = tr (": // resource/lights off .jpg "); UI-> lightlabel-> setpixmap (PIC); UI-> lightlcdnumber-> display (currentstatus. lightvalue );}