[C#.NET] PrintStoredFormat doesn't make printer print the stored format file

Tags: 

I am using Zebra SDK v2.15.2634 to develop Xamarin.Android application. It looks good until I try to execute following line:

var btAddress = "48:A4:93:89:63:97";
Connection connection = new BluetoothConnection(btAddress);
connection.MaxTimeoutForRead = 30000;

ZebraPrinter printer;
try
{
connection.Open();

// Successful printed.
//var bytes = Encoding.UTF8.GetBytes("^PON^LH0,0^FWN^XA^FO150,200^FDHelloWorld^FS^XZ");
//connection.Write(bytes);

printer = ZebraPrinterFactory.GetInstance(connection);

Dictionary<int, string> vars = new Dictionary<int, string>
{
};

printer.PrintStoredFormat("E:HELLO2.ZPL", vars, "utf-8");

}
catch (ConnectionException e)
{
Console.WriteLine(e.ToString());
}
catch (ZebraPrinterLanguageUnknownException e)
{
Console.WriteLine(e.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{

connection.Close();

}

The printer showed it got bluetooth activity by flashing the icon on display. But nothing print out.

Environment:
- Printer model: ZQ520
- Zebra SDK v2.15.2634
- Visual Studio 2019
- Xamarin.Android

FYI:
- There is HELLO2.ZPL file on the printer's flash storage.
- Successfully printed, if I send this file to printer via Printer Tools Program.
- There is no problem with connection, my app can connect to printer successfully.
- I can execute BluetoothConnection.Write() method successfully with printed slip, on following code:

var bytes = Encoding.UTF8.GetBytes("^PON^LH0,0^FWN^XA^FO150,200^FDHelloWorld^FS^XZ");
connection.Write(bytes);

The stored file is the problem.
- I used both manual created ZPL file and from Zebra Designer 3, both stored on the printer.
- I tried to PrintStoredFormat() with both of them. Nothing happened.
- The code has no exception and the connection was closed successfully.

How to reproduce the project and file I attached:
- Please store "HELLO2.ZPL" (manually created) and GT.ZPL (Zebra Designer generated) on the ZQ520

Additional question
- Do we have to name file in capital letter before send to printer? for ex. hello2.zpl
- if not, do we still to put its into capital letter when we refer to its path? for ex. printer.PrintStoredFormat("E:HELLO2.ZPL", vars, "utf-8");

Steven Si
Based on the code snippet

Based on the code snippet above, there are a few things I'd like to comment on.

1. The filenames of the files stored on the printer are case insensitive. You can use either uppercase, lowercase or mixed cases. They all should work.

2. It's unclear if the try{} block is executed on a UI thread or a background thread. When interacting with the printer, such as getting printer status or executing print functions via Link-OS SDK API, please do not use the UI thread. Always spin off a separate thread when interacting with the printer.

3. After calling printer.PrintStoredFormat() API, please add a delay before closing the connection to make sure that the data has been transmitted to the printer at Bluetooth link layer. The return of printer.PrintStoredFormat() API doesn't necessary mean that the entire data has been sent to the printer at the link layer. The suggested code is below. Hope this helps.

await Task.Run(async () => {
    try {

        connection.Open();

        ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);

        Dictionary<int, string> vars = new Dictionary<int, string> { };

        printer.PrintStoredFormat("E:HELLO2.ZPL", vars, "utf-8");

        await Task.Delay(1000);

    } catch (Exception e) {
        await DisplayConnectionStatusAsync($"Error: {e.Message}", Color.Red, 3000);
    } finally {
        try {
            connection?.Close();

        } catch (ConnectionException) { }
    }
});

 

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Teerasej Jiraph...
Printer responded flashing bluetooth icon, but nothing printed

I adapted the code following your suggestion. But the result still the same.

1. PrintStoredFormat() executed, there's bluetooth indicator flashed several time on the printer
2. The Task delayed as we set it, I adjust its to 5000 ms.
3. After delay the process reached connection.Close() without exception.
4. Nothing printed out.

I also at gist that show the code on MainActivity.cs which only execute after press the button, here https://gist.github.com/teerasej/12a56fbd298166c36dac89fa4c77e280

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Steven Si
Your HELLO2.ZPL has issues.

Your HELLO2.ZPL has issues. It should start with ^XA. Please use the attached HelloWorld.ZPL.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Teerasej Jiraph...
I cannot find the attachment.

I cannot find the attachment. But I remove code in the box as you mentioned, then stored its in the printer, then try again. Still nothing printed.

Before: ^PON^LH0,0^FWN^XA^FO150,200^FDHelloWorld^FS^XZ
After remove that code: ^XA^FO150,200^FDHelloWorld^FS^XZ

Even I use the file from Zebra designer 3 (GT.ZPL, in the attachment I added to the post), the printer flashed bluetooth icon until connection closed, but nothing printed.

Am I understand it correctly?:
1. I generated ZPL file, by coding, or generated from Zebra Designer
2. I stored them into printer with Zebra printer tool (by using "Store file to printer, destination: internal flash)
3. I can use its with PrintStoredFormat method by specified its path, ex: "E:HELLO.ZPL"

and can we just send an empty dictionary as the 2nd parameters? because there is not need for variable in the template.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Steven Si
The HelloWorld.ZPL was

The HelloWorld.ZPL was attached to this discussion thread, which is just under your attachment in your initial post. You can remove the _.txt extension, which was added by the unexpected by the upload tool. I've tested the HelloWorld.ZPL file with the code snippet and they worked for me.

Your understanding is correct. If there is no variable in the template to replace, then we simply pass in an empty Dictionary object.

When you say nothing printed, do you mean that the printer feeds the paper but nothing printed on the paper? If that's the case, then I would suspect that the media is not configured properly. Make sure the label or continuous paper is configured at least more than 200 dots in length, because of ^FO150,200 in your ZPL.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Teerasej Jiraph...
Thank you.

Thank you.

For nothing printed, I mean printer doesn't feed paper out with a blank label. It just keep silent.

I have tried with your ZPL file with the code. But no paper feed, nothing printed out. I recorded the video that show the situation here:

https://www.dropbox.com/s/84mxz74zanpz29g/zebra%20cant%20print.mp4?dl=0

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Steven Si
Hi Teerasej,

Hi Teerasej,

Thanks for sharing the video, which is very helpful. It helped us to find a bug apparently in the "Store file to printer" function in the driver.

After the driver uploads HelloWorld.ZPL to the printer, somehow, an extra pair of ^XA & ^XZ is added to the template, which causes PrintStoredFormat() API seems doing nothing.

I'd suggest you manually remove the extra pair of ^XA & ^XZ through http://<your printer ip>/index.html. Go to Directory Listing, where you can edit the file. The default password is 1234.

Alternatively, you can use ZebraDesigner for Developer 3 to create a template and send the template to the printer within ZebraDesigner for Developer 3 tool. Hope this helps.

[5/7/2021] After further investigation, our engineering team has confirmed that "Store file to printer" option is not for ZPL files. If we want to store the files that have ZPL content, such as ZPL template files, we need to use ^DF command and "Send file to printer" option instead.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments