我正在使用 Gebe 熱敏列印機 (GeBE-COMPACT Plus GPT-4672) 并遇到以下問題:
列印機(與大多數已知的熱敏列印機一樣)使用一組獨特的轉義命令來使用其功能。我正在撰寫一個 .Net 應用程式,它將列印作業發送到列印機,它作業正常,但之后,我需要發送一個轉義序列來剪切和釋放紙張。該公司發布了一個測驗應用程式,該應用程式接受一個命令并將其發送到列印機。然而,該函式本身卡在一個我無法訪問,也無法使用的 dll 檔案中,因為最終目標系統不支持發布的 dll,所以我正在嘗試創建一個解決方法。列印機通過 USB 連接。
剪紙命令如下(字面意思):<ESC>C<0d>。將此確切的行插入到測驗程式中,而 < 和 ESC 之間沒有額外的空間可以正常作業。我已使用 MSDN 頁面上提供的代碼將原始文本發送到列印機。但是,通過我的方法發送這行文本只會使其將這些行列印到紙上,但 Escape 除外。我試過將序列轉換為十六進制,但沒有成功。我試圖將文本作為二進制資料發送,也沒有奏效。感覺它只看到了 Escape 標志而忽略了字串的其余部分。有誰知道如何解決這個問題?在過去的幾個小時里,我來回轉換,使用了將近 12 英尺的紙,因為它不起作用。
編輯:
根據要求,源代碼:
切紙按鈕的功能:
private void BCut_Click(object sender, EventArgs e)
{
String output = "<ESC>C<0d>";
RawPrinterHelper.SendStringToPrinter("Gebe_Drucker", output);
}
“SendStringToPrinter”函式的代碼,它呼叫 sendbytestopprinter 函式:
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 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.
MessageBox.Show(szString);
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
就是這樣。我希望這有幫助。
uj5u.com熱心網友回復:
您不能只將 "< ESC>C<0d>" 寫為字串 - < ESC> 和 <0d> 是特殊代碼。< ESC> 用于二進制 27,<0d> 用于二進制 0。您必須以這種方式構造字串(例如,十六進制編碼): String output = "\u001BC\u0000";
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327280.html
