Receipt Printer Raw Data

I have a Epson TM-T20II printer I’m trying to print graphics and rawdata to. I can print the graphics fine but I want to send the rawdata command to cut the paper and can’t get it to work. I know my codes are right because I have it working in a VB.net app and I copied it from that (below). . Can some one help with sending the codes to the printer. Below is my VB.net code and my what I have so far for Xojo. This is on Windows.

'VB.NET code that works
Private prn As New RawPrinterHelper
Dim PrintString As String
''sends 3 line breaks and then autocuts
PrintString = Chr(10) & Chr(10) & Chr(10) & Chr(27) & Chr(109)
prn.OpenPrint(PrinterName)
prn.SendStringToPrinter(PrinterName, PrintString)
prn.ClosePrint()

'XOJO code----works for printing my graphics but need to autocut at end.
Dim ps As New PrinterSetup
Dim page As Graphics
page = OpenPrinter(ps)
If page <> Nil Then
Dim p As New Picture(Rectangle1.Width, Rectangle1.Height)
Rectangle1.DrawInto(p.Graphics, 0, 0)
Dim scale As Double = 0.75
page.DrawPicture(p, 10, 0, p.Width * scale, p.Height * scale, 0, 0, p.Width, p.Height)
end if

This may help:

Although I would have been looking for a way to send codes to the printer in the way that the VB code does.

Xojo printing assembles a picture, and then prints the picture.
You will only get a cut if the printer driver cuts when it gets a new page (or end of page)

I used to enjoy sending actual codes to a Printer via the LPT1: port
Thats basically what the VB code is doing - it doesnt send a page full of pixels, it sends a handful of bytes.

Perhaps using an API call…

Thanks Jeff. That is helpful but it looks like that thread already has the autocut feature set in the printer preferences. I guess I was looking for how to send a “cut” command to the printer.

Don’t know if this helps, but here’s an old project that sends escape sequences to a printer. Xojo’s built-in printing cannot send escape sequences. You have to use declares. And it’s all of one or all of the other. You can’t mix them.

2 Likes

Thanks Tim, not sure if that will work for me but it’s worth a look.

It’s only for windows, is there the same for macOS ?

is there the same for macOS ?

In windows and in Mac, you might ‘fall back’ on sending a text file holding the right bytes, to a nominated port.
I found this on another forum showing how to use a bash script to do it on a Mac…

data="\x1Bia\x00"
data="${data}\x1B@"
data="${data}\x1BX\x32"
data="${data}Hello"
data="${data}\x0C"

# Check your own binary for debugging:
echo -ne $data | hexdump -C

# print the actual label
echo -ne $data | lp -d Brother_PT_P900W
```

\x1B is ‘escape’ (hex 1B = 27)

So what the above is doing is assembling a variable called data and pushing that to
lp (local printer)
-d described as …

You might also be able to create a simple text file with a LOT of bytes in it, and use cat to do it.
eg
sudo cat my-file-to-print.txt > /dev/lp

We have WindowsAddPrintJobMBS class in MBS Xojo Win Plugin.

It allows to send raw PostScript to laser printer, but may also work for label printers.

I’d like to add, when in doubt, use MBS. Christian will do you right.

2 Likes

Thanks everyone. I do have a license for MBS plugins so I’ll take a look at WindowsAddPrintJobMBS too.

1 Like

I took Christians suggestion and tried printing the raw PostScript to the thermal receipt printer and it worked! The only issue is that it prints the raw postscript first instead of after my “DrawPicture”. So it cuts the paper and then prints the DrawPicture and then doesn’t cut Not sure how to make it wait until the page.DrawPicture is done.

'prints rectangle
page.DrawPicture(p, 10, 0, p.Width * scale, p.height * scale, 0, 0, p.Width, p.height)

'creates the cutting of the paper
const PrinterName = “EPSON TM-T20II Receipt”
dim w as new WindowsAddPrintJobMBS

if not w.OpenPrinter(PrinterName) then
MsgBox “OpenPrinter failed. Is the printer name correct in the source code?”
Return
end if

const DocName = “My Document”

if not w.StartDocPrinter(“My Document”, w.kDataFormatRAW) then
MsgBox “StartDocPrinter failed.”
Return
end if

call w.StartPagePrinter

Dim PostScript as string = Chr(27) + Chr(109)

dim BytesSent as Integer = w.WritePrinter(PostScript)

call w.EndPagePrinter
call w.EndDocPrinter

w = nil // close printer