I know it may sounds weird and Im really dont know whats going on here.
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM1", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
sp.on('open', function ()
{
console.log("writing...");
sp.write('b');
console.log("done");
});
this is my nodejs script when i run with my serial monitor open makes my led in my arduino blinks
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
now, my problem here is when I closed my serial monitor and run my nodejs script it doesnt blink my led. tho, i can see "writing..." and "done" in console.
any ideas why is this happening? or suggestion what to do? thanks in advance.
node version: 6.9.2
npm version: 3.10.9
OS: Ubuntu 14.04 LTS
I've read that the arduino cannot immediately receive a data after opening of port. Or something like that. So, ive tried this syntax and it perfectly works.
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
sp.on('open', function() {
sp.on('data', function (data) {
sp.write("b");
});
});
Related
First off, I'm new to both node.js and the concept of async functions and would greatly appreciate any help I can get here.
I've been trying to write a script using the node.js serialport module to scan the ports on my Windows machine and do simple stuff once an Arduino Micro is connected to a port. The below works fine if the Arduino is already connected, but I can't work out how to extend it so that it will wait, indefinitely, until the Micro is plugged in. It just terminates if nothing is found.
const serialPort = require('serialport');
var portName;
serialPort.list().then(function(ports){
ports.forEach(function(portInfo){
if (portInfo.vendorId == 2341 && portInfo.productId == 8037) {
portName = portInfo.path;
var myPort = new serialPort(portName);
myPort.on('open' , function() {
showPortOpen();
myPort.write('RL'); // command to initiate functions in Arduino code
});
myPort.on('data' , readSerialData); // echo data from Arduino
myPort.on('close', showPortClose);
}
})
});
function readSerialData(data) {
console.log(data);
}
function showPortOpen() {
console.log(portName,'opened');
}
function showPortClose() {
console.log(portName,'closed');
}
Problem solved in double-quick time. Thank you :-)
Not sure if it's the cleanest approach but by re-calling the setInterval function when the port closes, I have a script that waits and finds the Arduino once it's plugged into the USB and, if subsequently unplugged, will find it again once it's plugged in again. Just what I want!
const serialPort = require('serialport');
var portName;
loop(); // start searching for Arduino on a port
function loop() {
loopId = setInterval(function() {
serialPort.list().then(function(ports){
ports.forEach(function(portInfo){
if (portInfo.vendorId == 2341 && portInfo.productId == 8037) {
portName = portInfo.path;
var myPort = new serialPort(portName);
myPort.on('open' , function() {
showPortOpen();
myPort.write('RL'); // command to initiate Arduino functions
});
myPort.on('data' , readSerialData); // echo data from Arduino
myPort.on('close', showPortClose);
}
})
})
}, 1000)
};
function readSerialData(data) {
console.log(data);
}
function showPortOpen() {
console.log(portName,'opened');
clearInterval(loopId); // stop looping once Arduino found on port
}
function showPortClose() {
console.log(portName,'closed');
loop(); // start over when Arduino port is closed
}
This is the code I have in node js to read from the serial data port of the arduino.
var SerialPort = require('serialport');
var io = require('socket.io').listen(3000);
var serialPort = new SerialPort("COM4", {
baudRate: 9600,
parser: new SerialPort.parsers.Readline("\n")
});
io.sockets.on('connection', function(socket){
socket.on('message', function(msg){
console.log(msg);
});
socket.on('disconnected', function(){
console.log('disconnected');
});
});
var clearData = "";
var readData = "";
serialPort.on('open',function(){
console.log('open');
serialPort.on('data', function(data){
console.log(data);
readData += data.toString();
io.sockets.emit('message',data);
});
});
This is the code that I have in Arduino, it's just a short example.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Hello");
delay(2000);
}
However, in the console of node js the following is shown when printing the data:
How can I get the data correctly in one line?
To have something like this:
Hello
Hello
Hello
I cannot test this end to end (no arduino anywhere near), but since serialport seems to implement a Readable stream, I'd try using scramjet like this:
const {StringStream} = require('scramjet');
serialport.on('open', () => console.log('open');
serialport.pipe(new StringStream) // pipe the stream to scramjet StringStream
.lines('\n') // split per line
.each( // send message per every line
data => io.sockets.emit('message',data)
);
Scramjet would sort the readline issue for you.
I tested this code with the serial port device that uses RS-232 protocol. Its advantage is that you don't need install the third package from npm.
var recVal = '';
mySerial.on('data', function(data) {
if(data.indexOf('\n') != -1) {
io.emit('serial:data', {
value: recVal
});
console.log("Data: ", recVal.toString());
recVal = '';
} else {
recVal = recVal.concat(data);
}
});
I am trying to rink Raspberry Pi and Arduino by serial communication. My purpose is that the user controls a LED of Arduino from Raspberry Pi.
I found an example code of serial communication and it sends a String to Arduino automatically every 2sec. But I want to make two things:
Change the value sent instead of 'hello'.
And the user can send the value any time he wants, not automatically.
Can you help me please? I am not good on node.js.
var SerialPort = require("serialport")
var serialPort = new SerialPort('/dev/ttyACM0',
{ baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) { // 아두이노로부터 전달된 데이터
console.log('data received: ' + data);
});
serialPort.write("Hello from Raspberry Pi\n", function(err, results) {
console.log('err ' + err);
console.log('results ' + results); //전송한 바이트 수
});
setInterval(
function() { // 2초마다 아두이노에게 문자열을 전송하는 예
serialPort.write('hello');
}, 2000);
});
This is not that far off from working. A few minor tweaks
1. 'baudrate' should be mixed-caps 'baudRate'.
2. For anyone running this code, you do, of course, need to
find the device name (the first parameter to the Serial Port constructor,
in the example above '/dev/ttyACM0'). One way to find this is to
open the Arduino IDE and look at 'Tools' | 'Port' once you find the
one that communicates with the Arduino.
3. Lastly, the code above confuses by writing in two places. Just write in
the setInterval function. This sends the 'hello' string every 2 seconds.
Here is the code that worked for me:
var SerialPort = require("serialport")
var serialPort = new SerialPort('/dev/cu.usbmodem15',
{
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
serialPort.on("open", function () {
console.log('comm open');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
});
setInterval(
function() {
serialPort.write('hello');
}, 2000
);
});
I'm using nodejs "v0.10.28" and npm "1.4.9", downloaded from http://nodejs.org.
I've installed serialport through npm
npm install serialport
My program is running on a Raspberry-pi and needs to keep sending updated values quickly through serialport, so I've created this test program.
var serialPort = require("serialport");
var SerialPort = require("serialport").SerialPort;
var sp = new SerialPort("/dev/ttyAMA0", {
baudrate: 9600
}, false);
console.log("Starting up serial host...");
var message = "Num: ";
var counter = 1000000;
function write()
{
message= counter.toString();
counter+=1;
sp.open(function(err)
{
console.log("Writing serial data: " + message);
sp.write(message, function(err, res)
{
if (err)
{
console.log(err);
}
sp.close();
});
});
}
setTimeout(write, 10); //wait 10 ms for everything to initialize correctly
setInterval(write, 50); //write data every 50 ms
The program runs ok for exactly 1012 writes, then crashes.
Program output towards the end:
...
Writing serial data: 1001011
Writing serial data: 1001012
Writing serial data: 1001013
[Error: Serialport not open.]
events.js:72
throw er; // Unhandled 'error' event
^
Error: Serialport not open.
at SerialPortFactory.SerialPort.close (/home/pi/node_modules/serialport/serialport.js:476:17)
at /home/pi/serialTest.js:25:7
at SerialPortFactory.SerialPort.write (/home/pi/node_modules/serialport/serialport.js:290:9)
at /home/pi/serialTest.js:19:13
at /home/pi/node_modules/serialport/serialport.js:224:11
Why is it crashing?
Is there any buffer overflow in memory somewhere?
And why exactly 1012 writes? Very close to 1024, is it a coincidence?
A friend just helped with answering this privately so I'll share findings here.
Code:
var SerialPort = require("serialport").SerialPort;
var sp = new SerialPort("/dev/ttyAMA0", {
baudrate: 9600
}, false);
console.log("Starting up serial host...");
var message = "Num: ";
var counter = 1000000;
sp.open(function(err) {
if (err) {
console.log("Port open error: ", err);
}
else {
console.log("Port opened!");
}
});
function write()
{
if (sp.isOpen()) {
message= counter.toString();
counter+=1;
console.log("Writing serial data: " + message);
sp.write(message, function(err, res)
{
if (err)
{
console.log(err);
}
setTimeout(write, 50);
});
}
else {
setTimeout(write, 50);
}
}
setTimeout(write, 10); //wait 10 ms for everything to initialize correctly
If you look closely you'll notice that now sp.open() is used once and in the beginning of write function sp.isOpen() is used to check if port is open.
Also setInterval(write, 50); is gone and now only use setTimeout(write, 10);
Code now works, but that still doesn't answer the question why did it use to crash when opening and closing the port in the previous code.
I'm writing an npm module to interface with a piLite with node.js. I'd like to write it properly using TDD principles.
The code I need to test:
var SerialPort = require("serialport").SerialPort;
exports.PiLite = {
device: "/dev/ttyAMA0",
baudrate: 9600,
client: null,
init: function() {
this.client = new SerialPort(this.device, {
baudrate: this.baudrate
}, false);
},
connect: function(callback) {
this.init();
this.client.open(function() {
console.log('Connected to Pi Lite');
callback();
});
},
write: function (data) {
...
The standard usage would be:
var pilite = require('pilite').PiLite;
pilite.connect(function() {
pilite.write('some data');
// calls to functions to send messages to pilite
}
I understand how to test assertions but I don't see how I can test the connectivity to the serial port.
Should I test for it or just test the functions I'm using to write to the serial port?
Edit: I'm pretty new to Nodeunit so any pointers in the right direction would be great.