Matlab serial operation and GUI programming

Source: Internet
Author: User

Simple MATLAB GUI programming and serial control. Word editing, if you need a PDF version, please leave a message. To be honest, this is pretty ugly ...

Overview

This paper introduces the programming idea and function of program Ad9512_serial_gui. The program design to the MATLAB image user interface Programming basic method and the basic operation of the serial port. The purpose of the program is to configure the AD9512 through the serial port write control word (AD9512 through the SPI write register, this program is only a part of the entire control program).

Revision history

The following table shows the revision process for this document

Date

Version number

Revised content

2015/01/15

V0.0

Initial release, trial version [1]

[1]. The trial version shows that the application notes and the corresponding code in this version have not been verified, and the simulation analysis may not be carried out. The version number corresponding to the trial version is v0.x.

Brief introduction

This program is based on Matlab 2014a, the "Matlab" mentioned in this document refers to the specific version of MATLAB.

The function that this program wants to realize is: Host computer control AD9512, of course AD9512 is connected with FPGA, and FPGA is connected by serial port and host computer. The control method of the AD9512 is described in its datasheet, via the SPI configuration. In principle, both the SPI and the UART are full-duplex, but the read process also needs to be written to the control head, which is used in half-duplex mode.

The idea of this program is to think of the FPGA as a UART-to-SPI device that directly configures the AD9512 via the host computer. FPGA end of the processing is not considered, the MATLAB program according to the need to write the control word generated transmission program. The following are the relevant background knowledge of this program.

serial communication and RS232

slightly. (can refer to MATLAB help-Data File management–serial port devices or related literature)

MATLAB serial Operations The

Serial port operation and is actually very simple, most of the help from Matlab can be found. Set up, open, close, delete, write data, read data. This is the basic operation. Note that the serial port is opened and remember to close it. The basic operation has

s=serial (' COM1 ');

fopen (s);

Fwrite (s,100);% write one byte

Fread (s,[1 1], ' unit8 ');% read one byte

Fclose (s);

Delete (s);

GUI for MATLAB Programming

This program is still relatively elementary, for those who have not been in touch with Matlab GUI programming, refer to MATLAB help –matlab-gui Building-gui Building basics-examples and how To-crea Te a simple guide GUI, after one of the examples to complete its own, and understand its rationale, it is very easy to understand the program.

Programming

The programming process is carried out according to the following process:

1. Identify requirements

2. Design a reasonable interface

3. Write callback function to achieve basic interface control

4. write function function Implementation function

Demand

The function of the program is simple, in particular, only 3 functions can be implemented.

    • Serial port selection and open, close operation;
    • control word input function;
    • Send or receive relevant information.

Interface design

Comprehensive requirements to consider requirements, the interface design is as follows

At the bottom of the interface, you are ready to add a status Output window in a later version to keep abreast of the program's running state. This program uses GUI guide to write the image user interface, using the following components

Statictext: Static text

Button: Buttons

Buttongroup: Button groups (link these buttons together)

Table: Tables

EditText: Editable Text

Panel: panels, combining some controls

A relatively good habit in programming is to name (make sense) the "Tag" property of each control, rather than using the default name.

For MATLAB GUI programming, the personal view is (temporarily) for complex applications do not use MATLAB, for simple applications can be used, but really very bad use. The advantage of MATLAB is its powerful mathematical computing ability, and should not be used if the application does not require this (for example, the application).

Interface control

What I want to say here is that when a control operation is triggered, it is possible to change the corresponding state of other controls in time to prevent illegal operation. For example, the program only for a serial port operation, after the correct opening of the serial port, close the string can not be executed again "open" operation or change the port port number.

Complete this function and its simplicity, just use the SET command to change the state of the control, such as making the Open button unusable (grayed)

Set (Handles.portopen, ' Enable ', ' off ');

These statements are probably written in the callback function. The callback function is probably the most important in MATLAB GUI programming, if the memory is not mistaken, it is similar to the monitoring in Java. That is, the response to some operations. Different controls depart callback the process is not the same, the specific can refer to help or "MATLAB GUI design study notes." For almost all elements of the interface, it can be accessed through handles.

This version of the program to some of the operation has a corresponding treatment, but very imperfect. This also shows that seemingly simple things need to be carefully thought about, often do not be so easy.

function implementation

There are three functions in this program, including

    • SERIALOPEN.M Serial Port Open operation
    • SERIALWRITE.M Write operations
    • SERIALREAD.M Read operation

The advantage of writing a function without retaining it in the main program appears to have two points at a time: one is that the main program is too long and most of the content is automatically generated by MATLAB, and the other is that the corresponding application needs to modify the parameters or output format, just modify these functions.

The serial port opening operation is simple, can be opened by obtaining the port number in the editable text box. Read and write operations are similar, the following write operations as an example of a brief introduction.

Before writing the data, you need to get the data you want to write, that is, the user enters the data in the table. The table is called uitable in Matlab, and the type of data stored in it is the cell array (cell arrays). (Cell is a data type of MATLAB) It is important to note that when you set up a uitable data type, the data type of the uitable may change because of the type of cell you set, even if you do not change its type using the Set method. (When establishing uitable, there are also related warnings)

By using the Get method, all data in the table can be obtained, and the contents of the corresponding location can be accessed via data{m,n} (the cell is accessed by data (M,n)). The input format of this program is a string, corresponding to the two-bit 16 characters (the program does not have any error-checking ability, any illegal output will cause the program to stop running).

The operation in Serialwrite is simply to convert a 16-based string into a number, which is then sent out via the fwrite function. Corresponding to the AD9512, each write 1byte, the head should be 0+addr, followed by the register. Convert the string in the cell array and send it, remembering that it is necessary to write the 5A register to complete the update process. In the case of reading, the head writes 8+ADDR can wait to receive the data.

Reference

Matlab Help

A study of MATLAB GUI design

MATLAB Programming (second Edition)-Beginner's introductory textbook

Code

Code is described in Ad9512_serial_gui, the program is simple and not complete, not add comments.

Matlab serial operation and GUI programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.