本人從網上弄了個hp列印機usb作業狀態代碼操作函式具體如下:
現在能正常獲取列印機狀態,但每次呼叫這個方法后,無法用驅動里列印測驗頁列印,其他的列印軟體也不能呼叫列印機驅動列印。需要重新插拔列印機的usb頭才可以再正常列印。使用的是LibUsbDotNet 類別庫作usb的通信。請問有什么辦法解決這個問題。
private void button3_Click(object sender, EventArgs e)
{
string cmdLine1 = "\x1b%-12345X@PJL INFO STATUS \r\n\x1b%-12345X";
Printer.Sendcmd(cmdLine1, "0x03F0", "0x522A");
}
#region 檢查惠普列印機狀態方法
public static UsbDevice MyUsbDevice;
#region SET YOUR USB Vendor and Product ID!
public static UsbDeviceFinder MyUsbFinder = null; // new UsbDeviceFinder(0x03f0, 0x522a);
#endregion
public static void Sendcmd(string strcmd, string xVid,string xPid)
{
ErrorCode ec = ErrorCode.None;
int vid = Convert.ToInt32(xVid, 16);
int pid = Convert.ToInt32(xPid, 16);
MyUsbFinder = new UsbDeviceFinder(vid, pid);
try
{
// 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.");
}
else
{
ShowMsg("資訊:Device (VID:" + xVid + " , PID:" + xPid + ") 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" USB device. Before it can be used,
// the desired configuration and interface must be selected.
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
// open read endpoint 1.
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
// open write 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.CommandLine, "^\".+?\"^.*? |^.*? ", "", RegexOptions.Singleline);
*/
//string cmdLine = "GET /ePrint/ePrintConfigDyn.xml HTTP/1.1\r\nHOST: localhost \r\n\r\n";
//string cmdLine = "\x1b%-12345X@PJL INFO STATUS \r\n\x1b%-12345X";
if (!String.IsNullOrEmpty(strcmd))
{
int bytesWritten;
ec = writer.Write(Encoding.GetEncoding("utf-8").GetBytes(strcmd), 2000, out bytesWritten);
if (ec != ErrorCode.None)
{
ShowMsg("往列印機發送命令錯誤:" + UsbDevice.LastErrorString);
throw new Exception(UsbDevice.LastErrorString);
}
byte[] readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;
// If the device hasn't sent data in the last 100 milliseconds,
// a timeout error (ec = IoTimedOut) will occur.
ec = reader.Read(readBuffer, 100, out bytesRead);
if (bytesRead == 0)
{
//ShowMsg("沒有回傳狀態資訊.");
throw new Exception("No more bytes!");
}
// Write that output to the console.
string strRead = Encoding.GetEncoding("utf-8").GetString(readBuffer, 0, bytesRead);
//Console.Write(strRead);
//lbxMsg.Items.Add(strRead);
ShowCmdResult(strRead);
}
//Console.WriteLine("\r\nDone!\r\n");
ShowMsg("查詢狀態完成.");
}
else
{
ShowMsg("命令為空,沒有做任何事情.");
throw new Exception("Nothing to do.");
}
}
catch (Exception ex)
{
//Console.WriteLine();
//Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
if (ec == ErrorCode.None)
{
ShowMsg("資訊:" + ex.Message);
}
else
{
ShowMsg("錯誤資訊:" + ex.Message);
}
}
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 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.Exit();
}
/*
// Wait for user input..
Console.ReadKey();
*/
}
}
public delegate void dlg_PrinterMess(string strMsg);
public static dlg_PrinterMess PrinterMess;
private static void ShowMsg(string p)
{
PrinterMess(p);
}
public static dlg_PrinterMess getPrinterStatus;
public static void ShowCmdResult(string msg)
{
getPrinterStatus(msg);
}
#endregion
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/144146.html
標籤:C#
下一篇:c#畫圖后怎樣撤銷一步
