Hi, I tray to send string data from pc to arduino, Opened port and write message but it does not work :(
```js
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;
}
}
}```
I'm having the same problem.
It seems that when I use the #write function, it is not always that it writes the information on the serial port and, in case of not writing, the Error is not released.
I don't know why tho. But your scenario looks just like mine.
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
I did what you asked. I debugged and I couldn't find the reason why it is not writing. I'll leave my code along with a short output from the debug, below.
I noticed something: somethimes it does write. And I can see the debug using bindings/unixWrite, but it's rare. It seems like something related to the time that the js script is writing. I don't know.
My debug:
❯ DEBUG=serialport/binding* yarn start
yarn run v1.13.0
$ node src/index.js
serialport/bindings loading DarwinBinding +0ms
serialport/binding-abstract open +0ms
serialport/binding-abstract open +3ms
Error: Error Resource temporarily unavailable Cannot lock port
serialport/bindings/poller Creating poller +0ms
Connection with undefined opened.
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +7s
serialport/binding-abstract read +7s
serialport/bindings/unixRead Starting read +7s
serialport/bindings/unixRead Finished read 2 bytes +0ms
serialport/binding-abstract read +2ms
serialport/bindings/unixRead Starting read +2ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +4ms
serialport/bindings/poller received "readable" +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +1ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +0ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +1ms
serialport/bindings/poller Polling for "readable" +2ms
serialport/bindings/poller received "readable" +2ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +3ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +0ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +1ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +3ms
serialport/bindings/unixRead Starting read +3ms
serialport/bindings/unixRead Finished read 3 bytes +1ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +3ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +3ms
serialport/bindings/unixRead Starting read +3ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +1ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +1ms
serialport/bindings/poller Polling for "readable" +2ms
serialport/bindings/poller received "readable" +2ms
serialport/binding-abstract read +3ms
serialport/bindings/unixRead Starting read +3ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +3ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +0ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +0ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +0ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +0ms
serialport/bindings/poller received "readable" +4ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +1ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +4ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +20ms
serialport/bindings/unixRead Starting read +20ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +21ms
Recebido: {"user_Id":"jonathan","password":"test","id_sensor":123, "value": 259.90}
serialport/bindings/poller received "readable" +5s
serialport/binding-abstract read +5s
serialport/bindings/unixRead Starting read +5s
serialport/bindings/unixRead Finished read 1 bytes +0ms
serialport/binding-abstract read +1ms
serialport/bindings/unixRead Starting read +1ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +3ms
serialport/bindings/unixRead Starting read +3ms
serialport/bindings/unixRead Finished read 4 bytes +0ms
serialport/binding-abstract read +0ms
serialport/bindings/unixRead Starting read +1ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +1ms
serialport/bindings/poller received "readable" +3ms
serialport/binding-abstract read +4ms
serialport/bindings/unixRead Starting read +3ms
index.js
const Server = require('./server')
const app = new Server()
app.connect()
server.js
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const CondutivityController = require('./app/controllers/CondutivityController')
class Server {
constructor () {
this.parser = ''
this.connect()
}
async connect () {
let portName
const portList = await SerialPort.list()
const exp = new RegExp('arduino', 'gi')
portList.map(port => {
if (exp.test(port.manufacturer)) {
portName = port.comName
}
})
if (!portName) {
return
}
this.port = new SerialPort(portName, {
baudRate: 9600,
autoOpen: true
})
this.controller = new CondutivityController(this.port)
this.startParser()
this.startEvents()
this.handleErrors()
}
startParser () {
this.parser = this.port.pipe(new Readline({ delimiter: '\n' }))
}
startEvents () {
this.port.on('open', () => {
console.log(`Connection with ${process.env.PORT} opened.`)
})
this.parser.on('data', data => {
this.controller.store(data)
})
}
handleErrors () {
this.port.on('error', function (err) {
console.log('Error: ', err.message)
})
}
}
module.exports = Server
CondutivityController.js
const Authenticate = require('../utils/Authenticate')
const ArduinoWriter = require('../utils/ArduinoWriter')
class CondutivityController {
constructor (port) {
this.port = port
}
async store (incoming) {
const {
user_Id: userId,
password,
id_sensor: sensorId,
value
} = JSON.parse(incoming)
// Get Bearer token from api
const auth = await Authenticate(userId, password, sensorId)
console.log('Recebido: ', incoming)
// Create the payload that will be sent to arduino
const responseData = JSON.stringify({
min: auth.set_Point_Min,
max: auth.set_Point,
updateCycle: auth.update_Cycle_Segundos
})
// Send the payload.
ArduinoWriter(responseData, this.port)
}
}
module.exports = CondutivityController
ArduinoWriter.js
const ArduinoWriter = (message, port) => {
port.write(message, function (err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('Message written: ', message)
})
}
module.exports = ArduinoWriter
Arduino code:
#include <ArduinoJson.h>
#define syncTime 10
unsigned long delayMillis = 5000;
unsigned long lastMillis = 0;
unsigned long lastSyncMillis = 0;
unsigned long updateCycle = 999;
unsigned long maxSetpoint = 99;
unsigned long minSetpoint = 9;
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.flush();
}
void loop()
{
long currentMillis = millis();
if (currentMillis - lastMillis > delayMillis)
{
lastMillis = currentMillis;
upload();
download();
}
}
void upload()
{
String payload = "{\"user_Id\":\"jonathan\",\"password\":\"test\",\"id_sensor\":123, \"value\": 259.90}";
Serial.println(payload);
}
void download()
{
while (!Serial.available() > 0)
{
long currentMillis = millis();
if (currentMillis - lastSyncMillis > syncTime)
{
lastSyncMillis = currentMillis;
break;
}
}
const size_t bufferSize = JSON_OBJECT_SIZE(3) + 120;
StaticJsonDocument<bufferSize> doc;
DeserializationError err = deserializeJson(doc, Serial);
if (err)
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.flush();
return;
}
updateCycle = doc["updateCycle"];
minSetpoint = doc["min"];
maxSetpoint = doc["max"];
digitalWrite(LED_BUILTIN, LOW);
}
Try passing lock: false
Try passing lock: false
-- --- Francis Gulotta [email protected]
Thank you for the replies. I think it solved my problem.
Can you explain how this lock system works and why it wasn't letting the write function work properly?
The log was was saying something about the lock failing which should cause
an error. I'm on mobile so I haven't dug in.
Your drivers, or other devices using the port may prevent the locking
behavior, but it shouldn't stop the port from opening without an error.
On Fri, Apr 26, 2019 at 1:49 PM Jonathan Galdino notifications@github.com
wrote:
Try passing lock: false
-- --- Francis Gulotta [email protected]Thank you for the replies. I think it solved my problem.
Can you explain how this lock system works and why it wasn't letting the
write function work properly?—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/node-serialport/node-serialport/issues/1826#issuecomment-487143787,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAGK3URR2E5B6XMS7K3ZATPSM6CRANCNFSM4HCI2AOQ
.
The log was was saying something about the lock failing which should cause an error. I'm on mobile so I haven't dug in. Your drivers, or other devices using the port may prevent the locking behavior, but it shouldn't stop the port from opening without an error.
Understood. Thanks, tho. Means a lot.
I created an issue for the bug you found 👌
@reconbot I'm not sure if I'm having a similar problem, it's because I'm getting this message
serialport/stream flush attempted, but port is not open +8ms
serialport/stream _read queueing _read for after open +3ms
serialport/bindings/poller Creating poller +0ms
serialport/stream opened path: /dev/ttyUSB0 +3ms
serialport/stream _read reading { start: 0, toRead: 65536 } +1ms
serialport/binding-abstract read +15ms
serialport/stream _write 15 bytes of data +1ms
serialport/binding-abstract write 15 bytes +1ms
serialport/bindings/unixWrite Starting write 15 bytes offset 0 bytesToWrite 15 +0ms
serialport/bindings/unixRead Starting read +0ms
serialport/bindings/unixWrite write returned: wrote 15 bytes +1ms
serialport/bindings/unixWrite Finished writing 15 bytes +0ms
serialport/stream binding.write write finished +2ms
serialport/stream drain +0ms
serialport/binding-abstract drain +2ms
message written
serialport/bindings/unixRead read error { [Error: EAGAIN: resource temporarily unavailable, read] errno: -11, code: 'EAGAIN', syscall: 'read' } +1ms
serialport/bindings/unixRead waiting for readable because of code: EAGAIN +0ms
serialport/bindings/poller Polling for "readable" +5ms
serialport/stream binding.drain finished +1ms
I'm just writing a message, and the first time that it writes after opening the port It's getting a response, I mean reading the message, then It writes it again and it never gets another response from the device, I'm not sure if it's about reading, writing or related to the port(open/close)
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
The link isn't working :(
Sorry I broke the links with the last site upgrade. I dropped the language support because it was mostly untranslated.
Most helpful comment
I created an issue for the bug you found 👌