Step 2 Send Printing Data: Template Printing Function
You can combine and print the template and field data generated in Step 1.
Send the printing data using the procedure below.
The sample code is available at the bottom of the page.
Also, refer to the sample application in the SDK.
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 Step 2.
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.
val printer = StarPrinter(settings, applicationContext)
3 Set the template created in Step 1 in the template property variable.
try { var template: string = ... printer.template = template;
Memo
In Send Printing Data: Standard Function, the printing data created with StarXpandCommandBuilder is passed as the argument of the printAsync method.
On the other hand, to print the template, you need to set it in the template property.
4 Set the field data prepared in Step 1 in the fieldData variable.
let fieldData = …
5 Call the open method to connect to the printer.
await printer.open();
6 Call the print method to send the field data to the printer. The template is printed in a combination of the template set in the template property and the field data.
await printer.print(fieldData);
7 Identify the error cause based on the error type and implement the process for when an error occurs. Possible errors are described in the API reference of the open method and print method of the StarPrinter class.
catch(error) { if (error instanceof StarIO10ArgumentError) { } else if (error instanceof StarIO10NotFoundError) { } }
8 Call the close method to disconnect from the printer.
await printer.close();
9 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 {
// Set the print data created in Step 1 to the commands variable.
var template: string = ...
printer.template = template;
// Set the field data prepared in Step 1 to the fieldData variable.
var fieldData = …
await printer.open();
await printer.print(fieldData);
}
catch(error) {
if (error instanceof StarIO10ArgumentError) {
// Argument is not correct.
// This may be due to invalid template or field data.
}
else 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();
}