docs » hs.serial

Communicate with external devices through a serial port (most commonly RS-232).

Powered by ORSSerialPort. Thrown together by @latenitefilms.

Copyright (c) 2011-2012 Andrew R. Madsen (andrew@openreelsoftware.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

API Overview

API Documentation

Functions

availablePortNames
Signature hs.serial.availablePortNames() -> table
Type Function
Description

Returns a table of currently connected serial ports names.

Parameters:

  • None

Returns:

  • A table containing the names of any connected serial port names as strings.
availablePortPaths
Signature hs.serial.availablePortPaths() -> table
Type Function
Description

Returns a table of currently connected serial ports paths.

Parameters:

  • None

Returns:

  • A table containing the names of any connected serial port paths as strings.
deviceCallback
Signature hs.serial.deviceCallback(callbackFn) -> none
Type Function
Description

A callback that's triggered when a serial port is added or removed from the system.

Parameters:

  • callbackFn - the callback function to trigger.

Returns:

  • None

Notes:

  • The callback function should expect 1 argument and should not return anything:
    • devices - A table containing the names of any serial ports connected as strings.

Constructors

newFromName
Signature hs.serial.newFromName(portName) -> serialPortObject
Type Constructor
Description

Creates a new hs.serial object using the port name.

Parameters:

  • portName - A string containing the port name.

Returns:

  • An hs.serial object or nil if an error occured.

Notes:

  • A valid port name can be found by checking hs.serial.availablePortNames().
newFromPath
Signature hs.serial.newFromPath(path) -> serialPortObject
Type Constructor
Description

Creates a new hs.serial object using a path.

Parameters:

  • path - A string containing the path (i.e. "/dev/cu.usbserial").

Returns:

  • An hs.serial object or nil if an error occured.

Notes:

  • A valid port name can be found by checking hs.serial.availablePortPaths().

Methods

baudRate
Signature hs.serial:baudRate([value], [allowNonStandardBaudRates]) -> number | serialPortObject
Type Method
Description

Gets or sets the baud rate for the serial port.

Parameters:

  • value - An optional number to set the baud rate.
  • [allowNonStandardBaudRates] - An optional boolean to enable non-standard baud rates. Defaults to false.

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns the baud rate as a number

Notes:

  • This function supports the following standard baud rates as numbers: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400.
  • If no baud rate is supplied, it defaults to 115200.
callback
Signature hs.serial:callback(callbackFn | nil) -> serialPortObject
Type Method
Description

Sets or removes a callback function for the hs.serial object.

Parameters:

  • callbackFn - a function to set as the callback for this hs.serial object. If the value provided is nil, any currently existing callback function is removed.

Returns:

  • The hs.serial object

Notes:

  • The callback function should expect 4 arguments and should not return anything:
    • serialPortObject - The serial port object that triggered the callback.
    • callbackType - A string containing "opened", "closed", "received", "removed" or "error".
    • message - If the callbackType is "received", then this will be the data received as a string. If the callbackType is "error", this will be the error message as a string.
    • hexadecimalString - If the callbackType is "received", then this will be the data received as a hexadecimal string.
close
Signature hs.serial:close() -> serialPortObject
Type Method
Description

Closes the serial port.

Parameters:

  • None

Returns:

  • The hs.serial object.
dataBits
Signature hs.serial:dataBits([value]) -> number | serialPortObject
Type Method
Description

Gets or sets the number of data bits for the serial port.

Parameters:

  • value - An optional number to set the number of data bits. It can be 5 to 8.

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns the data bits as a number.
  • The default value is 8.
isOpen
Signature hs.serial:isOpen() -> boolean
Type Method
Description

Gets whether or not a serial port is open.

Parameters:

  • None

Returns:

  • true if open, otherwise false.
name
Signature hs.serial:name() -> string
Type Method
Description

Returns the name of a hs.serial object.

Parameters:

  • None

Returns:

  • The name as a string.
open
Signature hs.serial:open() -> serialPortObject | nil
Type Method
Description

Opens the serial port.

Parameters:

  • None

Returns:

  • The hs.serial object or nil if the port could not be opened.
parity
Signature hs.serial:parity([value]) -> string | serialPortObject
Type Method
Description

Gets or sets the parity for the serial port.

Parameters:

  • value - An optional string to set the parity. It can be "none", "odd" or "even".

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns a string value of "none", "odd" or "even".
path
Signature hs.serial:path() -> string
Type Method
Description

Returns the path of a hs.serial object.

Parameters:

  • None

Returns:

  • The path as a string.
sendData
Signature hs.serial:sendData(value) -> none
Type Method
Description

Sends data via a serial port.

Parameters:

  • value - A string of data to send.

Returns:

  • None
shouldEchoReceivedData
Signature hs.serial:shouldEchoReceivedData([value]) -> boolean | serialPortObject
Type Method
Description

Gets or sets whether the port should echo received data.

Parameters:

  • value - An optional boolean.

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
  • The default value is false.
stopBits
Signature hs.serial:stopBits([value]) -> number | serialPortObject
Type Method
Description

Gets or sets the number of stop bits for the serial port.

Parameters:

  • value - An optional number to set the number of stop bits. It can be 1 or 2.

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns the number of stop bits as a number.
  • The default value is 1.
usesDTRDSRFlowControl
Signature hs.serial:usesDTRDSRFlowControl([value]) -> boolean | serialPortObject
Type Method
Description

Gets or sets whether the port should use DTR/DSR Flow Control.

Parameters:

  • value - An optional boolean.

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
  • The default value is false.
usesRTSCTSFlowControl
Signature hs.serial:usesRTSCTSFlowControl([value]) -> boolean | serialPortObject
Type Method
Description

Gets or sets whether the port should use RTS/CTS Flow Control.

Parameters:

  • value - An optional boolean.

Returns:

  • If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
  • The default value is false.