Newsletters - MATLAB Digest
Emulating the Centronics Protocol in MATLAB
by Mike Librodo
Using the Flexibility of MATLAB to Talk to the Parallel Port
Overview
This article describes how to use MATLAB and the Data Acquisition Toolbox to emulate the Centronics protocol commonly used to communicate with parallel port devices. The Centronics protocol is a set of rules that dictate the exchange of signals between a host device and a client device. This protocol ensures that information is passed in a correct and timely manner.
With the Data Acquisition Toolbox providing the connectivity layer to the digital device and MATLAB serving as an environment to program the Centronics logic model, it is possible to develop an application that can take a simple string and output it to the parallel port. This article will discuss the logic model behind the Centronics protocol, the use of the Data Acquisition Toolbox, its new parallel port adaptor, and the construction of the graphical user interface (GUI) to drive the demo application called DIOPRINT. DIOPRINT can be downloaded from MATLAB Central.
Demonstration
To run this demo you will need a printer connected to one of your PC's parallel ports. Type DIOPRINT at the MATLAB command prompt to open the graphical user interface (Figure 1). You can also initiate DIOPRINT without the GUI. For more information on the demonstration type HELP DIOPRINT at the MATLAB command prompt.
![]() |
| Figure 1: dioprint graphical user interface. |
To use the application, enter text in the text box and push the send button. Your text should now be outputted to the printer. You can continue sending messages by just typing in the new message and hitting the Send button. The underlying logic behind the application is the Centronics protocol.
Centronics Protocol for Parallel Port Communication
The Centronics protocol is a set of rules that allows a host controller, such as a PC, to exchange information with a connected device. Instruments or devices connected through the parallel port typically use this protocol to communicate with each other. Centronics defines the timely exchange of signals between two connected devices to control the flow of data. For parallel port communication, Centronics is implemented using 11 digital lines. 8 lines are used to pass the data and 3 lines are used to direct when data should be passed.1
Below are some conventions that are used for digital devices.
Digital High = True = Logic 1 = Yes
Digital Low = False = Logic 0 = No
The Centronics protocol can be summarized as follows:
- The sending device makes a byte of data available on each of the 8 lines used for data.
- This device then checks the state of the BUSY status line. The device waits for BUSY to be in a low state (the device is not BUSY). If the BUSY line is in a high digital state, the receiving device is not ready to accept data.
- When BUSY is low, the device strobes (applies a signal that goes from digital high -> low -> high) the STROBE control line.
- The receiving device will set the BUSY status line to digital high while it accepts the data. When the receiving device has accepted the data it will apply a strobe on the acknowledge (ACK) status line. The receiving device will also set the BUSY status line to low.
- Once the sending device sees that the receiving device has acknowledged the data transfer, it is prepares to send the next byte of data.
![]() |
| Figure 2: Timing diagram of the Centronics protocol. The signals are transmitted via specific digital lines provided by the parallel port. 2 |
Figure 2 provides a timing diagram of the Centronics protocol. As shown in the figure, Centronics imparts a timely and systematic approach to sending information between two digital devices.
In the accompanying program, we have implemented the specifics of the protocol in a MATLAB function and used the Data Acquisition Toolbox to provide the hardware connectivity. This connectivity is accomplished through the use of the digital I/O (DIO) object and the parallel port adaptor to access the 11 digital lines required to implement this protocol. The specifics on the actual M-code will be discussed in detail below.
Parallel Port DIO Object
The application is using the parallel port DIO object to demonstrate Centronics emulation. The Data Acquisition Toolbox provides the parallel port adaptor to communicate with devices attached to the parallel port. It is necessary to take into account some characteristics of the parallel port on your computer. To determine the properties of the parallel port adaptor, you can use the Data Acquisition Toolbox DAQHWINFO function as follows:
AdaptorDllName:'D:\Matlab\toolbox\daq\daq\private\mwparallel.dll'
AdaptorDllVersion: 'Version 2.2 (R13) 28-Jun-2002'
AdaptorName: 'parallel'
BoardNames: {'PC Parallel Port Hardware'}
InstalledBoardIds: {'LPT1'}
ObjectConstructorName: {'' '' 'digitalio('parallel','LPT1')'}
The parallel port adaptor can use any of the possible three hardware ports available on the PC. This computer only has one parallel port but it is likely that your PC may have multiple ports configured. If that is the case then you need to look at the "InstalledBoardID" field returned by DAQHWINFO to identify which port you can use. The "InstalledBoardId" field returned by DAQHWINFO can identify which hardware ports are available.
hwports=a.InstalledBoardIds hwports ='LPT1'
The parallel port consists of 17 digital signal lines. These lines are subdivided into three groups called ports. These ports are:
- Port 0 (8 lines)
- Port 1 (5 lines)
- Port 2 (4 lines)
The three ports can send and receive data concurrently. Port 0 and Port 2 can be configured so that signals can be transmitted or received. Port 1 can only be read from. In the DIOPRINT application, the 8 lines of Port 0 are configured for output to the device. These 8 lines are used to transfer bytes of data. Port 1 is assigned for input so that the BUSY and ACK lines can be monitored (refer to Table 1). Knowing that the receiving device is a printer, three additional lines are added to monitor its status. NOTE: These lines are not involved in the Centronics protocol but are available because the connected device provides them. Lastly, Port 2 is configured for output. The STROBE signal is sent through this port. An additional line from Port 2 is used to initialize the printer.
Once the lines are added to the DIO object, the physical hardware lines must be connected. Parallel ports are typically found on the back of most computers in the form of a 25-pin "D-Type" connector. Figure 3 illustrates the DIO object's port association with the pins on the connector. From a physical view (Figure 3), the D-Type parallel port connector found in the back of most computers consists of 25 pins. The pins with a bar on top represent lines that are hardware inverted. Hardware inverted means that sending a digital low (0) from MATLAB becomes a digital high (1) at the connector.
![]() |
Figure 3: Physical view of the parallel port female connector. It is comprised of 25 pins, 8 belong to Port 0, 5 belong to Port 1, 4 belong to Port 2, and 8 are grounded. The pins with a bar on the top represent hardware inversion. 3 |
Table 1 lists the printer configuration. When wiring the parallel port to connect to the printer, the following usage of each pin is noted. In Table 1, a direction of "out" signifies that the signal is generated in the computer through the use of the putvalue function. A direction of "in" implies that the connected device is generating the signal and is read into the computer using the getvalue function. Signals that are active low are denoted by an "n" in the front of the signal name in Table 1. This means that a low signal is read as high. For example, pin 15 (nError) would be low if a printer error has occurred. Otherwise, during normal operations the line would normally be set to high. Again, hardware inversion means that the signal is inverted by the parallel port's hardware. For instance, if a digital low is applied to the BUSY pin, then it would read as digital high by the host PC. The inversion is specific to the parallel port hardware and is taken into consideration in the M-code.
| Port Connector | DIO Object | |||||
|---|---|---|---|---|---|---|
| Pin | Signal | Hardware Inverted | Port | Line | Direction
In/Out |
Signal Description |
|
1
|
nStrobe
|
Yes
|
2
|
0
|
In/Out
|
Active Low. Indicates valid data on the data lines |
|
2
|
Data 0
|
0
|
0
|
Out
|
Data Line | |
|
3
|
Data 1
|
0
|
1
|
Out
|
Data Line | |
|
4
|
Data 2
|
0
|
2
|
Out
|
Data Line | |
|
5
|
Data 3
|
0
|
3
|
Out
|
Data Line | |
|
6
|
Data 4
|
0
|
4
|
Out
|
Data Line | |
|
7
|
Data 5
|
0
|
5
|
Out
|
Data Line | |
|
8
|
Data 6
|
0
|
6
|
Out
|
Data Line | |
|
9
|
Data 7
|
0
|
7
|
Out
|
Data Line | |
|
10
|
nAck
|
1
|
3
|
In
|
A low asserted pulse indicating that the last byte was received | |
|
11
|
Busy
|
Yes
|
1
|
4
|
In
|
A high signal indicates that the printer is busy and cannot accept new data |
|
12
|
Paper Out
|
1
|
2
|
In
|
Paper Out | |
|
13
|
Select
|
1
|
1
|
In
|
A high signal indicates that the printer is online | |
|
14
|
nAuto Feed
|
Yes
|
2
|
1
|
In/Out
|
Active low. Instructs a printer to insert a line feed for each carriage return |
|
15
|
nError
|
1
|
0
|
In
|
A low signal indicates that a error condition exists | |
|
16
|
nInitialize
|
2
|
2
|
In/Out
|
Active low. Used to reset the printer | |
|
17
|
nSelect Printer
|
Yes
|
2
|
3
|
In/Out
|
Active low. Used to indicate to the printer that it is selected |
|
18-25
|
Ground
|
GND
|
GND | |||
Table 1: This table illustrates the relationship between the parallel port DIO object and the
parallel port connector found in the back of most computers.
Centronics Emulator Application
The DIOPRINT application can be separated into two distinct elements. The first element is the use of the DIO object and MATLAB to emulate the Centronics protocol. The second element is the graphical user interface (GUI).


