1

I have an issue here, which I am trying to encode magnetic stripe data to an Fargo DTC400 printer, in the specifications it says I need to send the following string commands from example notepad, wordpad etc etc :

~1%TRACK NUMBER ONE?
~2;123456789?
~3;123456789?

this example encodes the string in track one, and the numbers 123456789 in both track 2 and 3.. this works from Notepad.exe.

EDIT: Current delphi code I use works on another printer:

procedure SendQuote(MyCommand : AnsiString);
var
  PTBlock       : TPassThrough;

begin
  PTBlock.nLen := Length(MyCommand);
  StrPCopy(@PTBlock.SData, MyCommand);
  Escape(printer.handle, PASSTHROUGH, 0, @PTBlock, nil);
end;

when I am trying to encode this string from my own application I get trouble, it seems the printer is totally ignoring my commands, when I choose print to file, I can read the binary data and see my string in the printed file, when I try to print to file from example notepad.exe I get just rubish binary data and cannot find my strings at all...

so I wonder what does notepad do to send this string command which I dont ?

hope someone can shed light on this because I have been eager to implement fargo support in my application for a longer period of time .

thanks

Update. the following code is ancient but it does the job, however is there another way I can use this with the Passthrough code above?

var
  POutput: TextFile;
  k: Integer;
begin
  with TPrintDialog.Create(self) do
  try
    if Execute then
    begin
      AssignPrn(POutput);
      Rewrite(POutput);

      Writeln(POutput,'~1%TESTENCODER?');
      Writeln(POutput,'~2;123456789?');
      Writeln(POutput,'~2;987654321?');
      CloseFile(POutput);
    end;
  finally
    free;
  end
end;
Plastkort
  • 957
  • 1
  • 26
  • 40
  • 3
    This is a programming questions website. Please tell me what you're doing in your Delphi code. How are you printing? Don't make us guess what you're doing wrong. Show us how you print. I would have tested writing to a raw printer port like this. See this link for Raw Printing info. http://www.efg2.com/Lab/Library/Delphi/Printing/index.html – Warren P Mar 21 '12 at 20:27
  • 1
    yeah I forgot to paste the code I use, sorry :) I updated it now – Plastkort Mar 21 '12 at 21:07
  • What's wrong with using the `TextFile` output ? Seems like a clear and simple solution. – LU RD Mar 21 '12 at 21:21
  • What does the implementation of TPassthrough do, in the end. You're showing something high level that nobody here can tell you anything about. You have the source code. Follow it through (single step) until you find either a win32 api call, or something low level enough that people can help you understand it better. Right now, showing one of your classes that calls another one of your classes (TPassthrough -what is that?) is not answerable. – Warren P Mar 21 '12 at 21:21
  • @LU RD, I would use text files if it was not for one thing. I will also require to send bitmap to printer and combining this will not work so well i think, my app have two option, encoding only, and encoding together with bitmap. single encoding no problem to use textfile, but together with bitmap I have to combine with begindoc and enddoc – Plastkort Mar 21 '12 at 22:28
  • What makes you think this printer will print your bitmap? – Warren P Mar 22 '12 at 13:25

1 Answers1

4

TPassThrough should be declared like this :

type 
  TPassThrough = packed record 
    nLen  : SmallInt; 
    SData : Array[0..255] of AnsiChar; 
  end; 

You might be using a modern Delphi (2009 or newer) or forgotten the packed directive.

See also this SO question for a correct-way-to-send-commands-directly-to-printer.

At Torry's there is an example snippet (written by Fatih Ölçer): Remark : Modified for use with Unicode Delphi versions as well.

{
  By using the Windows API Escape() function,
  your application can pass data directly to the printer.
  If the printer driver supports the PASSTHROUGH printer escape,
  you can use the Escape() function and the PASSTHROUGH printer escape
  to send native printer language codes to the printer driver.
  If the printer driver does not support the PASSTHROUGH printer escape,
  you must use the DeviceCapabilities() and ExtDevMode() functions instead.


  Mit der Windows API Funktion Escape() kann man Daten direkt zum Drucker schicken.
  Wenn der Drucker Treiber dies nicht unterstützt, müssen die DeviceCapabilities()
  und ExtDevMode() Funktionen verwendet werden.
}

//  DOS like printing using Passthrough command
// you should use "printer.begindoc" and "printer.enddoc"

type
  TPrnBuffRec = packed record
  bufflength: Word;
  Buff_1: array[0..255] of AnsiChar;
end;

function DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
var 
  Buff: TPrnBuffRec;
  TestInt: Integer;
begin
  TestInt := PassThrough;
  if Escape(Printer.Handle, QUERYESCSUPPORT, SizeOf(TESTINT), @testint, nil) > 0 then
  begin
    if NextLine then  S := S + #13 + #10;
    StrPCopy(Buff.Buff_1, S);
    Buff.bufflength := StrLen(Buff.Buff_1);
    Escape(Printer.Canvas.Handle, Passthrough, 0, @buff, nil);
    Result := True;
  end
  else
    Result := False;
end;

// this code works if the printer supports escape commands
// you can get special esc codes from printer's manual

//  example:
printer.BeginDoc;
try
  DirectToPrinter('This text ');
finally
  printer.EndDoc;
end;
Community
  • 1
  • 1
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • yes, I am trying to make all to unicode but unicode not really needed for this part, since magnetic stripes are limited to ascii chars... altho i tried this declaration but nothing changed... it seem the strings are sent in a different way – Plastkort Mar 21 '12 at 22:26
  • 1
    I will take a look at the stack page tomorrow :) too late for me now, thanks for al for now, will continue testing – Plastkort Mar 21 '12 at 22:27
  • I have been testing alittlebit more today, in another program, notepad++ it also seem to be able to encode my printern, and from support from fargo I am doing the correct thing, but might this be an character encoding issue ? – Plastkort Mar 22 '12 at 17:24
  • According to the ascii-table the ~ character is #126. The others are between #32 and #95. I can't understand why this can be an encoding issue. – LU RD Mar 22 '12 at 18:41
  • yeah same here. it's very strange, i dont know where to start looking right now :( theroutine above did not work I am sending this after begindoc and before enddoc – Plastkort Mar 22 '12 at 19:04