Created
January 15, 2019 11:27
-
-
Save cotestatnt/5007da2e800ebe6e9323f20831a59949 to your computer and use it in GitHub Desktop.
node.js serialport arduino test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dialog library*/ | |
const dialog = require('dialog'); | |
/* Serial communication library*/ | |
const SerialPort = require('serialport'); | |
const Ready = require('@serialport/parser-ready') | |
const ReadLine = require('@serialport/parser-readline') | |
var arduino_com_port = 'comX'; | |
/* List all serial ports and try to connect, if Arduino, save COM path. */ | |
function findCom(){ | |
/* For each com ports, open a test connection. */ | |
SerialPort.list(function (err, com_ports) { | |
com_ports.forEach( function(port) { | |
var testSerial = new SerialPort(port.comName, { baudRate: 115200}); | |
var readyparser = testSerial.pipe(new Ready({ delimiter: 'ArduinoItsMe' })); | |
testSerial.on('open', function () { | |
console.log('try serial port', port.comName); | |
}); | |
/* If we have a "ArduinoItsMe" message as reply we find the right com port, save it and continue. */ | |
readyparser.on('ready', function() { | |
console.log('Arduino found at ', testSerial.path); | |
arduino_com_port = testSerial.path; | |
return; | |
}); | |
/* After a while, close the com port */ | |
setTimeout(function(){ | |
testSerial.close(function () { | |
console.log(port.comName, ' closed.'); | |
}); | |
}, 2000); | |
}); | |
}); | |
} | |
function begin(){ | |
/* Send data to Arduino from command line if necessary */ | |
const readline = require('readline').createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
readline.on('line', (input) => { | |
console.log('Received: ', input); | |
sendToSerial(input); | |
}); | |
/* Start working communication with Arduino */ | |
var myPort = new SerialPort(arduino_com_port, { autoOpen: false, baudRate: 115200 }); | |
myPort.open(function () { | |
console.log('Start communication with Arduino on ' + arduino_com_port + '.\nData rate: ' + myPort.baudRate); | |
const lineStream = myPort.pipe(new ReadLine()); | |
myPort.on('close', showPortClose); // called when the serial port closes | |
myPort.on('error', showError); // called when there's an error with the serial port | |
lineStream.on('data', readSerialData); // called when there's new data incoming | |
}); | |
/* Serial event functions: */ | |
function showPortClose() { | |
console.log('Serial port closed.'); | |
} | |
function showError(error) { | |
console.log('Serial port error: ' + error); | |
} | |
/* This is called from parser when new data comes into the serial port */ | |
function readSerialData(data) { | |
console.log(data); | |
var arduinoMsg = data.toLowerCase(); | |
if(arduinoMsg.indexOf('error') > -1){ | |
dialog.warn(data, 'Warning', null); | |
} | |
} | |
/* This is called from for send data to serial port */ | |
function sendToSerial(data) { | |
//console.log("Sending to serial: " + data); | |
myPort.write(data); | |
} | |
} | |
/* The script start here */ | |
console.log('\n\nSearching Arduino board...'); | |
// Search the Arduino com port | |
findCom(); | |
// Wait just a little before start communication with board | |
setTimeout(function(){ | |
// Start Arduino connection | |
if(arduino_com_port != 'comX') | |
begin(); | |
else | |
dialog.err('Arduino board not found', 'Error', null); | |
}, 3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define TEST_BUTTON 2 | |
String rxText; | |
void setup() { | |
pinMode(TEST_BUTTON, INPUT_PULLUP); | |
Serial.begin(115200); | |
delay(500); | |
/* Send string "ArduinoItsMe" in order to find the right COM port */ | |
for(byte i=0; i<3; i++){ | |
Serial.println("ArduinoItsMe"); | |
delay(200); | |
} | |
} | |
void loop() { | |
if(digitalRead(TEST_BUTTON) == LOW){ | |
String txText; | |
txText = "Something wrong. Error at time" + String(millis()) + " ms."; | |
Serial.println(txText); | |
delay(200); | |
} | |
while(Serial.available()){ | |
char inChar = Serial.read(); | |
rxText += inChar; | |
if(inChar == '\n'){ | |
Serial.print("Received: "); | |
Serial.println(rxText); | |
rxText = ""; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment