Как узнать состояние принтера GC420t?
Я использую LibUsbDotNet для связи с моим принтером GC420t Zebra.
Это прекрасно работает, когда дело доходит до печати:
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
if (MyUsbDevice == null) throw new Exception("Device Not Found.");
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
int bytesWritten;
if (writer.Write(Encoding.Default.GetBytes(someString), 2000, out bytesWritten) != ErrorCode.None)
throw new Exception(UsbDevice.LastErrorString);
Но я не могу найти способ заставить мой читательский код работать... Всегда возвращает 0 байтов чтения. Я поместил его прямо в конец приведенного выше кода, открыв крышку моего принтера (что, несомненно, должно дать мне код ошибки).
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
// above code...
ErrorCode ec = ErrorCode.None;
byte[] readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;
ec = reader.Read(readBuffer, 5000, out bytesRead);
Console.WriteLine("{0} bytes read", bytesRead);
Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
}
Если вы знаете, как заставить это работать... Или, если вы знаете лучший / быстрый / простой способ сделать это, я возьму это, спасибо.
РЕДАКТИРОВАТЬ: Итак, я попробовал еще несколько вещей, сделал еще несколько исследований.
Доступ к состоянию принтера с помощью winspool-> вернул 0, хотя я аккуратно вынул носитель из принтера. Ну, я думаю, он просто инициализирован в 0 и не получает значения. Этот код использует OpenPrinter/GetPrinter/ClosePrinter
шаблон.
LibUsbDotNet-> Пробовал все перечисленные способы чтения статуса, всегда читается 0 байтов.
RawPrinterHelper-> Работает для печати, но не нашел способа узнать состояние моего принтера.
Затем я прочитал что-то (не могу вспомнить, на каком сайте), сказав, что вы должны были прочитать статус, пока принтер печатал. Как это можно сделать?
РЕДАКТИРОВАТЬ: Ради полноты, вот как я генерирую свои команды для принтера (что, вероятно, не поможет, так как, опять же, это работает безупречно, когда дело доходит до печати):
StringBuilder sb = new StringBuilder().AppendLine()
.AppendLine("N")
.AppendLine("^ee") // The "give me an answer" code, also tested at the end of the commands, or as the only command (with newline and N)
// more appending...
.AppendLine(String.Format("P{0},{1}", 1, 1));
РЕДАКТИРОВАТЬ: Просто для записи, причина, почему я совершенно уверен, что вы можете получить статус этого GC420t... потому что вы просто можете, с помощью Zebra Setup Utilities. Если вы отправите ^ee с помощью прилагаемого инструмента (Open Communication With Printer), вы получите код ошибки правильно. Мне просто нужно знать, как это происходит.
2 ответа
Попробуйте использовать приведенный выше класс:
public class cPrintHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)] public string pDocName;
[MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)] public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )
{
// Start a document.
if( StartDocPrinter(hPrinter, 1, di) )
{
// Start a page.
if( StartPagePrinter(hPrinter) )
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if( bSuccess == false )
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter( string szPrinterName, string szFileName )
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte []bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes( nLength );
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter( string szPrinterName, string szString )
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
И звонок:
cPrintHelper.SendStringToPrinter(printerName, fileIn);
fileIn - это код escape-последовательности.
fileIn должен быть примерно таким. N I8, D Q120,24 q245 S4 D12 ZB A100,10,0,3,2,2, N, "A" A140,10,0,3,2,2, N, "B" P1
1
Этот код успешно работает 5 лет с принтером этикеток Zebra 2844.
Надеюсь, это поможет.:)
Написал приложение для киоска с использованием KR403 в прошлом году. Я смог успешно распечатать и опрашивать состояние принтера, чтобы узнать, не было ли бумаги с низким содержанием бумаги и т. Д. Через usb, используя сообщение в блоге ниже.
http://danielezanoli.blogspot.com/2010/06/usb-communications-with-zebra-printers.html
var printerStatusCommand = Encoding.GetEncoding(850).GetBytes(@"~HQES");
try
{
var zebraConnection = new ZebraUsbStream();
zebraConnection.Write(printerStatusCommand, 0, printerStatusCommand.Length);
var statusReturn = new byte[800];
var bytesRead = zebraConnection.Read(statusReturn, 0, 800);
if (bytesRead >= 132)
{
var stringResult = Encoding.Default.GetString(statusReturn.ToArray());
Console.WriteLine(stringResult);
}
}
catch
{
Console.WriteLine("Error");
}
Было много дополнительного анализа строки / байта stringResult, так как значения состояния хранились как отдельные биты внутри возвращаемых байтов, но документы на принтер довольно хорошо охватывали форматирование.