Correct way to send commands directly to printer!

前端 未结 3 1620
夕颜
夕颜 2021-01-03 12:04

Ok, here is how i do it:

procedure TMainWindow.btnRawPrintClick(Sender: TObject);
begin
  BeginPrint;
  SendStr(#27#69);
  SendStr(\'MyData\');
  SendStr(#10         


        
相关标签:
3条回答
  • 2021-01-03 12:43
    Procedure StrLstYazdir(pYazilacakListe: TStringList; pYazici: String);
    var
      hPrn: THandle;
      yazilacakVeri: AnsiString;
      intA: Integer;
    begin
      hPrn := StartRawPrintJob(PChar(pYazici), '', 'Varakim');
      if (Integer(hPrn) < 0) then
      Begin
        ShowMessage('StartRawPrintJob Hatalı');
        Exit;
      End;
    
      if (StartRawPrintPage(hPrn) < 0) then
      Begin
        ShowMessage('StartRawPrintPage Hatalı');
        EndRawPrintJob(hPrn);
        Exit;
      end;
    
      For intA := 0 to pYazilacakListe.Count - 1 do
      Begin
        yazilacakVeri := pYazilacakListe[intA] + #13 + #10;
    
        if (PrintRawData(hPrn, PAnsiChar(yazilacakVeri), Length(yazilacakVeri)) < 0)
        then
        begin
          ShowMessage('PrintRawData Hatalı');
          EndRawPrintPage(hPrn);
          EndRawPrintJob(hPrn);
          Exit;
        End;
      End;
      if (EndRawPrintPage(hPrn) < 0) then
      begin
        ShowMessage('EndRawPrintPage Hatalı');
        EndRawPrintJob(hPrn);
        Exit;
      End;
    
      if (EndRawPrintJob(hPrn) < 0) then
      begin
        ShowMessage('EndRawPrintJob Hatalı');
        Exit;
      End;
    End;
    

    Usage:

    StrLstYazdir(Memo1.Lines ,'Lexmark Forms Printer 2491')
    
    0 讨论(0)
  • 2021-01-03 12:45

    You're using the wrong function. Use Escape, passing the PASSTHROUGH flag as the second parameter. This sends the raw, unprocessed escape codes to the printer directly.

    Joe Hecht (formerly of Borland) has posted a unit several times that makes this easier. I found unit PrtRaw here.

    0 讨论(0)
  • 2021-01-03 12:45

    Your current code is sending data to the printer in the wrong format due to changes between Ansi and Unicode characters. The printer you're using is evidently able to tolerate some amount of error, which is why some of your commands worked, but there's a limit.

    In your version of Delphi, Char is equivalent to WideChar, so change your Char code to use AnsiChar instead, so you can send one-byte characters, as the printer expects. Your PrintRawData function was fine before. Your change is wrong. The printer does not expect to receive two-byte Unicode characters, but that's what your change amounts to.

    After restoring the original PrintRawData code, change your SendStr function to this:

    procedure TMainWindow.SendStr(const Text: string);
    var
      data: AnsiString;
    begin
      data := Text;
    
      if (PrintRawData(printHandle,
                       PAnsiChar(data),
                       Length(data)) < 0) then begin
        ShowMessage('PrintRawData Failed');
        EndRawPrintPage(printHandle);
        EndRawPrintJob(printHandle);
      end;
    end;
    

    I made the following changes to the code:

    1. Replace the Char array with an AnsiString.
    2. Instead of growing the data array one character at a time with a loop, do the Unicode-to-Ansi conversion with a single assignment statement and let the RTL take care of the conversion.
    3. Type-cast the data string to PAnsiChar for passing to PrintRawData.
    4. Pass string parameters as const unless you need to modify their contents.
    5. No need for an explicit exit statement when the function is already finished.
    0 讨论(0)
提交回复
热议问题