node-serialport: SerialPort write not working
Hi, I tray to send string data from pc to arduino, Opened port and write message but it does not work đŚ
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const port = new SerialPort('COM7', {baudRate: 9600});
const parser = new Readline({delimiter: '\n'});
port.pipe(parser);
SerialPort.list((error, ports) => {
if (ports) console.log(`connected to ${ports[1].manufacturer}`);
});
port.on('open', () => console.log(`port open. Data rate: ${port.baudRate}`));
port.write('0\n', (err) => {
if (err) return console.log('Error on write: ', err.message);
console.log('message written')
});```
// Code
```void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available()){
switch(Serial.read()){
case '0': digitalWrite(LED_BUILTIN, LOW); break;
case '1': digitalWrite(LED_BUILTIN, HIGH); break;
default: break;
}
}
}```
- SerialPort@ 7.1.4
- Node.js 10.4.2
- Windows
- Hardware and chipset Arduino Mega 2560
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 16 (5 by maintainers)
Just to expand on the on the above
Simple Node App
Arduino Code
Debug with 2000ms delay (Working)
Debug with 1000ms delay (Not Working)
This is the only way I can get my application to send successfully too, a somewhat arbitrary delay of 2000ms. Much lower and it stops working.
@reconbot presumably this is not the intended behaviour and
.on('open')
should be sufficient to have a usable port?Edit: apparently it is entirely expected: https://serialport.io/docs/api-stream#serialportwrite
I created an issue for the bug you found đ
Itâs usually wrong baudrates if you donât get any errors. You can try debug mode if you want to see whatâs going on under the hood. https://serialport.io/docs/en/guide-debugging