MATLAB GUIDE host computer serial communication development and drawing graphics

Due to the needs of the project, I need to develop this serial port communication host computer interface, which is used to draw the data sent by the serial port in real time. I have basically never used MATLAB before, so I know a function such as zeros(100) that is entered in the command line window. As for MATLAB's ability to develop graphical interface programs, I only found out after receiving this project, emmm, I was frightened by my ignorance. Well, this is the background of my development of this host computer software and my own MATLAB technical level (slag). Developed in less than two days and passed debugging. The following records my development process and some details.

How to develop MATLAB host computer software?

Don't laugh, I'm a MATLAB novice really don't know how to develop, let alone GUIDE stuff.
Therefore, the MATLAB host computer software starts from GUIDE.

Where is GUIDE?

You can find him in two ways:
1) Command line window: guide (you will see him when you enter)
2) MATLAB interface -> New -> Graphical user interface (click to see him)

How to develop?

If you have development experience, you can detour. This is the first time I use MATLAB to develop, so I see this problem in a "long-winded"
way. For those who have no development experience, did they think of "Baidu, you will be I know", I don't recommend this, I recommend using MATLAB's help documentation. This help document can be found in the "Help" option on the right side of the toolbar in the main MATLAB interface. The PS help document is the first-hand material for learning the software! ! ! If you don't understand English, you can only use Baidu

After calling up the "Help Browser", enter "guide" in the search bar, click the first "guide-Open GUIDE" in the search content, after entering, there is "More About" below, and there is "Create a Simple UI Using GUIDE", click in, and then follow the official instructions inside to learn how to build a host computer software.

After implementation, carefully experience the callback function inside (similar to the slot function in Qt).

How did I develop it?

After creating a blank area, you need to start thinking about which controls you need. The picture shows the addition of my controls:

After the study of the official tutorial, you have the ability to add COM port information to the drop-down menu. I added it to COM21.
Here you must define your own Tag tags, as shown in the figure below, I will not show the tags of other buttons.

Now right-click on the drop-down menu and select "Callback" as shown in the figure:

I have already written the execution function inside, but be sure to add a statement at the end, otherwise the selected COM port will not be saved
guidata(hObject, handles);% Save the configuration

The button callback program for opening the serial port is like this. The same following statement is essential, otherwise the configured serial port information will be lost.

​ guidata(hObject, handles);% save the configuration

For the configuration information of the serial port, you can open the help browser, search for "serial" or "Serial Port Devices" and you will find a new world. Of course, you will learn faster with Baidu.

The callback program for closing the serial port is well compiled according to the routine.

Serial port development (knock on the blackboard and draw key points)

The main function is the serial port receiving data, because the data sent by the serial port is 8 bits (determined by the serial port), and my collected data is stored in 16 bits. Therefore, the 16-bit data must be divided when sending. For example, a 16-bit vibration data originally occupies one pit. After the 16-bit data is divided into high 8 bits and low 8 bits and saved, each 8 bits occupies one pit, and now one vibration data occupies two pits. . Sending through the serial port requires two times to send a complete vibration data. Therefore, an algorithm should be designed on the MATLAB receiving end to re-merge the two 8-bit vibration data received into the original 16-bit vibration data. This is a simple communication protocol development. When merging data on the MATLAB side, it must be noted that the transmitted data is 8-bit, but the default variable of MATLAB is double type. Therefore, the variable that accepts serial port data must be forced to be converted, and then the corresponding shift processing is performed. . Just do it. As shown below:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325038951&siteId=291194637