Ok, here is how i do it:
procedure TMainWindow.btnRawPrintClick(Sender: TObject);
begin
BeginPrint;
SendStr(#27#69);
SendStr(\'MyData\');
SendStr(#10
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:
Char
array with an AnsiString
.PAnsiChar
for passing to PrintRawData
.exit
statement when the function is already finished.