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.
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- X-NUCLEO-IKS01A3 sensor test based on STM32F401RE development board 3D acceleration LIS2DW12 sensor test...
- ARM Embedded Learning Resource Sharing——"ARM Embedded Architecture and Interface Technology".pdf
- I would like to ask if the 8255A chip can only select one port at a time. After defining the address for the port, such as Q8255A XBYTE...
- Power supplies with digital control
- Please advise, brothers.
- One year's work equals three years' work? TSMC engineer shows off 2.5 million yuan bonus
- Evaluation summary: Mil based on NXP's first NPU kit MYC-JX8MPQ
- How to choose a suitable water level sensor?
- Can someone help me think of a microcontroller project?
- Development of User Graphical Interface (GUI) for Digital Oscilloscope Based on DSP