Home > Net >  C # usb for HP printer status after the printer test page can't print after sending
C # usb for HP printer status after the printer test page can't print after sending

Time:10-17

On the Internet, I got a HP printer usb job status code operation function which is as follows:
Can now normal access to a printer, but after each call to this method, can't use the test page prints out driving in print, other printing software also can't call a printer driver to print, need to plug the printer usb head can be normal print, using a LibUsbDotNet class library for usb communication, excuse me what method to solve this problem,


Private void button3_Click (object sender, EventArgs e)
{
String cmdLine1="\ x1b - 12345 - x % @ PJL INFO STATUS \ r \ n \ % x1b - 12345 - x";
Printer. Sendcmd (cmdLine1, "0 x03f0", "0 x522a");
}

# region HP printer state method

Public static UsbDevice MyUsbDevice;

# region SET YOUR USB Vendor and Product ID!

Public static UsbDeviceFinder MyUsbFinder=null;//new UsbDeviceFinder (x522a x03f0 0, 0);

# endregion
Public static void Sendcmd (string STRCMD, string xVid, string xPid)
{
ErrorCode=ec ErrorCode. None;

Int vid.=the Convert ToInt32 (xVid, 16);
Int pid=the Convert. ToInt32 (xPid, 16);

MyUsbFinder=new UsbDeviceFinder (vid and pid);
Try
{
//the Find and open the usb device.
MyUsbDevice=UsbDevice. OpenUsbDevice (MyUsbFinder);

//If the device is open and ready
If (MyUsbDevice==null)
{
//ShowMsg (" Device Not Found. ");
Throw new Exception (" Device (VID: "+ xVid + PID:" + xPid + ") Not Found. ");
}
The else
{
ShowMsg (" information: Device (VID: "+ xVid +", PID: "+ xPid +") the Opened. ");
}

//If this is a "whole" usb device (libusb - win32 Linux libusb)
//it will have an IUsbDevice interface. If not (WinUSB) the
//variable will be null indicating this is an interface of a
//device.
IUsbDevice wholeUsbDevice=MyUsbDevice as IUsbDevice;
if (! ReferenceEquals (wholeUsbDevice, null))
{
//This is a "whole" is a USB device. Before it can be 2,
//the desired configuration and interface must be selected.

//Select the config # 1
WholeUsbDevice. SetConfiguration (1);

//the Claim interface # 0.
WholeUsbDevice. ClaimInterface (0);
}

//open the read the endpoint 1.

UsbEndpointReader reader=MyUsbDevice. OpenEndpointReader (ReadEndpointID. Ep01);

//open the write the endpoint 1.
UsbEndpointWriter writer.=MyUsbDevice OpenEndpointWriter (WriteEndpointID Ep01);

//Remove the exepath/startup filename text from the begining of the CommandLine.
/*
String cmdLine=Regex. Replace (
Environment.Com mandLine, "^ ". +? \ "^. *? | ^. *? ", "", RegexOptions. Singleline);
*/
//string cmdLine="GET/ePrint/ePrintConfigDyn XML HTTP/1.1 \ r \ nHOST: localhost \ r \ n \ r \ n";
//string cmdLine="\ x1b - 12345 - x % @ PJL INFO STATUS \ r \ n \ % x1b - 12345 - x";
if (! String. IsNullOrEmpty (STRCMD))
{
Int bytesWritten;
Ec=writer. Write (Encoding. GetEncoding (" utf-8 "). GetBytes (STRCMD), 2000, the out bytesWritten);
If (ec!=ErrorCode. None)
{
ShowMsg (" send to the printer command error: "+ UsbDevice. LastErrorString);

Throw new Exception (UsbDevice. LastErrorString);
}

Byte [] readBuffer=new byte [1024].
While (ec==ErrorCode. None)
{
Int bytesRead;

//If the device hasn 't sent the data in the last 100 milliseconds,
//a timeout error (ec=IoTimedOut) will occur.
Ec=reader. Read (readBuffer, 100, out bytesRead);

If (bytesRead==0)
{
//ShowMsg (" not return status information. ");
Throw new Exception (" No more bytes!" );
}

//Write that the output to the console.
String strRead=Encoding. GetEncoding (" utf-8 "). Get string (readBuffer, 0, bytesRead);
//the Console. Write (strRead);
//lbxMsg. Items. The Add (strRead);
ShowCmdResult (strRead);
}

//the Console. WriteLine (" \ r \ nDone! \r\n");
ShowMsg (" query status. ");
}
The else
{
ShowMsg (" command is empty, didn't do anything. ");
Throw new Exception (" Nothing to do. ");
}

}
The catch (Exception ex)
{
//the Console. WriteLine ();
//the Console. WriteLine ((ec!=ErrorCode. None? Ec + ":" : String. The Empty) + ex. Message);
If (ec==ErrorCode. None)
{
ShowMsg (" : "+ ex. Message);
}
The else
{
ShowMsg (" error Message: "+ ex. Message);
}
}
The finally
{
If (MyUsbDevice!=null)
{
If (MyUsbDevice. IsOpen)
{
//If this is a "whole" usb device (libusb - win32 Linux libusb 1.0)
//it exposes an IUsbDevice interface. If not (WinUSB) the
//'wholeUsbDevice variable will be null indicating this is
//an interface of a device; It does not require the or support
//configuration and interface selection.
IUsbDevice wholeUsbDevice=MyUsbDevice as IUsbDevice;
if (! ReferenceEquals (wholeUsbDevice, null))
{
//Release interface # 0.
WholeUsbDevice. ReleaseInterface (0);
}

MyUsbDevice. Close ();
}
MyUsbDevice=null;

//Free usb resources
UsbDevice. The Exit ();

}

/*
//Wait for user input..
Console.ReadKey();
*/
}
}

Public delegate void dlg_PrinterMess (string strMsg);
Public static dlg_PrinterMess PrinterMess;
nullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related