StarXpand SDK for React Native Developer's Manual Ver. 1.6.0

Last update: February 9, 2024

Use Cash Drawer

Generate control data for opening the cash drawer, and then send it to the printer.
To generate control data, use the DrawerBuilder class, and to send it, use the StarPrinter class.

Control the cash drawer using the procedure below.
The sample code is available at the bottom of the page.

1 Create the printer destination information.
Set interfaceType, which is used for communication, and identifier, which specifies the destination.
If the destination printer has already been searched for in "Search for Printer", this setting is unnecessary.
Set the StarPrinter instance acquired with the onPrinterFound method in the printer variable in Step2.

var settings = new StarConnectionSettings();
settings.interfaceType = InterfaceType.Lan;
settings.identifier = “00:11:62:00:00:01”;

2 Provide the printer destination information to acquire the StarPrinter instance.

var printer = new StarPrinter(settings);

3 Generate control data for opening the cash drawer, and then set it in the commands variable.

Memo
To control the cash drawer together with printing, add PrinterBuilder and DrawerBuilder to DocumentBuilder according to Generating Printing Data: Standard Function.

try {
    var builder = new StarXpandCommand.StarXpandCommandBuilder();
    builder.addDocument(new StarXpandCommand.DocumentBuilder()
        .addDrawer(new StarXpandCommand.DrawerBuilder()
        .actionOpen(new StarXpandCommand.Drawer.OpenParameter()
            .setChannel(StarXpandCommand.Drawer.Channel.No1)
        )
        )
    );

    var commands: string = await builder.getCommands();

4 Call the open method to connect to the printer.

await printer.open();

5 Call the print method to send the control data for opening the cash drawer to the printer.

await printer.print(commands);

6 Implement the process for when an error occurs. You can identify the error cause based on the error instance type.
Possible errors are described in the API reference of the open method and print method of the StarPrinter class.

}
catch(error) {
    if (error instanceof StarIO10NotFoundError) {
        // Printer not found.
        // This may be due to the printer not being turned on or incorrect connection information.
    }

    console.log(`Error: ${String(error)}`);
}

7 Call the close method to disconnect from the printer.

finally {
    await printer.close();

8 Call the dispose method to dispose the printer instance.

await printer.dispose();
}
Sample code

var settings = new StarConnectionSettings();
settings.interfaceType = InterfaceType.Lan;
settings.identifier = "00:11:62:00:00:01";

var printer = new StarPrinter(settings);

try {
    var builder = new StarXpandCommand.StarXpandCommandBuilder();
    builder.addDocument(new StarXpandCommand.DocumentBuilder()
        .addDrawer(new StarXpandCommand.DrawerBuilder()
          .actionOpen(new StarXpandCommand.Drawer.OpenParameter()
            .setChannel(StarXpandCommand.Drawer.Channel.No1)
          )
        )
    );
	
    var commands: string = await builder.getCommands();
    await printer.open();
    await printer.print(commands);
}
catch(error) {
    if (error instanceof StarIO10NotFoundError) {
        // Printer not found.
        // This may be due to the printer not being turned on or incorrect connection information.
    }
    console.log(`Error: ${String(error)}`);
}
finally {
    await printer.close();
    await printer.dispose();
}