This part of programming is very useful for engineers who want to simulate their code that access serial port easily. It is even simple than we ever imagine. Only need 3 lines for declaration, set any properties and open it.
s = serial('COM1');
set(s,'BaudRate',4800);
fopen(s);
The first line "tells" the computer that we want to use com1 port, which must be available. As information, one computer could have more than one serial port, which means we can replace com1 with com2 etc as long as the port is available. This command will create a serial port object S in memory.
The second line means we set the Baudrate of serial port to 4800. Baudrate is one of configurable properties of serial port object. Other properties that we can configure are :
ByteOrder: [ {littleEndian} | bigEndian ]
BytesAvailableFcn
BytesAvailableFcnCount
BytesAvailableFcnMode: [ {terminator} | byte ]
ErrorFcn
InputBufferSize
Name
OutputBufferSize
OutputEmptyFcn
RecordDetail: [ {compact} | verbose ]
RecordMode: [ {overwrite} | append | index ]
RecordName
Tag
Timeout
TimerFcn
TimerPeriod
UserData
BaudRate
BreakInterruptFcn
DataBits
DataTerminalReady: [ {on} | off ]
FlowControl: [ {none} | hardware | software ]
Parity: [ {none} | odd | even | mark | space ]
PinStatusFcn
Port
ReadAsyncMode: [ {continuous} | manual ]
RequestToSend: [ {on} | off ]
StopBits
Terminator
The third line means that we want to connect to the real serial port hardware described in the first line above.
If you already have an indicator attached to your serial port (for example a led or a microcontroller) that will give an action when we send the data to serial port, than you can start to send data to it. You cannot use Hyperterminal because there will be a crash in our application.
Assumed that the device indicator already attached, than you can send data in a number or ascii format or binary :
fwrite(s,68);
or
fwrite(s,'D');
or
fwrite(s,1000100);
(to be continue ...)
Friday, November 7, 2008
Thursday, November 6, 2008
GUI (Graphical User Interface) Design on Matlab
It is very easy to design a user interface (commonly called a form) on Matlab. Although the GUI items/components (such us button, textbox, combobox, chart, etc) are not as much as Borland Delphi or Microsoft Visual Studio, it is very comfortable for a "newbie" programmer (or tobe) to use Matlab GUI for their first initial learning about graphical programming. Here, Matlab offers a minimalist concept for us to build an interactive application.
This session will give step-by-step tutorial on how to start building a simple GUI form within Matlab environment.
1. As usually, run your Matlab.
2. Then go to File -> New -> GUI.

3. GUIDE Quick Start window will appear. There are two selection tabs, Create New GUI and Open Existing GUI. In this case, use Create New GUI. Since we will create a new blank GUI, then select Blank GUI (Default). Click OK button.
4. The new window with title "untitled1.fig" will be shown. This window contain a new form, and on this form we can put any GUI components.
5. From this point, we can run our new GUI application. Click Run button. This Run button is on the toolbar, on the top part of window.
6. The save confirmation dialog titled GUIDE will appear. You can either thick "Do not show this dialog again" or leave it blank, then click Yes button.
7. After you give the name of your application and save it on the disk, the m-file editor related to this new application will be opened, and a few second later your application should be run.
8. That is it, now we already have a new GUI application which contain only one form. In Matlab environment, GUI application produces 2 files: a figure file with .fig extension and a m-file with .m extension. Just check inside your folder where you save your GUI application.
Until this point, we do not write any code into our GUI application yet. We will have it on the next session of Matlab GUI Programming.
Thank you for visiting.
1. As usually, run your Matlab.
2. Then go to File -> New -> GUI.

3. GUIDE Quick Start window will appear. There are two selection tabs, Create New GUI and Open Existing GUI. In this case, use Create New GUI. Since we will create a new blank GUI, then select Blank GUI (Default). Click OK button.
4. The new window with title "untitled1.fig" will be shown. This window contain a new form, and on this form we can put any GUI components.
5. From this point, we can run our new GUI application. Click Run button. This Run button is on the toolbar, on the top part of window.
6. The save confirmation dialog titled GUIDE will appear. You can either thick "Do not show this dialog again" or leave it blank, then click Yes button.
7. After you give the name of your application and save it on the disk, the m-file editor related to this new application will be opened, and a few second later your application should be run.Until this point, we do not write any code into our GUI application yet. We will have it on the next session of Matlab GUI Programming.
Thank you for visiting.
Subscribe to:
Posts (Atom)