{"action":"closed","issue":{"url":"https://api.github.com/repos/node-serialport/node-serialport/issues/1508","repository_url":"https://api.github.com/repos/node-serialport/node-serialport","labels_url":"https://api.github.com/repos/node-serialport/node-serialport/issues/1508/labels{/name}","comments_url":"https://api.github.com/repos/node-serialport/node-serialport/issues/1508/comments","events_url":"https://api.github.com/repos/node-serialport/node-serialport/issues/1508/events","html_url":"https://github.com/node-serialport/node-serialport/issues/1508","id":301461885,"node_id":"MDU6SXNzdWUzMDE0NjE4ODU=","number":1508,"title":"How do I restart .on('data') handler ?","user":{"login":"scorpioRED","id":12165650,"node_id":"MDQ6VXNlcjEyMTY1NjUw","avatar_url":"https://avatars3.githubusercontent.com/u/12165650?v=4","gravatar_id":"","url":"https://api.github.com/users/scorpioRED","html_url":"https://github.com/scorpioRED","followers_url":"https://api.github.com/users/scorpioRED/followers","following_url":"https://api.github.com/users/scorpioRED/following{/other_user}","gists_url":"https://api.github.com/users/scorpioRED/gists{/gist_id}","starred_url":"https://api.github.com/users/scorpioRED/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scorpioRED/subscriptions","organizations_url":"https://api.github.com/users/scorpioRED/orgs","repos_url":"https://api.github.com/users/scorpioRED/repos","events_url":"https://api.github.com/users/scorpioRED/events{/privacy}","received_events_url":"https://api.github.com/users/scorpioRED/received_events","type":"User","site_admin":false},"labels":[{"id":1062256547,"node_id":"MDU6TGFiZWwxMDYyMjU2NTQ3","url":"https://api.github.com/repos/node-serialport/node-serialport/labels/stale-issue","name":"stale-issue","color":"ffffff","default":false},{"id":46026455,"node_id":"MDU6TGFiZWw0NjAyNjQ1NQ==","url":"https://api.github.com/repos/node-serialport/node-serialport/labels/support","name":"support","color":"fbca04","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-03-01T16:06:26Z","updated_at":"2018-11-25T12:43:25Z","closed_at":"2018-11-25T12:43:25Z","author_association":"NONE","body":"- __SerialPort Version__: 6.1.1\r\n- __NodeJS Version__: 8.6.0\r\n- __Operating System__ and __Hardware Platform__: linux\r\n\r\n- Have you checked the right __version__ of the __api docs?__: yes\r\n- Are you having trouble installing and you checked the __Installation Special Cases__ docs? \r\n- Are you using __Electron__ and have you checked the _Electron Docs_?: no\r\n\r\n### Summary of Problem\r\n\r\nI try to create Class which should based on node-serialport.\r\nThis Class has methods which should send data through serial port then return answer from device (promise). As I understand, there is only one way to get answer from device (through serial port) it's use **serialport.on('data')** handler. But I need send end receive data many time. How do I do that correct , without direct answer form my request to device by **serialport .write** ?\r\n\r\n### Steps and Code to Reproduce the Issue\r\n\r\n```\r\n\r\nclass HardwareManager {\r\n    constructor(path) {\r\n        this.port = new SerialPort(path, {\r\n            baudRate: 9600,\r\n            dataBit: 8,\r\n            autoOpen: false\r\n        });\r\n\r\n        this.port.open((err) => {\r\n            if (err) {\r\n                return console.log('Error opening port: ', err.message);\r\n            }\r\n\r\n        });\r\n\r\n    }\r\n\r\n\r\n    _runCMD(cmd) {\r\n        let self = this;\r\n        let counter = 0;\r\n        let response = \"\";\r\n        return new Promise((resolve, reject) => {\r\n            self.port.write(cmd);\r\n            self.port.on('data', (data) => {\r\n                counter++;\r\n                response = counter > 1 && counter < 4 ? response + data.toString('hex') : \"\";\r\n                if (counter == 3) {\r\n                    console.log(\" stop answer for response\");\r\n                    self.port.write(Buffer.from(\"06\", \"hex\"));\r\n\r\n                    let toResolve = self._responseParser(response, cmdTitle);\r\n                    counter = 0;\r\n                    resolve(toResolve);\r\n                }\r\n    actionB(quontity) {\r\n        let dataToSend = Buffer.from(this.formater(quontity) \"hex\")\r\n        return this._runCMD(dataToSend, \"deposit\")\r\n            })\r\n        })\r\n    }\r\n\r\n    actionA() {\r\n        let dataToSend = Buffer.from(\"045002\", \"hex\");\r\n        return this._runCMD(dataToSend)\r\n    }\r\n\r\n\r\n\r\n}\r\n\r\n// and try to use it\r\n\r\n\r\nlet hardware = new HardwareManager(\"/dev/ttyS0\");\r\n\r\nhardware.actionA().then(answerA => {\r\n    console.log(\"A answer is ->> \", answerA);\r\n    hardware.actionB(\"1\").then(answerB => {\r\n        console.log(\"B answer is ->> \", answerB);\r\n    })\r\n})\r\n\r\n\r\n```\r\n\r\nIt's work but I'm not sure is it correct, because I can't control port.on('data') handler, I'm afraid about memory leek after couple of thousands running methods actionA and ActionB\r\n\r\n\r\nP.S.\r\nMy device should send me after request one response as\r\n <Buffer  01 50 02 45 30 33 30 33 31 31 30 30 03 15>\r\n\r\nbut I got 2 response  <Buffer 01 50 02 45 30 33 30 33> and <Buffer 31 31 30 30 03 15>\r\nHow can I fix it too ?"}}
{"actor":{"display_login":"stale"}}