Serial Port Using GUI
Serial Port Using GUI
Serial Port Using GUI
Jason Laska
Matthew Berry
Daniel Sachs
This work is produced by The Connexions Project and licensed under the
†
Creative Commons Attribution License
Abstract
Explains how to send and receive data with the DSP through the serial port. Example code is shown
in both assembly and C (for the DSP) as well as MATLAB for interfacing on the PC. Some example
code for creating a MATLAB GUI is also shown.
1 Introduction
The serial port on the back of the DSP box can be used to transmit data between the DSP and the PC
during real-time operation. Eight bits of data can be transmitted at one time (signed or unsigned). This can
then be used as feedback from the DSP for a variety of applications. The serial port is connected to a data
buer which holds values coming from the DSP until they are read. This allows for sending larger amounts
of data (more than 1-byte) at a time from the DSP to the PC. The same is true in the opposite direction.
Our Serial Port Specications
• Port: com2
• DataRate: 38400
• Parity: None
• StopBits : 1 Stop Bit
• DataBits : 8 Data Bits
• Handshaking : none
These parameters can be used when accessing the serial port through third-party utilities such as HyperT-
erminal (included with windows).
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 2
1 .copy "v:\54x\dsplib\core.asm"
2
3 .sect ".data"
4 hold .word 0
5
6 .sect ".text"
7 main
8 stm #hold,AR3 ; Read to hold location
9
10 READSER 1 ; Read one byte from serial port
11
12 cmpm AR1,#1 ; Did we get a character?
13 bc main,NTC ; if not, branch back to start
14
15 stm #hold,AR3 ; Write from hold location
16 WRITSER 1 ; ... one byte
17
18 b main
1 http://cnx.org/content/m12062/latest/ser_echo.asm
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 3
On Line 8, we tell READSER to receive into the location hold by setting AR3 to point at it. On Line
9, we call READSER 1 to read one serial byte into hold; the byte is placed in the low-order bits of the
word and the high-order bits are zeroed. If a byte was read, AR1 will be set to 1. AR1 is checked in
Line 12; Line 13 branches back to the top if no byte was read. Otherwise, we tell reset AR3 to hold
(since READSER moved it), then call WRITSER to send the word we received on Line 16. On Line 18,
we branch back to the start to receive another character.
.copy "v:\ece320\54x\dsplib\core.asm"
.sect ".data"
value_1 .word 0
value_2 .word 0
.sect ".text"
main:
loop:
WAITDATA
endblock:
b loop
get_data:
pshm AR0 ; we save all registers that are used in
pshm AR2 ; this function - note that accumulator
pshm AR3 ; B IS OVERWRITTEN!
pshm BK
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 4
popm BK
popm AR3
popm AR2
popm AR0
ret
note: The above program does not describe an alternative means for transmitting data from the
DSP board. Some simple sleuthing in the core.asm le starting at stx head should shed some light
on the subject.
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 5
Example 3
The following example shows a simple C program that will echo received serial data back through
the serial port, much like the assembly example from the previous section.
As you can see, working with the serial port is easier in C than in assembly.
Example 4
The next example demonstrates how to receive and transmit multiple bytes using SerialRXm()
and SerialTXm.
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 6
which sets the port to all of neccesary parameters. The port is still not open for writing, however it is now
in the correct mode. To open the port, the fopen command is used, which returns a le descriptor to the
port:
'com2:' is the port on the PC, 'w' means that we are opening the port for writing, and d is the le descriptor.
For our purposes, you can think of the le descriptor as the port buer itself, and when you write to it, you
are writing directly to the buer. To write to the serial port buer, the fwrite command is used:
fwrite(fid,data,'int8');
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 7
data is the data to send to the port, d is the le descriptor of the open port, and 'int8' is the type of data
being sent. For a list of dierent data types, check MATLAB help les with help serial. Since the DSP
is blind to the dierent types and we can only use 8 bits at a time, int8 should work.
Before nishing a function, or before executing a read from the serial port, the port MUST BE CLOSED.
Failure to close the port, will result in blocking access to other functions and apps on the machine that need
to use the port. A reset pulse is sent before closing. The port is closed with the fclose command:
It seems intuitive that to read from the port, it need to be opened with a 'r' or a 'r+' instead of 'w'.
According to the MATLAB help les this is true, but in practice it does not work. See the next section
for information on how to read from the serial port. Another method of opening the port is using the fid
= serial('com2'); command. This does not seem to work for reading either. See the MATLAB help for
more details and methods.
GetSerialData.cpp to handle other serial port protocols (such as handshaking and other features) you can
use this to help you re-make the code.
Files you will need:
• GetSerialData.cpp3
• stdafx.h4
To compile the code, change to the directory (in MATLAB) with GetSerialData.cpp. Type the command:
mex GetSerialData.cpp
MATLAB may ask you to set up a compiler. Choose the MATLAB compiler (It is usually one of the rst
options and has the word MATLAB somewhere in its path). After doing this, repeat the 'mex' command
on the code. Note: This code will only work with Windows (only been tested on XP).
Compiling the C code produces a .dll le. The le at this stage is similar to a .m le in that it adds custom
functionality to MATLAB. To use the le, place it in the directory where you will use the GetSerialData
function.
2 http://cnx.org/content/m12062/latest/GetSerialData.dll
3 http://cnx.org/content/m12062/latest/GetSerialData.cpp
4 http://cnx.org/content/m12062/latest/StdAfx.h
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 8
• 'port' is the serial port to which the DSP is connected. For our use it will be 'com2'. The port name
must be entered in quotes.
• baud is the speed at which we transfer data. For the DSPs in lab we use 38400.
• size is the length of the row vector you want to acquire.
• y is the output vector.
After calling the function, it will not return until it has receive size bytes from the serial port. If it never
receives the bytes, it will eventually time out. Since the serial port only outputs single bytes at a time,
the max integer that can be acquired is 255 and the min is 0. If you wish to use signed numbers, a fourth
parameter can be entered into the function to specify. To see notes on usage and other baud rates, ports,
signed data, etc type:
GetSerialData('help');
This will bring up a help screen with the full set of options.
Example 5
This example shows what type of vector would be aquired if the DSP was constantly counting
up and outputting these numbers. We are taking in vector of size 6 at some random point in the
operation of the DSP:
y = GetSerialData('com2', 38400, 6)
y =
7 8 9 10 11 12
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 9
The numbers are counting up as written in the C DSP code. We can also specify signed numbers
and if we catch the counting at the right moment we get an output like this:
y = getSerialData('com2', 38400, 6, 1)
y =
Example 6
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 10
12 sld1 = uicontrol(Fig,'units','normal','pos',[.2,.7,.5,.05],...
13 'style','slider','value',4,'max',254,'min',0,'callback','wrt_slid');
14
15 % second slider
16 sld2 = uicontrol(Fig,'units','normal','pos',[.2,.5,.5,.05],...
17 'style','slider','value',4,'max',254,'min',0,'callback','wrt_slid');
18
19 % third slider
20 sld3 = uicontrol(Fig,'units',normal','pos',[.2,.3,.5,.05],...
21 'style','slider','value',4,'max',254,'min',0,'callback','wrt_slid');
Lines 12 through the end create the three sliders for the user interface. Several parameters are used to
specify the behavior of each slider. The rst parameter, Fig, tells the slider to create itself in the window we
created in Line 7. The rest of the parameters are property/value pairs:
• units: Normal tells Matlab to use positioning relative to the window boundaries.
• pos: Tells Matlab where to place the control.
• style: Tells Matlab what type of control to place. slider creates a slider control.
• value: Tells Matlab the default value for the control.
• max: Tells Matlab the maximum value for the control.
• min: Tells Matlab the maximum value for the control.
• callback: Tells Matlab what script to call when the control is manipulated. wrt_slid is a Matlab le
that writes the values of the controls to the serial port.
Every time a slider is moved, the wrt_slid.m le is called:
Example 7
Line 7 retrieves the value from the slider using the get function to retrieve the value property. The value is
then rounded o to create an integer, and the integer is sent as an 8-bit quantity to the DSP in Line 8. (The
http://cnx.org/content/m12062/1.1/
Connexions module: m12062 11
number that is sent at this step will appear when the serial port is read with READSER or a C equivalent
in your code.) The other two sliders are sent in the same way. Line 17 sends 0xFF (255) to the DSP, which
can be used to indicate that the three previously-transmitted values represent a complete set of data points.
This can be used to prevent the DSP and Matlab from losing synchronization if a transmitted character is
not received by the DSP.
note: Line 20 closes the serial port. Matlab buers the data being transmitted, and data is often
not sent until the serial port is closed. Make sure you close the port after sending a data block to
the DSP.
%GUI.m
%****Sample GUI, Text and a Button***
%Button
but1 = uicontrol(Fig,'foregroundcolor','blue','units','Normalized','pos',[.1,.4,.5,.1],...
'string','Press Me!','style','pushbutton','callback','SampleGUI');
A Text box is created with default text in it that says: "Defaul Text". A button is also created,
which when pressed, will execute the callback function SampleGUI.m
%SampleGUI.m
%Get Text
testText = get(ed2,'string')
Now testText holds whatever string was enetered into the text box. The function get() is used to
retrieve the data from the 'string; parameter in the ed2 handle. MATLAB help uicontrol gives
the full list of options for interface elements.
http://cnx.org/content/m12062/1.1/