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
);
});
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
}
I have googled and searched alot for my issues but did not get any threat of use. I have the same issue as https://github.com/yaacov/node-modbus-serial/issues/115 but i am using a Windows 10 OS. I tried the suggested solutions but still same issue.
I think it should be simple but been stuck for days now
var path = "COM3";
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
client.connectRTUBuffered(path, { baudRate: 9600, stopbits: 1, databits: 8, parity: 'none' }, false, read());
client.setID(1);
function read() {
// read the 20 registers starting at address 0
// on device number 1.
var data = client.readHoldingRegisters(0, 20)
console.log(data)
}
With this code i get no port permission/ permission denied error.
PORT INFORMATION via Device manager port properties
BITS PER SECOND: 9600
DATA BITS: 8
PARITY: NONE
STOP BITS: 1
FLOW CONTROL: NONE
OUTPUT 1:
(node:10784) UnhandledPromiseRejectionWarning: Error: Opening COM3: Access denied
Tried to open the port manually
var x = setInterval(function () {
if (client.isOpen) {
client.setID(1);
//console.log("WORKING WORKING");
try {
client.readHoldingRegisters(0, 10, function (err, data) {
console.log(data);
});
} catch (err) {
console.log("Error Encountered: " + err)
}
} else {
console.log("ERROR ERROR ERROR");
}
}, 1000);
I have tested this code for two devices, but it works without any problems for one device, and for the other it only sends an SMS once, and gives a timeout error for the second time.
var serialportgsm = require('serialport-gsm');
var modem = serialportgsm.Modem();
var options = {
baudRate: 115200,
dataBits: 8,
stopBits: 1,
parity: 'none',
rtscts: false,
xon: false,
xoff: false,
xany: false,
autoDeleteOnReceive: true,
enableConcatenation: true,
incomingCallIndication: true,
incomingSMSIndication: true,
pin: '',
customInitCommand: '',
logger: console
};
modem.open(com, options, function (err, result) {
if (err) {
console.log("error in open modem", err);
}
if (result) {
console.log("modem open", result);
}
});
modem.on('open', function () {
modem.initializeModem(function (msg, err) {
if (err) {
console.log('Error Initializing Modem - ', err);
} else {
console.log('InitModemResponse: ', JSON.stringify(msg));
modem.setModemMode(function () {
var i = 0;
modem.sendSMS(Mobile, Message, false, function (result) {
i++;
if(i == 2){
modem.close(function () {
console.log('modem closed')
});
}
});
}, 'PDU');
}
})
});
I also tried not to close the modem after sending the first message, but it still failed to send for the second time on the same device.
Both devices are from the same company and the same model, only their versions are different.
Can someone help me ?
thanks.
Serial port gsm is build for SIM800C gsm module, however it can work well for some gsm modules but not on gsm modems
I'm trying to send sms using AT commands from huawei modem. but sometimes when using AT+CMGS it returns +CMS ERROR:302
This is my code
var SerialPort = require('serialport');
var port = new SerialPort("/dev/ttyUSB0", {
baudRate: 9600,
dataBits: 8,
parity: 'none'
});
console.log('port is now open');
port.on("open", function () {
console.log('Serial communication open');
port.write("AT");
port.write('\r');
port.on('data', function(data) {
console.log("Received data: " + data);
});
gsm_message_sending(port, "test", "+94758034032");
});
function gsm_message_sending(serial, message ,phone_no) {
serial.write("AT+CMGF=1");
serial.write('\r');
serial.write("AT+CMGS=\"" + phone_no + "\"");
serial.write('\r');
serial.write(message);
serial.write(Buffer([0x1A]));
serial.write('^z');
}
i need help to figure out what causes this error.. thanks
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.