p5.serial is a simple serial communication library for p5.js, which is using web serial on the backend, and designed to be easy to use and to work with the p5.js library.
<script src="https://cdn.jsdelivr.net/gh/TetsuakiBaba/p5.serial/p5.serial.js" type="text/javascript"></script>
p5.serial.getting.started.scratch.mp4
- Single Byte receive(gotByte): https://editor.p5js.org/tetsuakibaba/sketches/EgLpDNrFq
- Multiple Bytes receive(gotCSV): https://editor.p5js.org/tetsuakibaba/sketches/bMXPV-gvu
Please check README on getting_started for more example codes.
Tip
Please check API document page for more detailed.
Create a new Serial object.
let serial = new Serial();
Open the serial port.
serial.begin();
Close the serial port.
serial.close();
This function is called when a byte is received.
serial.gotByte = function(value) {
console.log(value);
}
This function is called when bytes are received.
serial.gotBytes = function(values) {
console.log(values);
}
This function is called when a CSV string is received. We recommend using this function when you want to receive multiple values at once.
serial.gotCSV = function(values) {
// if you send println('hello,world') from arduino, values is an array of number. ex) ['hello', 'world']
for( v of values){
console.log(v); // hello, world
}
}
Write a byte to the serial port.
serial.writeByte(0x01);
Write bytes to the serial port.
serial.writeBytes([0x01, 0x02, 0x03]);
Write a CSV string to the serial port. We recommend using this function when you want to send multiple values at once.
serial.writeCSV('hello,world'); // send arduino "hello,world\n"
npm run generate-docs