infoxicator / react-native-star-prnt

React-Native bridge to communicate with Star Micronics Bluetooth/LAN Printers
MIT License
67 stars 65 forks source link

StarIOExtManager print not working #41

Open khaledwj90 opened 5 years ago

khaledwj90 commented 5 years ago

I'm not able to print it says invalid port name check my code below:

`printInvoice = async () => { try { var connect = await StarPRNT.connect('BT:00:12:F3:3A:66:1B', 'StarPRNT', true); console.log(connect); // Printer Connected! } catch (e) { console.error(e); }

    let commands = [];
    commands.push({
        append:
            "Star Clothing Boutique\n" +
            "123 Star Road\n" +
            "City, State 12345\n" +
            "\n"
    });
    commands.push({appendCutPaper: StarPRNT.CutPaperAction.PartialCutWithFeed});

    try {
        var printResult = await StarPRNT.print('StarPRNT', commands);
        console.log(printResult); // Success!
    } catch (e) {
        console.error(e);
    }
};`

however, it gives below error:

Printer Connected ExceptionsManager.js:82 Error: Invalid port name. at createErrorFromErrorData (NativeModules.js:155) at NativeModules.js:104 at MessageQueue.__invokeCallback (MessageQueue.js:414) at MessageQueue.js:127 at MessageQueue.__guard (MessageQueue.js:314) at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126) at debuggerWorker.js:80

hunwalk commented 5 years ago

Try the followings:

Option 1:

Make sure that the port is exactly the port of the device, and you're connected to it. Always try to connect dynamically, do not hardcode the port inside the connection script.

Option 1.5:

Use another emulation. If you're using mPop StarPRNT is fine but raster printers like the TSP100 and many others are using StarGraphics. (however bad emulation shouldnt throw this error, but it's worth a try)

Option 2:

I've encountered a strange bug a while ago. I don't know if it has been fixed or not, but when you use connect and print together, IOS does not ask for the port inside the print function however android did, even if it was connected. Solution:

if (Platform.OS === 'ios') {
  var printResult = yield StarPRNT.print(data.printer.emulation, data.printer.commands)
}else{
  var printResult = yield StarPRNT.print(data.printer.emulation, data.printer.commands, data.printer.portName)
}

keep in mind, that I'm using a generator function because of redux-saga, so don't freak out when you see the yield keyword, just use await

Option 3:

Disconnect your barcode scanner, and do not use connect, just try to freefall print with:

var printResult = yield StarPRNT.print(data.printer.emulation, data.printer.commands, data.printer.portName)

If you want to get the status of the printer once in a while, just connect listen for hardware events, then disconnect. I used to get invalid port name myself sometimes, i solved it with try-catching and reconnecting, it usually works, and reporting back connection errors to my server but i did not get constant data.