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. |