The best way to get printer status is to use snmp. Its accurate and timely.
The MIB OIDs for printer status, page count, etc can be found here:
http://www.alvestrand.no/objectid/1.3.6.1.2.1.43.html[
^]
use a free MIB browser to view the values available in your printer (I only use a few that are generic enough that all printers implement):
http://www.ireasoning.com/downloadmibbrowserfree.php[
^]
Here is a sample code snippet I use.
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib;
using System.Net;
class Program {
static string printerIP = "10.28.40.80";
static PrinterStatusObj printerStatus = new PrinterStatusObj();
static void Main(string[] args) {
GetSNMPPrinterStatus();
Console.WriteLine("printer status is: " + printerStatus.PrinterStatus);
Console.WriteLine("total page count is is: " + printerStatus.TotalPageCount);
Console.ReadLine();
}
static void GetSNMPPrinterStatus() {
printerStatus = new PrinterStatusObj();
string deviceStatusOID = "1.3.6.1.2.1.25.3.2.1.5.1";
string printerStatusOID = "1.3.6.1.2.1.25.3.5.1.1.1";
string totalPageCountOID = "1.3.6.1.2.1.43.10.2.1.5.1.1";
var result = Messenger.Get(VersionCode.V1,
new IPEndPoint(IPAddress.Parse(printerIP), 161),
new OctetString("public"),
new List<Variable> {
new Variable(new ObjectIdentifier(deviceStatusOID)),
new Variable(new ObjectIdentifier(printerStatusOID)),
new Variable(new ObjectIdentifier(totalPageCountOID))
},
60000);
if (result.Count > 0) {
printerStatus = new PrinterStatusObj() {
DeviceStatus = result[0].Data.ToString(),
PrinterStatus = result[1].Data.ToString(),
TotalPageCount = result[2].Data.ToString(),
};
int statuscode = 10 * Convert.ToInt32(printerStatus.DeviceStatus) + Convert.ToInt32(printerStatus.PrinterStatus);
switch(statuscode) {
case 21:
case 23:
printerStatus.PrinterStatus = "PRINTER_READY";
break;
case 24:
printerStatus.PrinterStatus = "PRINTER_PRINTING";
break;
case 33:
if( (printerStatus.ErrorState & 2) != 2) {
printerStatus.PrinterStatus = "PRINTER_READY";
}
break;
case 34:
if( (printerStatus.ErrorState & 2) != 2) {
printerStatus.PrinterStatus = "PRINTER_PRINTING";
}
break;
case 55:
printerStatus.PrinterStatus = "PRINTER_READY";
break;
default:
printerStatus.PrinterStatus = "PRINTER_NOT_READY";
break;
}
}
}
class PrinterStatusObj {
public string JobName = "";
public string DeviceStatus = "0";
public string PrinterStatus = "0";
public string TotalPageCount = "0";
public byte ErrorState = 0;
public string AlertDesc = "";
}