Skip to content

TetsuakiBaba/p5.serial

Repository files navigation

p5.serial

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.

CDN

<script src="https://cdn.jsdelivr.net/gh/TetsuakiBaba/p5.serial/p5.serial.js" type="text/javascript"></script>

Getting Started

from scratch

p5.serial.getting.started.scratch.mp4

p5.js editor

Examples

Please check README on getting_started for more example codes.

API

Serial class

Tip

Please check API document page for more detailed.

Serial()

Create a new Serial object.

let serial = new Serial();

Serial.begin(baudrate=9600)

Open the serial port.

serial.begin();

Serial.close()

Close the serial port.

serial.close();

Serial.gotByte()

This function is called when a byte is received.

serial.gotByte = function(value) {
    console.log(value);
}

Serial.gotBytes()

This function is called when bytes are received.

serial.gotBytes = function(values) {
    console.log(values);
}

Serial.gotCSV()

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
    }
}

Serial.writeByte()

Write a byte to the serial port.

serial.writeByte(0x01);

Serial.writeBytes()

Write bytes to the serial port.

serial.writeBytes([0x01, 0x02, 0x03]);

Serial.writeCSV()

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"

for Developers

How to update API documentation

npm run generate-docs

About

Web Serial class for p5.js

Resources

License

Stars

Watchers

Forks