Most modern instruments such as oscilloscopes, spectrum analyzers, function generators, and even some power supplies support communication with a computer through a parallel port or a serial port. This communication provides an easy and accurate way to control an experiment and collect and process data. Here we will be more focused on serial communication. In serial communication Signal can have two states 0 or 1. The signal state is determined based on voltage levels on the signal line. +5v to +15v implies state 0(low) and –5v to –15v implies signal state of 1(High).
This entire project is divided in to 2 parts, the first part demonstrates the software implementation of the serial communication between MATLAB and the PC and the second part demonstrates the hardware implementation of serial communication between the MATLAB and WSN-AK development kit (ATMEGA324PA).
Sending a command or data through a serial port is very similar to writing data to a file. Before tackling the problem of serial port communication. Writing data in MATLAB is accomplished using the fprintf() statement. Typing fprintf(‘PHYS352’). When you press ‘Enter’, the test string PHYS352 should appear on your screen. To direct this output to a file instead of the computer screen we need to first open a file. This is done using the fopen() statement.
will open a file called ‘datafile.txt’ that can be used for writing data (using ‘r’ in place of ‘w’ opens the file on read mode). When the fopen() statement is executed, MATLAB returns a file identifier. In this example, the value of the file identifier is captured by the variable id. Including the file identifier in the fprintf() statement, data is written to the file instead of the screen.Typing the statement
To see if the operation is successful we can examine the contents of the file datafile.txt. However, before we can do this, you will need to close the file. This is done by using
Close the file and check that the text string was stored in the file successfully.
(For example, enter the command type datafile.txt) Data can be read from the file using the
Reopen the file, this time using ‘r’ in place of ‘w’ to indicate that you want to read from the file. Enter
The argument ‘%s’ is a conversion character that indicates that we want to read a string of characters. (Examples of other conversion characters are %d for decimal, %f for fixed point, and %e for exponential notation.) The variable data should now hold the character string ‘PHYS352’. Very similar operations are performed when you want to communicate with an instrument using the serial port.
Serial IO with MATLAB (quick launch)
The following steps are required for serial data communication
Create serial port object. Use MATLAB command serial
Configure serial port object. get and set commands
Connect to the device. Fopen
Configure if required. get and set commands
Write data with fprintf command. Read data with fscanf command.
Disconnect device on transmission over. Use fclose
MATLAB Program for Serial Port Communication :
Before we can send or receive information using a serial port, we must first identify what port will be used (COM1 or COM2). This is done by using the serial command. For example,
selects COM1 and returns an identifier which is captured by the variable ‘s’. If we enter the command without the ‘;’ and press return, the current settings of COM1 should be
The output for a particular computer is shown below:
Serial Port Object: Serial-COM1
It is obviously important that the serial port on the computer and the oscilloscope are configured in
exactly the same manner. You can change the serial port settings using ‘s. commands’. (Note: ‘s’ is the serial port identifier assigned when the serial command was executed.) For example, on a particular computer: s.BaudRate = 9600; changed the Baud rate from 1200 to 9600, and s.Terminator = ‘LF’; changed the line terminator from a ‘carriage return’ to a ‘line feed’. Ensure that the Baud rate on the COM1 port of your computer is 9600, and that the terminator is a ‘LF’. To see a complete list of the COM1 settings,
Type : get(s)
Although you do not need to be concerned with most of the properties (the default settings are probably fine, there are two properties of the serial port that you may need to change. One is the Timeout and the other is the Input Buffer Size.
The Timeout property tells the computer how long to wait during the data transfer before deciding that something is wrong and terminating the transfer.
Input buffer size is the size of the buffer in which received will be stored.
Resize the buffer size using:
s.InputBufferSize = 50000;
It must be noted that some properties can be set only before establishing connection
Terminator property: This property is associated with use of fprintf (for writing to serial port) fgets, fgetl & fscanf(for read operation). It can be any value from 1 to 127.
Ex: fgetl will read a set of bytes till terminator is found. fprintf will replace \n with this terminator character.
The serial port on the computer side of the system should now be ready to go.