desionlab / node-cashcode-bv

Library for working with "CashCode" Bill Validator devices from Node.js app.
MIT License
11 stars 8 forks source link

TypeError: this.onSerialPortClose is not a function #8

Open sarathjasrin opened 5 years ago

sarathjasrin commented 5 years ago

When calling device.close() method it's gives TypeError: this.onSerialPortClose is not a function. The light is also showing green because of that.

BillValidator : Cashcode MSM 3021

fenixphp commented 5 years ago

Please provide sample code.

sarathjasrin commented 5 years ago
const { CCNet } = require('cashcode-bv');
let device = new CCNet.BillValidator(getConfig().cash_reader_com);
class CashReader {
    async getCash(amt) {
        amount = amt;
        await device.connect()
            .then(res => {
                if (res) {
                }
            }).catch(err => {
                console.log("Connection Error", err);
                console.log("Reconnecting...");
                device.connect();
            });

        await device.begin();
    }

    async stopCash() {
        console.log("stoping BV");
        await device.end();    
    }

    async close() {        
        await device.end();
        await device.close()
            .then(res => {
                console.log("cash reader is disconnected", res);
            }).catch(err => {
                console.log("cash reader is not disconnected", err);
            });
    }
}

Error Message:

(node:9792) UnhandledPromiseRejectionWarning: TypeError: this.onSerialPortClose is not a function
    at SerialPort.<anonymous> (C:\Recharge\recharge-kiosk\node_modules\cashcode-bv\Source\CCNet\BillValidator.js:76:12)
    at SerialPort.emit (events.js:182:13)
    at binding.close.then (C:\Recharge\recharge-kiosk\node_modules\@serialport\stream\stream.js:434:12)
(node:9792) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 8)
Error: Request timeout.
    at Timeout.timerHandler [as _onTimeout] (C:\Recharge\recharge-kiosk\node_modules\cashcode-bv\Source\CCNet\BillValidator.js:814:16)
    at ontimeout (timers.js:425:11)
    at tryOnTimeout (timers.js:289:5)
    at listOnTimeout (timers.js:252:5)
    at Timer.processTimers (timers.js:212:10)
Error: Request timeout.
    at Timeout.timerHandler [as _onTimeout] (C:\Recharge\recharge-kiosk\node_modules\cashcode-bv\Source\CCNet\BillValidator.js:814:16)
    at ontimeout (timers.js:425:11)
    at tryOnTimeout (timers.js:289:5)
    at listOnTimeout (timers.js:252:5)
    at Timer.processTimers (timers.js:212:10)
fenixphp commented 5 years ago

How exactly do you use this class CashReader?

sarathjasrin commented 5 years ago

I'm using it in a Electron app. When I click stop cash button in front end it will trigger stop cash event. Once the event triggered I used to call stopCash() function.

const { ipcMain, BrowserWindow } = require('electron');
ipcMain.on('stop-cash', async () => {
    let bv = new CashReader();
    bv.stopCash();
});
fenixphp commented 5 years ago

yes, there is an error in the library code, you can intercept it with "try", I will fix how it will be possible.

sarathjasrin commented 5 years ago

could you please tell me where I need to put try catch ??

fenixphp commented 5 years ago
try {
  await device.close();
} catch (error) {

}

I'm not sure that the place is right .. now there is no PC at hand to check.

sarathjasrin commented 5 years ago

Yep. The above suggestion is not working. please let me know once you have the time to check. Thanks for the support.

sarathjasrin commented 5 years ago

The BV is showing green light even after await device.end(). Is there anyway I can turn off that?