Delphi POS – 透過USB Printer打開錢櫃

procedure USBOpenCashDrawer(PrinterName: String);
var
  // use winspool
  Handle, hDeviceMode: THandle;
  N: DWORD;
  DocInfo1: TDocInfo1;
  Device, Driver, Port: array [0 .. 255] of char;
  ThePrinter: string;
  code:ansistring;
begin
  ThePrinter := PrinterName;
  Code := chr(27) + chr(64)+ chr(27) + chr(112) + chr(0) + chr(50) + chr(200);

  Printer.PrinterIndex := Printer.Printers.Indexof(ThePrinter);
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  PrinterName := Format('%s', [Device]);
  if not WinSpool.OpenPrinter(PChar(PrinterName), Handle, nil) then
  begin
   // ShowMessage('Error : ' + IntToStr(GetLastError));
    Exit;
  end;
  with DocInfo1 do
  begin
    pDocName := 'Test';
    pOutputFile := nil;
    pDataType := 'RAW';
  end;

  WinSpool.StartDocPrinter(Handle, 1, @DocInfo1);
  WinSpool.StartPagePrinter(Handle);
  WinSpool.WritePrinter(Handle, PAnsiChar(Code), Length(Code), N);
  WinSpool.EndPagePrinter(Handle);
  WinSpool.EndDocPrinter(Handle);
  WinSpool.ClosePrinter(Handle);

end;