Skip to Main Content Skip to Search
Accelerating the pace of engineering and science

 

Newsletters - MATLAB Digest

Emulating the Centronics Protocol in MATLAB (continued)


Connecting using to the device using Data Acquisition Toolbox

The following set of commands describes how to initialize the digital I/O object with the parallel port adaptor and read/write to the printer port.

%Create the DIO object using the parallel adaptor.

dio=digitalio('parallel','lpt1');

%Create the data, status and control interface using ADDLINE.
%By using the addline function, the DIO object allows specific
%lines or line groups to be differentiated. This simplifies
%reading/writing information on individual lines

Port0=addline(dio,0:7,0,'out'); %Pin 2-9
Port1.Error=addline(dio,0,1,'in'); %Pin 15
Port1.Select=addline(dio,1,1,'in'); %Pin 13
Port1.PaperOut=addline(dio,2,1,'in'); %Pin 12
Port1.Ack=addline(dio,3,1,'in'); %Pin 10
Port1.Busy=addline(dio,4,1,'in'); %Pin 11
Port2.Strobe=addline(dio,0,2,'out'); %Pin 1
Port2.Autofeed=addline(dio,1,2,'out'); %Pin 14
Port2.InitializePrinter=addline(dio,2,2,'out');%Pin 16
Port2.SelectPrinter=addline(dio,3,2,'out'); %Pin 17

To display the summary of the parallel port DIO object and its lines type:

dio

The summary should look similar to the following:

Display Summary of DigitalIO (DIO) Object Using 'PC Parallel Port Hardware'.
Port Parameters: Port 0 is port configurable for reading and writing.
Port 1 is port configurable for reading.
Port 2 is port configurable for reading and writing.
Engine status: Engine not required.
DIO object contains line(s):
Index LineName HWLine Port Direction
1 'Pin2' 0 0 'Out'
2 'Pin3' 1 0 'Out'
3 'Pin4' 2 0 'Out'
4 'Pin5' 3 0 'Out'
5 'Pin6' 4 0 'Out'
6 'Pin7' 5 0 'Out'
7 'Pin8' 6 0 'Out'
8 'Pin9' 7 0 'Out'
9 'Pin15' 0 1 'In'
10 'Pin13' 1 1 'In'
11 'Pin12' 2 1 'In'
12 'Pin10' 3 1 'In'
13 'Pin11' 4 1 'In'
14 'Pin1' 0 2 'Out'
15 'Pin14' 1 2 'Out'
16 'Pin16' 2 2 'Out'
17 'Pin17' 3 2 'Out'

Now that the digital I/O object and its lines are created, it is possible to communicate with the device. Key elements on how to send a byte of data to the device using MATLAB and the Data Acquisition Toolbox will be covered here. To view the code in its entirety type:

edit dioprint.m

According to the Centronics protocol, the first step is to send data to the data lines. To write data to the data lines, use the putvalue function.

%This puts the binary value of 'a' in Port 0
putvalue(Port0, double('a'));

After sending the data, the next step is to check the BUSY line and then strobe the device. If the printer is busy, it cannot accept any incoming data. To check the BUSY line, issue the getvalue function on the BUSY Line. If the value returned is low (0) then the printer is busy. Remember that the BUSY line is hardware inverted. After checking the BUSY line, the next step is to strobe the device and eventually read the ACK line. The latter three steps are implemented within the checkbusy_strobe_ackn subfunction in DIOPRINT.M. This subfunction works similar to the code below.

%Read the BUSY line, waiting to make sure that the
%device is not busy while getvalue(Port1.Busy) == 0
      disp("Printer is busy")
end

putvalue(Port2.Strobe,1) %Strobe Line set high
pause(1e-3)
putvalue(Port2.Strobe,0) %Strobe Line set low
getvalue(Port1.Ack) %Read from the Acknowledge Line

Before the printer can print a line of text, you need to explicitly send a carriage return and linefeed command to the parallel port. These commands are implemented in the carriagereturn_linefeed subfunction following the Centronics protocol. The reason for these extra steps for printing is that printers usually follow the DOS text file convention. This convention requires that the print head receive an explicit carriage return character followed by a linefeed character at the end of a line of text. After receiving these commands, the printer outputs its buffer and prints.

The latter code is all that is required to send a byte of data to the parallel port using a DIO object. By putting this code into a function, it is possible to send entire messages to the parallel port with little effort.

Creating the Graphical User Interface

The application GUI was created using GUIDE, a GUI development environment within MATLAB that you can use to design your interface. It offers a variety of tools that enable you to easily layout your GUI and program the associated callbacks. A callback is an expression that is evaluated in response to an event such as a mouse click. The DIOPRINT application has several callbacks associated with the user interface controls (i.e., pushbutton and editbox). GUIDE provides an application M-file that serves as the framework for programs that control the GUI. All code, including the callbacks, is self-contained within this single file.

This application contains a few user interface controls, such as the push button, text box, and frames. After creating the layout in GUIDE, an M-file is automatically generated. The M-file served as the template for all the callbacks for the GUI components. The GUI also included a menu bar. The menu items had associated callbacks that were generated in the file. All that was required was to populate the callbacks with the required methods. To examine the callbacks for the GUI, open DIOPRINT.M. For more information on GUIDE, the application M-file, and how callbacks are evaluated visit:

Summary

MATLAB and the Data Acquisition Toolbox can be used to create an environment that emulates the Centronics communication protocol using the new parallel port adaptor in the Data Acquisition Toolbox. This article demonstrates how easy it is to prototype this protocol once the interface is understood. If you needed to design a new protocol, the flexibility of MATLAB and the Data Acquisition Toolbox allow for rapid prototyping in a single environment.

Reference
1. Warp Nine Engineering, The IEEE 1284 Experts Parallel Port Background
www.fapo.com/porthist.htm

2. Interfacing to the IBM-PC Parallel Printer Port
www.doc.ic.ac.uk/~ih/doc/par/

3. Interfacing the Standard Parallel Port
www.beyondlogic.org/spp/parallel.htm

Contact sales
Subscribe to newsletters