Arduinoã®Serial Monitorã使ã£ã¦ãè¯ãã®ã§ãããå人çã«ã¯ãããã£ã¨Node.jsãæ¸ãã¦ãã¿ã¼ããã«ä¸ã§ç¢ºèªãããã¨ãå¤ãã§ãã
åä¿¡
ã½ã¼ã¹ã³ã¼ã
package.json
{ "name": "serial-develop", "version": "1.0.0", "main": "index.mjs", "dependencies": { "serialport": "^12.0.0" } }
index.mjs
import { SerialPort, ReadlineParser } from 'serialport'; (async () => { let path = ''; const baudRate = 9600; // Arduinoå´ã®baudRateã«åããã const manufacturer = 'Arduino'; await SerialPort.list().then((list) => { list.forEach((item) => { if ( item.manufacturer && (new RegExp(manufacturer)).test(item.manufacturer) ) { path = item.path; } }); }); if (path) { const port = new SerialPort({ path, baudRate }); const parser = new ReadlineParser(); parser.on('data', (data) => { console.log(data); // åä¿¡ããæååã表示 }); port.pipe(parser); } })();
å®è¡
node index.mjs
ããã§ãã·ãªã¢ã«éä¿¡ãã¿ã¼ããã«ä¸ã§ç¢ºèªã§ãã¾ãã
éä¿¡
ã½ã¼ã¹ã³ã¼ã
â» package.jsonã¯åä¿¡ã¨åãã§ã
index.mjs
import { SerialPort, ReadlineParser } from 'serialport'; (async () => { let path = ''; const baudRate = 9600; // Arduinoå´ã®baudRateã«åããã const manufacturer = 'Arduino'; await SerialPort.list().then((list) => { list.forEach((item) => { if ( item.manufacturer && (new RegExp(manufacturer)).test(item.manufacturer) ) { path = item.path; } }); }); if (path) { const port = new SerialPort({ path, baudRate }); port.on('open', () => { port.write(`${ process.argv[2] }\n`, (err) => { if (err) { console.error(err); } }); }); } })();
å®è¡
å®è¡
node index.mjs Ya-Ha-!
ããã§ãArduinoã« Ya-Ha-!\n ã¨ããæååãéä¿¡ã§ãã¾ãã
éåä¿¡
ã¾ã¨ããã¨ãä¸è¨ã®ã³ã¼ãã§éä¿¡ã¨åä¿¡ã確èªã§ãã¾ãï¼
import { SerialPort, ReadlineParser } from 'serialport'; (async () => { let path = ''; const baudRate = 9600; // Arduinoå´ã®baudRateã«åããã const manufacturer = 'Arduino'; await SerialPort.list().then((list) => { list.forEach((item) => { if ( item.manufacturer && (new RegExp(manufacturer)).test(item.manufacturer) ) { path = item.path; } }); }); if (path) { const port = new SerialPort({ path, baudRate }); parser.on('data', (data) => { console.log(data); // åä¿¡ããæååã表示 }); port.pipe(parser); port.on('open', () => { port.write(`${ process.argv[2] }\n`, (err) => { if (err) { console.error(err); } }); }); } })();