6.2. ICommandBuilder Migration Guide (A Function Incorporated in the StarPRNT V5)

This is an ICommandBuilder migration guide to minimize printer-model dependency.

ICommandBuilder is a function that enables human-readable command creation instead of conventional binary command creation and incorporated in the extension library of StarPRNT V5.0 or later (hereinafter, StarIOExtension )

Until now, developers had to create commands after understanding the specifications of printer emulation, however this function can reduce learning costs and man-hours for adding printer models

Please use ICommandBuilder referring to this guide.

6.2.1. Required Work Contents

The works required to migrate to ICommandBuilder and add a printer model are as follows.

  1. Implement StarIOExtension
  2. Change API and command creation method
  3. Change emulation
  4. Specify portName

Important

Depending on the combination of printer models, it may be necessary to change the portSettings specification .

6.2.2. Implement StarIOExtension

Make sure that your project includes StarIOExtension. Use libraries of the same SDK version.

Nuget package

winmd files

  • StarIOPort.winmd
  • StarIO_Extension.winmd
  • SMCloudServices.winmd

Details of the library installation procedure are described in How to configuration StarPRNT Application .

6.2.3. How to change API and command creation method (Introduction of ICommandBuilder)

Differences in overall flow

As shown in the following flow, no changes other than the command creation method are required to use ICommandBuilder .

migrationFlowEN

Changes in print data creation program (printer built-in font)

The code for printing the printer built-in font used for characters on receipts will change as shown in the sample below. Please make the necessary change according to the function you are using.

See ICommandBuilder for a list of supported features.

Note

The TSP100 series does not support printer fonts. Please see Changes in Changes in print data creation program (image data)

Before: Using only StarIOPort
string command = "\x1b\x40";       // Initialization
command += "\x1b\x1d\x61\x01";     // Alignment
command += "Hello world";
command += "\x1b\x64\x02";         // Cut paper
After: Using StarIOPort + ICommandBuilder/StarIO_Extension
ICommandBuilder builder = StarIO_Extension.StarIoExt.CreateCommandBuilder(emulation);

builder.BeginDocument();
builder.AppendAlignment(AlignmentPosition.Right);
builder.AppendData(Encoding.GetEncoding(encoding).GetBytes("Hello world").AsBuffer());
builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed);
builder.EndDocument();

builder.GetCommands()

Changes in print data creation program (image data)

The code for printing image data used for store logos etc.

See ICommandBuilder for a list of supported features.

After: Using StarIOPort + ICommandBuilder/StarIO_Extension
BitmapDecoder starLogoBitmapDecoder = await CreateCouponImageAsync("star_logo_image.png");
ICommandBuilder builder = StarIO_Extension.StarIoExt.CreateCommandBuilder(emulation);

builder.BeginDocument();
await builder.AppendBitmapAsync(starLogoBitmapDecoder, true);
builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed);
builder.EndDocument();

builder.GetCommands();

6.2.4. How to add a printer model

When adding or migrating a printer model, if you are using ICommandBuilder to create printer commands, you will only need to change the emulation in most cases.

However, in some situations, additional changes are required. Please make the necessary changes referring to the information below.

How to change the emulation ( ICommandBuilder )

The mC-Print3 and other printers may support different emulations.

Check the supported emulation on the Emulation page and change the emulation when creating an ICommandBuilder instance.

For example, in case of TSP100/TSP650II and mC-Print3, it is necessary to change the emulation according to the table below.

Printer Emulation
TSP650II StarLine
TSP100 StarGraphic
mC-Print3 StarPRNT
Sample code for emulation change
// TSP650II (StarLine)
// Emulation emulation = Emulation.StarLine;

// TSP100 (StarGraphic)
// Emulation emulation = Emulation.StarGraphic;

// mC-Print3 (StarPRNT)
Emulation emulation = Emulation.StarPRNT;
ICommandBuilder builder = StarIO_Extension.StarIoExt.CreateCommandBuilder(emulation);

How to search for printers ( FindAllAsync )

The printer search method by FindAllAsync is the same.

If the model name obtained from the search is used for some determination in the application, it is necessary to change it

For example, the printer model names included in the search results for TSP100/TSP650II and mC-Print3 are as follows.

LAN interfaces

Printer Model Name (PortInfo)
TSP100III “TSP143IIILAN (STR_T-001)”, “TSP143IIIW (STR_T-001)”
TSP650II “TSP654 (STR_T-001)”
mC-Print3 “MCP31 (STR-001)”, “MCP30 (STR-001)”

Bluetooth interface

Printer Model Name (PortInfo)
TSP100 “TSP100-XXXXX”
TSP650II “Star Micronics”
mC-Print3 “mC-Print3-XXXXX”

XXXXX : 5-digit identification number that differs for each product

How to specify ConnectAsync - portName parameter

If the return value of FindAllAsync ( ProductInformation ) is used to specify portName , there is no need to change the parameter.

However, if the parameter is specified directly (fixed value, etc.), the default value of the device name differs between mC-Print3 and other models, so the parameter may need to be changed.

For example, the default values of TSP100/TSP650II and mC-Print3 are as follows.

Printer Bluetooth DeviceName
TSP100 “TSP100-XXXXX”
TSP650II “Star Micronics”
mC-Print3 “mC-Print3-XXXXX”

XXXXX : 5-digit identification number that differs for each product

Sample
StarIOPort.Port port = new StarIOPort.Port();

string portSettings = "";

// TSP650II (Bluetooth)
// string portName = "BT:Star Micronics";

// TSP100 (Bluetooth)
// string portName = "BT:TSP100-XXXXX";

// mC-Print3 (Bluetooth)
// string portName = "BT:mC-Print3-XXXXX";

// No changes required if used "BT:"
string portName = "BT:";

await port.ConnectAsync(portName, portSettings);

Other interfaces

No specific device names are specified for each printer. Refer to the StarIOPort_getPort page and set the appropriate portName for each interface.

How to specify ConnectAsync - portSettings parameter

The identifier that should be used with each model may be different. Please change it referring to 2. portSettings Parameter of ConnectAsync .

For example, the identifiers of portSettings of TSP100/TSP650II and mC-Print3 are the same.

Printer portSettings
TSP100 “”
TSP650II “”
mC-Print3 “”
Sample
StarIOPort.Port port = new StarIOPort.Port();
string portName = "BT:";

// string portSettings = "l10000"
string portSettings = "";

await port.ConnectAsync(portName, portSettings);

How to specify the printable area

The initial value of the printable area setting differs depending on the printer models. Specify the printable area with AppendPrintableArea as necessary.

If graphic data is printed by using AppendBitmapAsync etc., please change the layout according to the printable area of the printer model you want to migrate to.

6.2.5. Code flow from command creation to data transmission

The following is the flow from command creation to writing. In actual operation, execute the following function in a non-UI thread.

public async Task<Result> printSample()
{
    Result result = Result.ErrorUnknown;
    try
    {
        Emulation emulation = Emulation.StarPRNT;
        ICommandBuilder builder = StarIO_Extension.StarIoExt.CreateCommandBuilder(emulation);
        builder.BeginDocument();
        builder.AppendAlignment(AlignmentPosition.Right);
        builder.AppendData(Encoding.GetEncoding(encoding).GetBytes("Hello world").AsBuffer());
        builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed);
        builder.EndDocument();

        IBuffer buffer = builder.GetCommands()

        using (StarIOPort.Port port = new StarIOPort.Port())
        {
            result = Result.ErrorOpenPort;

            String portName = "BT:";
            String portSettings = "";

            await port.ConnectAsync(portName, portSettings);

            StarIOPort.Status status;

            result = Result.ErrorBeginCheckedBlock;
            status = await port.BeginCheckedBlockAsync();

            if (status.Offline == true)
            {
                string message = "Printer is Offline.";

                if (status.ReceiptPaperEmpty == true)
                {
                    message += "\nPaper is Empty.";
                }
                if (status.CoverOpen == true)
                {
                    message += "\nCover is Open.";
                }
                throw new Exception(message);
            }

            result = Result.ErrorWritePort;
            if (await port.WriteAsync(buffer) != buffer.Length)
            {
                throw new Exception("WriteAsync failed.");
                }

            result = Result.ErrorEndCheckedBlock;
            status = await port.EndCheckedBlockAsync();

            if (status.Offline == true)
            {
                string message = "Printer is Offline.";


                if (status.ReceiptPaperEmpty == true)
                {
                    message += "\nPaper is Empty.";
                }
                if (status.CoverOpen == true)
                {
                    message += "\nCover is Open.";
                }
                throw new Exception(message);
            }
            result = Result.Success;
        }
    }
    catch (Exception)
    {
    }
    return result;
}