串口数据传输:
Step1:串口数据的发送和接收过程,我们需要单独的添加串口的相关模块进去,模块名称叫做serialport,我们需要建立的工程是QMainWindow的基类程序,不是QWidget程序
我们添加的位置是在xxx.pro文件当中,改文件如前所述,是包含当前工程所需要的核心模块的文件:
QT += core gui serialport
这里添加了core、gui、serialprot三个核心需要使用到的模块。
Step2:完成整个串口通讯程序的UI界面的设计工作:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>473</width> <height>382</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QLabel" name="label_6"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>321</width> <height>51</height> </rect> </property> <property name="font"> <font> <pointsize>28</pointsize> <italic>true</italic> <underline>false</underline> <strikeout>false</strikeout> </font> </property> <property name="cursor"> <cursorShape>BlankCursor</cursorShape> </property> <property name="text"> <string>Qt interface Demo!</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> <widget class="QLabel" name="label_5"> <property name="geometry"> <rect> <x>0</x> <y>50</y> <width>171</width> <height>51</height> </rect> </property> <property name="font"> <font> <pointsize>16</pointsize> <italic>true</italic> <underline>false</underline> <strikeout>false</strikeout> </font> </property> <property name="cursor"> <cursorShape>BlankCursor</cursorShape> </property> <property name="text"> <string>SerialPort Control</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> <widget class="QLabel" name="label_4"> <property name="geometry"> <rect> <x>10</x> <y>100</y> <width>171</width> <height>21</height> </rect> </property> <property name="font"> <font> <pointsize>10</pointsize> <italic>true</italic> <underline>false</underline> <strikeout>false</strikeout> </font> </property> <property name="cursor"> <cursorShape>BlankCursor</cursorShape> </property> <property name="text"> <string>Designed by : mm1994uestc</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>10</x> <y>210</y> <width>91</width> <height>17</height> </rect> </property> <property name="text"> <string>Send Content</string> </property> </widget> <widget class="QLabel" name="label_2"> <property name="geometry"> <rect> <x>0</x> <y>250</y> <width>111</width> <height>17</height> </rect> </property> <property name="text"> <string>Recieve Content</string> </property> </widget> <widget class="QTextEdit" name="Send_Text"> <property name="geometry"> <rect> <x>120</x> <y>200</y> <width>231</width> <height>31</height> </rect> </property> </widget> <widget class="QTextEdit" name="Recieve_Text"> <property name="geometry"> <rect> <x>120</x> <y>240</y> <width>231</width> <height>81</height> </rect> </property> </widget> <widget class="QPushButton" name="Send"> <property name="geometry"> <rect> <x>360</x> <y>200</y> <width>98</width> <height>27</height> </rect> </property> <property name="text"> <string>SEND</string> </property> </widget> <widget class="QPushButton" name="Clear"> <property name="geometry"> <rect> <x>360</x> <y>260</y> <width>98</width> <height>27</height> </rect> </property> <property name="text"> <string>CLEAR</string> </property> </widget> <widget class="QComboBox" name="BaudRate"> <property name="geometry"> <rect> <x>280</x> <y>80</y> <width>78</width> <height>27</height> </rect> </property> <item> <property name="text"> <string>115200</string> </property> </item> <item> <property name="text"> <string>57600</string> </prop 19:41:19 收到数据:erty> </item> <item> <property name="text"> <string>38400</string> </property> </item> <item> <property name="text"> <string>19200</string> </property> </item> <item> <property name="text"> <string>9600</string> </property> </item> <item> <property name="text"> <string>4800</string> </property> </item> <item> <property name="text"> <string>2400</string> </property> </item> <item> <property name="text"> <string>1200</string> </property> </item> </widget> <widget class="QLabel" name="label_3"> <property name="geometry"> <rect> <x>290</x> <y>60</y> <width>61</width> <height>17</height> </rect> </property> <property name="text"> <string>Baud Set</string> </property> </widget> <widget class="QLabel" name="label_7"> <property name="geometry"> <rect> <x>200</x> <y>60</y> <width>65</width> <height>17</height> </rect> </property> <property name="text"> <string>Port Set</string> </property> </widget> <widget class="QComboBox" name="Port_Set"> <property name="geometry"> <rect> <x>190</x> <y>80</y> <width>78</width> <height>27</height> </rect> </property> </widget> <widget class="QComboBox" name="DataBit"> <property name="geometry"> <rect> <x>380</x> <y>80</y> <width>78</width> <height>27</height> </rect> </property> <item> <property name="text"> <string>8</string> </property> </item> <item> <property name="text"> <string>7</string> </property> </item> <item> <property name="text"> <string>6</string> </property> </item> <item> <property name="text"> <string>5</string> </property> </item> </widget> <widget class="QLabel" name="label_8"> <property name="geometry"> <rect> <x>390</x> <y>60</y> <width>61</width> <height>17</height> </rect> </property> <property name="text"> <string>Data Bit</string> </property> </widget> <widget class="QComboBox" name="CheckBit"> <property name="geometry"> <rect> <x>190</x> <y>150</y> <width>78</width> <height>27</height> </rect> </property> <item> <property name="text"> <string>None</string> </property> </item> <item> <property name="text"> <string>Even</string> </property> </item> <item> <property name="text"> <string>Odd</string> </property> </item> <item> <property name="text"> <string>Mark</string> </property> </item> <item> <property name="text"> <string>Space</string> </property> </item> </widget> <widget class="QLabel" name="label_9"> <property name="geometry"> <rect> <x>190</x> <y>130</y> <width>81</width> <height>20</height> </rect> </property> <property name="text"> <string>Data Parity</string> </property> </widget> <widget class="QComboBox" name="StopBit"> <property name="geometry"> <rect> <x>280</x> <y>150</y> <width>78</width> <height>27</height> </rect> </property> <item> <property name="text"> <string>1</string> </property> </item> <item> <property name="text"> <string>1.5</string> </property> </item> <item> <property name="text"> <string>2</string> </property> </item> </widget> <widget class="QLabel" name="label_10"> <property name="geometry"> <rect> <x>290</x> <y>130</y> <width>61</width> <height>17</height> </rect> </property> <property name="text"> <string>Stop Bit</string> </property> </widget> <widget class="QComboBox" name="Stream"> <property name="geometry"> <rect> <x>380</x> <y>150</y> <width>78</width> <height>27</height> </rect> </property> <item> <property name="text"> <string>NoFlowCtl</string> </property> </item> <item> <property name="text"> <string>SoftWareCtl</string> </property> </item> <item> <property name="text"> <string>HardWareCtl</string> </property> </item> </widget> <widget class="QLabel" name="label_11"> <property name="geometry"> <rect> <x>370</x> <y>130</y> <width>121</width> <height>17</height> </rect> </property> <property name="text"> <string>Stream Control</string> </property> </widget> <widget class="QPushButton" name="Start_Stop_Button"> <property name="geometry"> <rect> <x>20</x> <y>130</y> <width>71</width> <height>71</height> </rect> </property> <property name="text"> <string>Start/Stop</string> </property> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>473</width> <height>25</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>