Implementation of serial communication programming based on Matlab GUI

Publisher:QingfangLatest update time:2010-07-12 Source: 现代电子技术Keywords:Matlab Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0 Introduction

Matlab is a scientific computing software developed by MathWorks. Matlab is stable, reliable and easy to use, and is a powerful assistant for scientific researchers to conduct scientific research. Matlab not only has powerful scientific computing functions, but also has data acquisition and interface design and development functions that meet general requirements. Matlab GUI (Graphic User Interface) is a module built into Matlab for graphical interface development.

This paper uses Matlab GUI to design an interface for sending and receiving data through the serial port, and uses the serial communication API built into Matlab to implement the serial port data sending and receiving functions.

After starting Matlab, run the guide command to start the Matlab GUI development tool, as shown in Figure 1.


Create a new Blank GUI, as shown in Figure 2.

The newly created Blank GUI interface includes general interface elements, such as menus, buttons, axes, controls, etc. Add necessary serial communication parameter setting buttons.

The interface after running is shown in Figure 3.



2 Serial port data sending and receiving function implementation

2.1 Basic steps to establish serial communication process

Matlab provides a series of functions for opening and closing the serial port, setting serial port parameters, etc. These functions can be used to select the serial port number, set serial port communication parameters (baud rate, data bit, stop bit, check bit, etc.), and perform interrupt control and flow control. The complete process from establishing serial port communication to ending serial port communication includes the following steps:
(1) Create a serial port object for the application. The function that implements this function is:

The parameter port is the complete serial port name, such as cornl. PropertyName is the serial port communication parameter, such as baudrate, startbits, etc. In the process of creating a serial port object, PropertyName can also be ignored. Its function is:

(2) Connect and open the serial port. The function that implements this function is:

obj is the return value of the function that creates the serial port object. After connecting and opening the serial port, the serial port communication parameters can be modified.

(3) Set or modify serial port communication parameters. Before serial port communication can be carried out effectively, the correct serial port communication parameters must be set. The function to implement this function is:

obj is the return value of the function that creates a serial port object; PropertyName is the serial port communication parameters, such as baudrate, startbits, etc.

(4) Read and write data from the serial port. After the previous three steps are completed normally, data can be read from or written to the serial port, that is, data can be received or sent. There are multiple functions to implement the serial port reading function. The difference lies in selecting the appropriate read function according to the type of data arriving at the serial port. They mainly include: fgetl, fgets, fread, and fscanf. Here we mainly introduce fread, which is used to read binary data from the serial port. The implementation form of fread is:

A = fread(obj, size)

A is the read data, stored in the form of an array, and the stored data is in bytes; obj is the return value of the function that creates the serial port object; size specifies the number of bytes read in a read operation. There are two functions for writing to the serial port, namely fwrite and fprintf. fwrite writes data to the serial port in binary form, and the implementation form is:

fwrite(obj, A)

obj is the return value of the function that creates the serial port object; A is the written data, which is stored in the form of an array. fwrite writes data to the serial port in text form, that is, writes data to the serial port in the form of ASCII code. The implementation form is:

fprintf(obj, 'cmd')

obj is the return value of the function used to create the serial port object; cmd is the written text data, stored in the form of an array.

(5) Close the serial port and release the storage space occupied by the serial port object. The function to close the serial port is: fclose(obj). The function to release the memory space occupied by the serial port object is: delete(obj). The function to release the storage space occupied by the serial port object in the Matlab workspace is: clear obj.

The above 5 steps are the basic steps from establishing the serial communication process to closing the serial port and releasing the resources occupied by the serial port. The basic steps can realize manual sending and receiving of data. The routine is as follows:

After running the above statements, the displayed results are as follows:

After receiving the serial port data, you need to close the serial port and release the resources occupied by the serial port object. The commands used are as follows:

2.2 Serial port interrupt setting and interrupt handling function

To realize automatic sending and receiving of data, it is also necessary to define the serial port interrupt processing function and the method of triggering the serial port interrupt. Defining the serial port interrupt processing function is to define the serial port data receiving or sending function. The purpose of defining the method of triggering the serial port interrupt is to notify and start the serial port data receiving function to perform data receiving operation when the serial port detects receiving data; when the serial port output buffer is empty, notify and start the serial port data sending function.

(1) How to trigger a serial port interrupt. In Matlab serial port communication programming, Matlab triggers a serial port interrupt by detecting a serial port communication event. Events involving serial port reading and writing include: Bytes available, Output empty. There are two types of Bytes available events: one is when the number of characters received reaches the manually set number, the system generates this event; the other is when the specified character is received, the system generates this event. The Output erupty event is generated when the system detects that the output buffer is empty.

The Bytes available event needs to be set in advance. You can use the functions: set(obj, 'BytesAvailableFcnMode', 'byte'); set(obj, 'BytesAvailableFcn-Count', 240); The above two functions set the serial port interrupt to be triggered when the serial port detects that 240 characters of data have arrived in the input buffer. In addition, it can also be set to trigger a serial port interrupt when the system detects that a certain character has reached the serial port. The setting function is: set(obj, 'BytesAvailableFcnMode', 'terminator'); set(obj, 'terminator', 'H'). The above two functions set the serial port interrupt to be triggered when the serial port detects the character H.

The output buffer is empty event is generated. This event is automatically detected by the system and does not require special settings by the user. This event is generally generated after the last character in the output buffer is sent. The user can define the serial port interrupt processing function caused by this event.

(2) Serial port interrupt processing function. The serial port interrupt processing function can be defined according to user needs. For example, the serial port read interrupt processing function can be defined as follows: obj.BytesAvailableF-cn=@reeeiveData. receiveData is the serial port read interrupt processing function. The serial port read operation can be performed in the read interrupt processing function. That is, the data in the input buffer area is read into the user-defined storage variable for subsequent data processing and analysis. Similarly, a serial port interrupt processing function that is triggered when the output buffer is empty can be defined: obj.OutputEmptyFcn=@write-Data.

The serial communication program developed in this paper is used to receive and collect the measurement data of the accelerometer and gyroscope output by the IMU (Inertial Measurement Unit). By defining the serial port read interrupt event and serial port interrupt processing function, the automatic data collection is realized and the data is displayed in real time in Matlab graphics.

3 Compilation and release of serial communication program

3.1 Compiler

In order to keep the code confidential and facilitate users to publish executable programs that can run on different platforms, Matlab provides Matlab program compilation and publishing functions. Before compiling Matlab programs, you need to set up the compilation tool. In the Matlab command window, enter and run mbuild-setup to set up the compilation tool. See Figure 4.


After the mbuild-setup command is run, Matlab will detect the compilers installed on the local machine. The user can select any compiler. After the selection is completed, run the compile command and Matlab will use the compiler selected by the user to compile the program.

After setting up the compilation tool, you can run the compilation command in the command window to compile the Matlab program into an executable program. The command format is:

mcc[-options]mifilel[mfile2…mfileN];
options are compilation parameters; mfilel is the Matlab file to be compiled. If it contains multiple Matlab files, they can be listed directly after mfilel.

For example, to compile a Matlab file named myfun.m into an independent executable program, you can use the mcc command:
mcc -m myfun:
The mcc command can be used with multiple compilation parameters. The meaning of the corresponding parameters can be found in the Matlab help documentation.

3.2 Release Procedure

After the Matlab file is compiled, the generated executable program still needs the support of the Matlab environment. To publish it to a machine without Matlab installed, the last task is to package the Matlab component runtime environment (MCR). Package the MCR with the executable program and copy it to another machine without Matlab installed. Install the MCR on the machine. After the installation is complete, you can directly run the compiled Matlab executable program on the machine. The MCR installation file is located at:

Matlab Root\toolbox\compiler\deploy\win32

In the 2008 version of Matlab, the Matlab compilation and release toolbox has been built in. Running the deploytool command in the Matlab command window can call up the tool window. This tool can be used to compile and release Matlab programs conveniently and quickly.

Matlab has powerful scientific computing and graphic display functions. It is appropriate to use Matlab as a data processing and analysis tool, but its interface development and data acquisition functions are relatively weak, not as powerful as VisualC++ or LabView. However, for general interface development and ordinary serial communication development, Matlab is competent enough, and its powerful data analysis and processing functions can also be directly used. This article shows that the programming implementation of the serial communication function based on Matlab GUI shows that the interface development containing general interface elements can be carried out using Matlab GUI. On this basis, the serial communication function is added to realize the real-time data acquisition and analysis processing, and graphical display.

Keywords:Matlab Reference address:Implementation of serial communication programming based on Matlab GUI

Previous article:Design of Embedded Ethernet and Matlab Communication System Based on FPGA
Next article:Design of Interface between TMS320C6201 Processor and FLASH Memory

Recommended ReadingLatest update time:2024-11-16 16:32

Communication between Matlab and digital oscilloscope
    Abstract: The communication process between Matlab and TDS series digital oscilloscopes under Windows environment is given. The relevant communication procedures are given, and the waveform data read by the oscilloscope is processed in the frequency domain in Matlab. This shows that the communication between them
[Industrial Control]
Construction of pure electric vehicle regenerative braking model based on Simulink
The research on electric vehicle braking force distribution strategy is nothing more than improving the front and rear axle braking force distribution strategy theory under the conditions of ECE braking regulations, motor peak torque limit and battery charging and discharging characteristics. This paper studies four
[Embedded]
Construction of pure electric vehicle regenerative braking model based on Simulink
Simulation Research of AC Chopper PFC Circuit Based on Matlab
0 Introduction The widespread use of a large number of power electronic devices and nonlinear loads has caused the voltage and current waveforms of the power system to be distorted, resulting in a large number of harmonics, which has led to a decrease in the power factor of the power input, causing serious
[Power Management]
Why did hardware simulation tools surpass software simulation for the first time?
Over the past two years, something has been happening in the EDA space that needs attention but no one seems to be paying attention to: revenue from hardware verification tools (basically hardware emulation and FPGA-based prototyping) has surpassed revenue from HDL or RTL Simulation. The statistical report released
[Semiconductor design/manufacturing]
Why did hardware simulation tools surpass software simulation for the first time?
GUI porting on MINI2440 QEMU's eCos
After testing redboot, kernel, network card driver, and web server, we naturally thought of UI. This is not just about porting a GUI, but more importantly making the MINI2440 QEMU ecos emulation platform more complete. The porting of emwin requires two parts, which are old news and can be found everywhere
[Microcontroller]
GUI porting on MINI2440 QEMU's eCos
Application interface program between Matlab and C language
Application interface program between Matlab and C language , Understand several commonly used socket functions #include #include int socket(int domain,int type,int portocol); domain refers to the protocol family used, which can be AF_UNIX and AF_INET, usually only AF_INET (referring to
[Embedded]
Tesla's Optimus humanoid robot shows significant progress, and domestic manufacturers are competing to deploy it
On July 16, CITIC Securities released an in-depth research report focusing on the extraordinary progress of the Opmus humanoid since its stunning debut at SLA Day in 2021. The report shows that as of May 2024, the Gen2 version of Optimus has achieved a qualitative leap in performance, with a walking speed soaring to
[robot]
Calibrate multiple inertial measurement units (IMUs) and external parameters for each axis
summary Hello everyone, today's article is - Exnding kalibr: Calibrating the Extrinsics of Multiple IMUs and of Individual Axes More and more systems are equipped with multiple inertial measurement units (IMUs). Due to competing goals, when used for control, it is expected to be close to the center of gravity,
[robot]
Latest Embedded 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号