Checking Printer Messages using OPOS Drivers in Delphi

Checking Printer Messages using OPOS Drivers in Delphi

绝情姑娘 发布于 2021-11-25 字数 271 浏览 889 回复 3 原文

I'm trying to open a Point of Sale (POS) printer using the OPOS Drivers in Delphi (BDS2006), but don't have a clue on how to check the printer status.

How would I check for messages like Check Paper and Paper Jam from the printer?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末蓝 2022-06-07 3 楼

First of all you have to install the right support software for your device, which you probably have to download from the manufacturer's website. Keep in mind that sometimes, many devices (like receipt printers) contain standard hardware (ex EPSON TX-88III) although the brand name might differ.

The support software usually contains the driver, configuration tools, and possibly programming examples of how to use the driver. Make sure that the following steps are correctly completed:

  1. Installation of driver, config tools is done

  2. The device is correctly connected using the right cables (I had problems finding the correct serial cable, since there are many different types of them)

  3. Your device is recognised by the configuration software (through the driver) and communicates well, at least it responds to some functions

  4. Use the ActiveX control that was installed with the driver. It should have similar name with the driver.

After the above steps you will have a control in your application that provides you with all available functions, status properties and events (for paper, or anything other).

可是我不能没有你 2022-06-07 2 楼

Are you using the ActiveX control from here: http://monroecs.com/oposccos.htm? It has an event for error status.

驱逐舰岛风号 2022-06-07 1 楼

I haven't used OPOS Drivers but I have done some work with POS Drivers for an Epson receipt printer connected to a cash drawer. What I discovered was that, if the printer is installed in Windows, you can then open a direct connection to it and make it do whatever you want.

The reason the printer is so slow is that it's using the graphical font functions of Windows. When you open the printer directly, you will set the mode to RAW and it will just send text out like an old-style dot-matrix. To kick the cash drawer open, you just send it the specific control codes as if you were going to print them. The printer intercepts the codes before it prints and kicks the drawer open.

BTW, I have no idea how this would work with Unicode. The printer I had only really worked with ASCII data. There might be variants designed for international markets that would work differently.

Here's the code I've used to make it work (VxMsgBox is just a cover to MessageBox):

{***************************************************************************}
{**             PrintDirect2Printer                                       **}
{***************************************************************************}
procedure PrintDirect2Printer(PrinterName, Data:pchar; dwByteCount:DWORD);
var PrinterHandle  : THandle;
    DocInfo        : TDocInfo1;
    dwJob          : DWORD;
    dwBytesWritten : DWORD;
begin
if not OpenPrinter(PrinterName, PrinterHandle, nil) then exit; //failed to open printer, abort
DocInfo.pDocName    := 'Direct 2 Printer';
DocInfo.pOutputFile := nil;
DocInfo.pDataType   := 'RAW';
dwJob:=StartDocPrinter(PrinterHandle, 1, @DocInfo);
if dwJob=0 then //failed to start a document
   begin
   ClosePrinter(PrinterHandle);
   exit;
   end;
if not StartPagePrinter(PrinterHandle) then
   begin
   EndDocPrinter(PrinterHandle);
   ClosePrinter(PrinterHandle);
   exit;
   end;
if not WritePrinter(PrinterHandle, Data, dwByteCount, dwBytesWritten) then
   begin
   EndPagePrinter(PrinterHandle);
   EndDocPrinter(PrinterHandle);
   ClosePrinter(PrinterHandle);
   exit;
   end;
if not EndPagePrinter(PrinterHandle) then
   begin
   EndDocPrinter(PrinterHandle);
   ClosePrinter(PrinterHandle);
   exit;
   end;
if not EndDocPrinter(PrinterHandle) then
   begin
   ClosePrinter(PrinterHandle);
   exit;
   end;
ClosePrinter(PrinterHandle);
if dwBytesWritten<>dwByteCount then
   VxMsgBox('Print Direct To Printer failed.', 'Printer Error', mb_Ok);
end;

{***************************************************************************}
{**             OpenPrintDirect2Printer                                   **}
{***************************************************************************}
function OpenPrintDirect2Printer(PrinterName, DocName:pchar; var PrinterHandle:THandle):boolean;
var DocInfo        : TDocInfo1;
    dwJob          : DWORD;
begin
result:=false;
if not OpenPrinter(PrinterName, PrinterHandle, nil) then exit; //failed to open printer, abort
DocInfo.pDocName    := DocName;
DocInfo.pOutputFile := nil;
DocInfo.pDataType   := 'RAW';
dwJob:=StartDocPrinter(PrinterHandle, 1, @DocInfo);
if dwJob=0 then //failed to start a document
   begin
   ClosePrinter(PrinterHandle);
   exit;
   end;
if not StartPagePrinter(PrinterHandle) then
   begin
   EndDocPrinter(PrinterHandle);
   ClosePrinter(PrinterHandle);
   exit;
   end;
result:=true;
end;

{***************************************************************************}
{**             WritePrintDirect2Printer                                  **}
{***************************************************************************}
function WritePrintDirect2Printer(PrinterHandle:THandle; Data:pchar; dwByteCount:DWORD):boolean;
var dwBytesWritten : DWORD;
begin
result:=true;
if not WritePrinter(PrinterHandle, Data, dwByteCount, dwBytesWritten) then
   result:=false;
if dwBytesWritten<>dwByteCount then
   VxMsgBox('WritePrintDirect2Printer byte check failed.', 'Printer Error', mb_Ok);
end;


{***************************************************************************}
{**             ClosePrintDirect2Printer                                  **}
{***************************************************************************}
procedure ClosePrintDirect2Printer(var PrinterHandle:THandle);
begin
if not EndPagePrinter(PrinterHandle) then
   begin
   EndDocPrinter(PrinterHandle);
   ClosePrinter(PrinterHandle);
   PrinterHandle:=0;
   exit;
   end;
if not EndDocPrinter(PrinterHandle) then
   begin
   ClosePrinter(PrinterHandle);
   PrinterHandle:=0;
   exit;
   end;
ClosePrinter(PrinterHandle);
PrinterHandle:=0;
end;