Serial library

Index:

Functions:

serial-close

serial-close

Synopsis
Closes a serial port.
Usage
(serial-close port)
Returns
closed
Where
  • port is string: the port to close.
  • closed is boolean: true if port was closed sucessfully or false otherwise.
Description
Closes the specified serial port.
Examples
(serial-close "tty.Bluetooth-Incoming-Port")
true
serial-open

serial-open

Synopsis
Opens a serial port.
Usage
(serial-open
  port
  "baud-rate" => baud_rate
  "data-bits" => data_bits
  "stop-bits" => stop_bits
  "parity" => parity
)
Returns
opened
Where
  • port is string: the port name, any of the values returned by (serial-ports).
  • baud_rate is number[0..1]: the baud rate.
  • data_bits is number[0..1]: the data bits, 7 or 8.
  • stop_bits is number[0..1]: the stop bits, 1, 2 (1.5 stop bits) or 3 (2 stop bits).
  • parity is string[0..1]: the parity, any of the following: "none", "even", "odd", "mark" or "space".
  • opened is boolean: true if port was opened sucessfully or false otherwise.
Description
Opens the given serial port with the specified parameters.
Examples
(serial-open
  "tty.Bluetooth-Incoming-Port"
  "baud-rate" => 115200
  "parity" => "none"
)
true
serial-ports

serial-ports

Synopsis
List the serial ports available.
Usage
(serial-ports)
Returns
(port ... port)
Where
  • port is string[0..N]: a port name.
Description
Returns a list that contains the names of the serial ports available.
Examples
(serial-ports)
(
  "cu.Bluetooth-Incoming-Port"
  "tty.Bluetooth-Incoming-Port"
)
serial-read

serial-read

Synopsis
Reads data from a serial port.
Usage
(serial-read port "mode" => mode "count" => count)
Returns
result
Where
  • port is string: the port to read from.
  • mode is string[0..1]: the read mode. It may be "bytes" or "line". By default, mode is "bytes".
  • count is number[0..1]: the number of bytes to read in "bytes" mode. This function will block until count bytes are available. If count is 0, this function will only read the number of bytes available and therefore will never block. By default, count is zero.
  • result is object: the data read, in "line" mode a string without the ending LF or CR character and in "bytes" mode a list of bytes.
Description
This function reads data from the specified serial port as a list of bytes when mode is "bytes" or as a string when mode is "line". In "line" mode, this function will block until a LF (10) or CR (13) is read. In "bytes" mode, this function will only block if count is greater than 0 and bytes available to read are less than count.
Examples
(serial-read port "mode" => "bytes")
(45 12)
(serial-read "tty.usb2" "mode" => "line")
"G123 14,78"
serial-write

serial-write

Synopsis
Writes data to a serial port.
Usage
(serial-write port data "charset" => charset)
Returns
num_sent
Where
  • port is string: the port to write to.
  • data is object: the data to write to the serial port. data may be a number, a list of numbers or a string. When data is a number, this number is sent as a byte. When data is a list of numbers they are sent as a sequence of bytes. When data is a string, this function sends the bytes of this string encoded in the specified charset.
  • charset is strint[0..1]: the charset to use for converting data to bytes when data is a string. By default, charset is "UTF-8".
  • num_sent is number: the number of bytes sent.
Description
Writes a string, a byte or a chunk or bytes to the specified serial port.
Examples
(serial-write "tty.usb1" 78)
1
(serial-write port (127 22 87 0))
4
(serial-write port "Hello")
5
Top