Click here to Skip to main content
11,920,156 members (57,569 online)
Click here to Skip to main content
Add your own
alternative version


69 bookmarked

Reading Data Directly from the Printer

, 13 Jun 2006
Rate this:
Please Sign up or sign in to vote.
This article will help you know how to use PJL commands to fetch information from a printer without using the Windows Spooler.Well, this was tested on printers which are connected to LANs and not to personal computers using a parallel port.

Sample Image


This code will help you to directly fetch information from a network printer using the Printer Job Language. Here, you will have to provide the IP address of your printer and connect to the printer. Once done, you can send commands and retrieve information from the printer directly without using the Windows Spooler. This article will help you in knowing the basics about how to get configuration information, change printer settings, page count, status, toner level etc. from the printer directly.


It started when a need aroused in my organization for some project where we wanted to get the settings and status of a printer directly from the printer, without the use of the Windows Spooler as it has many drawbacks. Also, the accuracy of data is not satisfactory when using the Windows Spooler. So a need aroused to look for different options. At that time, I came to know about this language called PJL (Printer Job Language) which is supported by almost all printer brands in the market. During that time, I wrote this code as an experiment to test its feasibility for implementation. My greatest inspiration behind this code was: Fun With C# and HP Laserjets, which had a funny code written which changes the display message on the printer screen.

Using the code

To understand the code and to execute it, you must have a basic knowledge about IP addresses, socket programming, and PJL (Printer Job Language). You can get the technical reference manuals for PJL from the links given below:

Follow these steps in order to execute this code properly:

  • Enter the IP address of the printer in the textbox provided.
  • Check the printer port number of your network computer and then change it. The default port number of a printer is 9100.
  • Press Connect. It will return no error messages if connected successfully, also the Close and Send Command buttons will be enabled.
  • Once connected, press on the Send Command button, and it will retrieve the printer configuration data and show it in textbox given below the Send Command button.
  • Do not forget to press the Close button once the execution is done, in order to close the connection, else it may happen sometimes that your connection will remain active and other users on the network won't be able to access the printer.

These are few of the PJL commands, and you can execute them one by one. Each command has a specific usage, and throws unique data from the printer. Also, you will have to learn the exact syntax of sending PJL commands to the printer else no data will be received. You can refer the functionality of all the commands from the links provided above.

sendString = String.Format("\x1B%-12345X@PJL INFO PAGECOUNT \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INQUIRE" + 
                           " RESOLUTION \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INQUIRE LPARM :" + 
                           " PCL PITCH \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INFO USTATUS \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INFO STATUS \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INFO MEMORY \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INFO FILESYS \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL " + 
                           "DINQUIRE LOWTONER \r\n\x1B%-12345X\r\n");
sendString = String.Format("\x1B%-12345X@PJL INFO CONFIG \r\n\x1B%-12345X\r\n");

Note: PJL can even be destructive; if not properly used, it may change the whole printer setting due to which the printer may give some weird results, and pages may not be printed properly.


I will give you a basic overview of all the methods in the order they are called.


The method named cmdConnect_Click is called on pressing the Connect button which tries to connect to the IP address provided, and on failure, it returns an exception message. If successful, then the WaitForData() method is called.

//Creating instance of Socket
m_socClient = new Socket (AddressFamily.InterNetwork, 
              SocketType.Stream ,ProtocolType.Tcp );

// retrieve the remote machines IP address
IPAddress ip = IPAddress.Parse (txtIPAddr.Text);
//A printer has open port 9100 which 
//can be used to connect to printer
int iPortNo = System.Convert.ToInt16 ( txtPort.Text);
//create the end point 
IPEndPoint ipEnd = new IPEndPoint (ip.Address,iPortNo);
//connect to the remote host
m_socClient.Connect ( ipEnd );
//wait for data to arrive 

Wait For Data

The method named WaitForData() keeps on checking for data arrival from the connected port.

if  ( pfnCallBack == null ) 
    pfnCallBack = new AsyncCallback (OnDataReceived);
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = m_socClient;
// now start to listen for any data which arrives
m_asynResult = m_socClient.BeginReceive (theSocPkt.dataBuffer ,0, 
               theSocPkt.dataBuffer.Length, SocketFlags.None, 


The method named cmdSend_Click sends the PJL command to the connected printer after encoding it in the proper format.

string sendString;
sendString = String.Format("\x1B%-12345X@PJL" + 
                           " INFO CONFIG \r\n\x1B%-12345X\r\n");
Object objData = txtDataSend.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(sendString); 
m_socClient.Send (byData);


The method OnDataReceived is called as soon as the WaitForData method detects any incoming data, and then the received data is decoded and appended byte by byte and displayed in the data-received textbox.

CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
//creating object of the class

//end receive of data
int iReceive  = 0 ;
iReceive = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iReceive +  1];
//creating object for decoding
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iReceive, chars, 0);
System.String szData = new System.String(chars);
txtDataReceive.Text = txtDataReceive.Text + szData;
//as data arrives the bytes are appended to the existing 
//string printer throws data in an ASCII format 1 byte at a time



The method cmdClose_Click closes the socket connection.

string sendString;
sendString = String.Format("\x1B%-12345X@PJL INFO " + 
                           "CONFIG \r\n\x1B%-12345X\r\n");

Object objData = txtDataSend.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(sendString);
m_socClient.Send (byData);

Points of Interest

It was great fun writing this code and I learnt many unknown characteristics of printers and Windows. I even wrote a small code which made our network printer go crazy: for every page which was being sent for printing, it printed 40 pages. I also learnt many things about the Windows Spooler which also uses a DLL called PJLMON.dll which has an inbuilt PJL command, INFO CONFIG, which gives all the details of a printer which you can see in the Control Panel Printers option.

Currently, I am trying to retrieve data from a printer connected to a local PC via parallel port. Many of my attempts were unsuccessful, but I am still trying. If anyone has any suggestions on it, please mail me at


This version only works on network printers and also lacks validation, which, if possible, I will implement in the next version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

sandeep Kumtakar
Web Developer
India India
I have done my Masters in Computer Application and currently working with an MNC as a software engineer.Working on .NET technology and Databases.

You may also be interested in...

Comments and Discussions

GeneralPrinter jobs Pin
vv223346-Aug-14 21:36
membervv223346-Aug-14 21:36 
Questiontrying to lock the Control Panel but the setting on the printer did not change Pin
Danny Teoh25-Jun-14 6:46
memberDanny Teoh25-Jun-14 6:46 
QuestionGet Authenticated pin user Pin
mld5427-May-14 2:04
membermld5427-May-14 2:04 
Newshow to get my Local printer status Pin
Member 103741941-Nov-13 2:51
memberMember 103741941-Nov-13 2:51 
Questionwhere do I insert my new command jpl? Pin
blueobsession22-Mar-12 23:50
memberblueobsession22-Mar-12 23:50 
QuestionNo data being returned. Pin
bryan halterman28-Dec-11 12:18
memberbryan halterman28-Dec-11 12:18 
AnswerRe: No data being returned. Pin
sandeep Kumtakar13-Feb-12 20:44
membersandeep Kumtakar13-Feb-12 20:44 
GeneralNo data is recieved upon clicking Send Command button Pin
sk.sangita19-Oct-10 8:47
membersk.sangita19-Oct-10 8:47 
I am able to connect to the printer Canon iR5050 PCL6 present in my network using the IP and port 9010. No data is displayed when i click on "Send Command". What could be the reason? How can i get it work?
GeneralRe: No data is recieved upon clicking Send Command button Pin
bryan halterman28-Dec-11 12:19
memberbryan halterman28-Dec-11 12:19 
GeneralRe: No data is recieved upon clicking Send Command button Pin
sandeep Kumtakar13-Feb-12 20:45
membersandeep Kumtakar13-Feb-12 20:45 
QuestionHow to send PJL Commands to a local printer connected on a USB port or a COM Port ? Pin
rabii4-May-10 4:50
memberrabii4-May-10 4:50 
GeneralQuestion Pin
427748021-Mar-10 12:10
member427748021-Mar-10 12:10 
GeneralWorks great Pin
jellifer10-Feb-10 13:41
memberjellifer10-Feb-10 13:41 
GeneralToner level Pin
Thomas Wopienka12-Nov-09 5:03
memberThomas Wopienka12-Nov-09 5:03 
GeneralNo information displayed Pin
matusa_14022-Sep-08 1:16
membermatusa_14022-Sep-08 1:16 
Generalprint spooler Pin
mamtayg7-Sep-08 19:26
membermamtayg7-Sep-08 19:26 
GeneralReading data directly from printer Pin
snehal7774-Sep-08 0:27
membersnehal7774-Sep-08 0:27 
Question" No connection could be made because the target machine actively refuse it" Pin
mamtayg2-Sep-08 2:28
membermamtayg2-Sep-08 2:28 
GeneralPrinters acting using Pin
balu1234530-Jul-08 8:50
memberbalu1234530-Jul-08 8:50 
GeneralGetting status Out-Of-Paper from USB Printer Pin
DLauwers14-Feb-08 22:48
memberDLauwers14-Feb-08 22:48 
GeneralRe: Getting status Out-Of-Paper from USB Printer Pin
sandeep Kumtakar15-Feb-08 0:27
membersandeep Kumtakar15-Feb-08 0:27 
QuestionHow to reset printer? Pin
kvijayajyothy16-Dec-07 20:19
memberkvijayajyothy16-Dec-07 20:19 
AnswerRe: How to reset printer? [modified] Pin
sandeep Kumtakar15-Feb-08 0:11
membersandeep Kumtakar15-Feb-08 0:11 
Generalsend commands to printer Pin
katz4523-May-07 11:08
memberkatz4523-May-07 11:08 
GeneralRe: send commands to printer Pin
sandeep Kumtakar15-Feb-08 0:40
membersandeep Kumtakar15-Feb-08 0:40 
GeneralSending PJL commands to Printer using USB port and Reading data from Printer Pin
RamWebReg18-Apr-07 3:51
memberRamWebReg18-Apr-07 3:51 
GeneralRe: Sending PJL commands to Printer using USB port and Reading data from Printer Pin
sandeep Kumtakar26-Apr-07 1:10
membersandeep Kumtakar26-Apr-07 1:10 
GeneralZPL Pin
jagger_mack11-Mar-07 22:19
memberjagger_mack11-Mar-07 22:19 
GeneralRe: ZPL Pin
sandeep Kumtakar26-Apr-07 1:06
membersandeep Kumtakar26-Apr-07 1:06 
QuestionDoes PJL support HP printers only? Pin
Cassie Knudsen26-Sep-06 4:57
memberCassie Knudsen26-Sep-06 4:57 
AnswerRe: Does PJL support HP printers only? Pin
sandeep Kumtakar1-Nov-06 1:39
membersandeep Kumtakar1-Nov-06 1:39 
QuestionNo connection could be made because the target machine actively refuse it Pin
coefficientX29-Aug-06 18:29
membercoefficientX29-Aug-06 18:29 
QuestionRe: No connection could be made because the target machine actively refuse it Pin
sandeep Kumtakar15-Sep-06 3:34
membersandeep Kumtakar15-Sep-06 3:34 
GeneralAbout The local Paralle Communication Pin
jayoscar19-Jun-06 16:54
memberjayoscar19-Jun-06 16:54 
AnswerRe: About The local Paralle Communication Pin
sandeep Kumtakar20-Jun-06 3:05
membersandeep Kumtakar20-Jun-06 3:05 
GeneralRe: About The local Paralle Communication Pin
Andrew Eisenberg22-Jun-06 14:45
memberAndrew Eisenberg22-Jun-06 14:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151120.1 | Last Updated 13 Jun 2006
Article Copyright 2006 by sandeep Kumtakar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid