diff --git a/docs/reference/serial.md b/docs/reference/serial.md index ad8820e5..f261cdd3 100644 --- a/docs/reference/serial.md +++ b/docs/reference/serial.md @@ -10,10 +10,17 @@ serial.writeString(""); serial.readUntil(","); serial.readLine(); serial.readString(); -serial.redirect(SerialPin.P0, SerialPin.P0, BaudRate.BaudRate115200); serial.onDataReceived(",", () => {}) ``` +### Advanced + +```cards +serial.redirect(SerialPin.P0, SerialPin.P0, BaudRate.BaudRate115200); +serial.writeBuffer(pins.createBuffer(0)); +serial.readBuffer(64); +``` + ### See Also [writeLine](/reference/serial/write-line), [writeNumber](/reference/serial/write-number), [writeValue](/reference/serial/write-value), [writeString](/reference/serial/write-string), [readLine](/reference/serial/read-line), [redirect](/reference/serial/redirect-to) diff --git a/docs/reference/serial/read-buffer.md b/docs/reference/serial/read-buffer.md new file mode 100644 index 00000000..47b20237 --- /dev/null +++ b/docs/reference/serial/read-buffer.md @@ -0,0 +1,12 @@ +# Serial Read Buffer + +Read the buffered serial data as a buffer + +```sig +serial.readBuffer(64); +``` + +### Returns + +* a Buffer containing input from the serial port. Empty if no data available. + diff --git a/docs/reference/serial/write-buffer.md b/docs/reference/serial/write-buffer.md new file mode 100644 index 00000000..cd8444db --- /dev/null +++ b/docs/reference/serial/write-buffer.md @@ -0,0 +1,11 @@ +# Serial Write Buffer + +Write a buffer to the [serial](/device/serial) port. + +```sig +serial.writeBuffer(pins.createBuffer(0)); +``` + +### Parameters + +* `buffer` is the buffer to write to the serial port diff --git a/libs/core/serial.cpp b/libs/core/serial.cpp index 4d9a7894..76f0a3b3 100644 --- a/libs/core/serial.cpp +++ b/libs/core/serial.cpp @@ -1,5 +1,7 @@ #include "pxt.h" +#define MICROBIT_SERIAL_READ_BUFFER_LENGTH 64 + enum SerialPin { P0 = MICROBIT_ID_IO_P0, P1 = MICROBIT_ID_IO_P1, @@ -101,9 +103,39 @@ namespace serial { //% weight=87 //% blockId=serial_writestring block="serial|write string %text" void writeString(StringData *text) { + if (!text) return; + uBit.serial.send(ManagedString(text)); } + /** + * Sends a buffer through Serial connection + */ + //% help=serial/write-buffer advanced=true weight=6 + void writeBuffer(Buffer buffer) { + if (!buffer) return; + + ManagedBuffer buf(buffer); + uBit.serial.send(buf.getBytes(), buf.length()); + } + + /** + * Reads multiple characters from the rxBuff and fills a user buffer. + * @param length default buffer length, eg: 64 + */ + //% help=serial/read-buffer advanced=true weight=5 + Buffer readBuffer(int length) { + if (length <= 0) + length = MICROBIT_SERIAL_READ_BUFFER_LENGTH; + + ManagedBuffer buf(length); + int read = uBit.serial.read(buf.getBytes(), buf.length()); + if (read != buf.length()) + buf = buf.slice(read); + + return buf.leakData(); + } + /** * Dynamically configuring the serial instance to use pins other than USBTX and USBRX. * @param tx the new transmission pins, eg: SerialPin.P0 diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index eca140b2..4de872b6 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -714,6 +714,19 @@ declare namespace serial { //% blockId=serial_writestring block="serial|write string %text" shim=serial::writeString function writeString(text: string): void; + /** + * Sends a buffer through Serial connection + */ + //% help=serial/write-buffer advanced=true weight=6 shim=serial::writeBuffer + function writeBuffer(buffer: Buffer): void; + + /** + * Reads multiple characters from the rxBuff and fills a user buffer. + * @param length default buffer length, eg: 64 + */ + //% help=serial/read-buffer advanced=true weight=5 shim=serial::readBuffer + function readBuffer(length: number): Buffer; + /** * Dynamically configuring the serial instance to use pins other than USBTX and USBRX. * @param tx the new transmission pins, eg: SerialPin.P0