Skip to content

SERIAL_WRITE

Download Flojoy Studio to try this app
Write a custom string to the selected serial device. Params: device : SerialDevice Defines the communication port on which the serial device is connected. write : str The string to write to the serial device. encoding : select Which string encoding method to use. Returns: out : String The input string.
Python Code
from typing import Optional, Literal
from flojoy import String, SerialDevice, flojoy, DataContainer


@flojoy(inject_connection=True)
def SERIAL_WRITE(
    connection: SerialDevice,
    write: str = "",
    encoding: Literal["bytes", "utf-8", "ascii"] = "bytes",
    terminator: Literal["CR+LF", "CR", "LF", "None"] = "None",
    default: Optional[DataContainer] = None,
) -> String:
    """Write a custom string to the selected serial device.

    Parameters
    ----------
    device : SerialDevice
        Defines the communication port on which the serial device is connected.
    write : str
        The string to write to the serial device.
    encoding : select
        Which string encoding method to use.

    Returns
    -------
    String
        The input string.
    """

    ser = connection.get_handle()

    match terminator:
        case "CR+LF":
            terminator = "\r\n"
        case "CR":
            terminator = "\r"
        case "LF":
            terminator = "\n"
        case "None":
            terminator = ""

    write += terminator

    match encoding:
        case "bytes":
            ser.write(write.encode())
        case "utf-8":
            ser.write(write)
        case "ascii":
            ser.write(write.encode(encoding="ascii"))

    return String(s=write)

Find this Flojoy Block on GitHub

Videos

Arduino Temperature Sensor

Example App

Having problems with this example app? Join our Discord community and we will help you out!
React Flow mini map

In this example, serial commands are sent to an Arduino.

First the serial port must be opened with OPEN_SERIAL. 2 TIMER blocks were also added to add delays between the commands.

Three WRITE_SERIAL blocks are used to send commands to the Arduino. The first sends a command to turn on a red LED. The second turns off the red LED and turns a green LED on. The last block turns the green LED off.