Replies: 1 comment 2 replies
-
I fixed the stacked answer/same answer by removing the interval and relying entirely on async/await. But that still left me with a memory leak error because of the constant creation of eventemitters, by placing parser.removeListener('data', () => {}) i got rid of the eventemitter as soon as i receive the packet, i thought this was not going to work because it kept giving me errors of a new eventemitter being created, turns out i tried the same code in the next day and everything worked, it seemed the gargabe collector waited a bit to do it's thing and kill the eventemitters not being used. Now that everything works the main question still stands, is this a good way to manage a response/request comm? import SerialPort from 'serialport'
import InterByteTimeout from '@serialport/parser-inter-byte-timeout'
const port = new SerialPort('/dev/ttyACM0', {baudRate: 2400})
const parser = port.pipe(new InterByteTimeout({interval: 50}))
port.once('error', (err) => {
console.log(err);
});
const sendSync = (port, packet) => {
return new Promise((resolve, reject) => {
port.write(packet);
try {
parser.once('data', (data) => {
parser.removeListener('data', () => {})
resolve(data.toString());
});
} catch (err) {
reject(err)
}
});
}
const loopSec = async (index) => {
const typ = await sendSync(port, '^P003TYP')
const nom = await sendSync(port, '^P003NOM')
const bt1 = await sendSync(port, '^P003BT1')
console.log(typ)
console.log(nom)
console.log(bt1)
if (index < 10) {
console.log(index)
loopSec(index + 1)
}
};
loopSec(0) ^D0010
^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095
^D021136,136,,,,,,,,,,,,,,
0
^D0010
^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095
^D021136,136,,,,,,,,,,,,,,
1
^D0010
^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095
^D021136,136,,,,,,,,,,,,,,
2
^D0010
^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095
^D021136,136,,,,,,,,,,,,,,
3
.
.
. |
Beta Was this translation helpful? Give feedback.
-
Hello, i'm developing a project where i have a communication protocol divided in "packages" and i have to request the package and get the response using the serial port. I made a version in Python and everything went fine, but i'm having a really bad time trying to implement it in javascript. I checked some of the recommended code in stackoverflow and tried to mount up a scratch to discuss what my difficulties are. I'm gonna apologize in advance for i have minimal knowledge of working with Streams/EventEmitters/AsyncIterators and i'm learning as i go.
Main objective:
'^P003NOM'
.^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095
let response = getNOM('^P003NOM')
.That's all.
Problems in implementation so far:
^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095
, i would get something like this:^D0010^D054120,600,120,600,01400,00840,0,2,096,144,01012015,01095^D021136,136,,,,,,,,,,,,,,
which are the answers of multiple function calls eg getNOM(...), getBT1(...),getTYP(...).What do i want with this discussion?
To understand how to work with serialport to get a simple request<->response comm flow going.
The code for where the last problems listed are apparent is below:
node version: v14.17.3
serialport version: v9.2.8
Beta Was this translation helpful? Give feedback.
All reactions