UART

public final class UART

UART is a two-wire serial communication protocol used to communicate with serial devices. The devices must agree on a common transmisson rate before communication.

  • Initialize an interface for UART communication.

    Usage Example

    // Initialize a UART interface UART0.
    let uart = UART(Id.UART0)
    

    Declaration

    Swift

    public init(_ id: IdName,
                baudRate: Int = 115200,
                dataBits: DataBits = .eightBits,
                parity: Parity = .none,
                stopBits: StopBits = .oneBit,
                readBufferLength: BufferLength = .small)

    Parameters

    id

    REQUIRED The name of the UART interface.

    baudRate

    OPTIONALThe communication speed. The default baud rate is 115200.

    dataBits

    OPTIONALThe length of the data being transmitted.

    parity

    OPTIONALThe parity bit to confirm the accuracy of the data transmission.

    stopBits

    OPTIONALThe bits reserved to stop the communication.

    readBufferLength

    OPTIONALThe length of the serial buffer to store the data.

  • Set the baud rate for communication. It should be set ahead of time to ensure the same baud rate between two devices.

    Declaration

    Swift

    public func setBaudrate(_ baudRate: Int)

    Parameters

    baudRate

    The communication speed.

  • Clear all bytes from the buffer to store the incoming data.

    Declaration

    Swift

    public func clearBuffer()
  • Return the number of received data from the serial buffer.

    Declaration

    Swift

    public func checkBufferReceived() -> Int

    Return Value

    The number of bytes received in the buffer.

  • Write a byte of data to the external device through the serial connection.

    Declaration

    Swift

    @inline(__always)
    public func write(_ byte: UInt8)

    Parameters

    byte

    One 8-bit binary data to be sent to the device.

  • Write a series of bytes to the external device through the serial connection.

    Declaration

    Swift

    @inline(__always)
    public func write(_ data: [UInt8])

    Parameters

    data

    A byte array to be sent to the device.

  • Write a string to the external device through the serial connection.

    Declaration

    Swift

    @inline(__always)
    public func write(_ string: String)

    Parameters

    string

    A string to be sent to the device.

  • Read a byte of data receiving from the external device. The maximum time for data reception is decided by the timeout value. If the data is received before the time set, the read process will end up automatically.

    -1 is set to wait until receiving the required data. 0 is set to end up the read immediately no matter whether the data is received or not. A value greater than 0 is set to wait for a certain period (in milliseconds) and then end up the read.

    Declaration

    Swift

    @inline(__always)
    public func readByte(timeout: Int = -1) -> UInt8

    Parameters

    timeout

    The max time for data reception.

    Return Value

    One 8-bit binary data read from the device.

  • Read a series of bytes receiving from the external device.The maximum time for data reception is decided by the timeout value. If the data is received before the time set, the read process will end up automatically.

    -1 is set to wait until receiving the required data. 0 is set to end up the read immediately no matter whether the data is received or not. A value greater than 0 is set to wait for a certain period (in milliseconds) and then end up the read.

    Declaration

    Swift

    @inline(__always)
    public func read(count: Int, timeout: Int = -1) -> [UInt8]

    Parameters

    timeout

    The max time for data reception.

    count

    The number of bytes to read.

    Return Value

    A byte array read from the device.

  • The parity bit is used to ensure the data transmission according to the number of logical-high bits.

    See more

    Declaration

    Swift

    public enum Parity : UInt8
  • One or two stops bits are reserved to end the communication.

    See more

    Declaration

    Swift

    public enum StopBits : UInt8
  • This indicates the length of the data being transmitted.

    See more

    Declaration

    Swift

    public enum DataBits : UInt8
  • This indicates the storage size of the serial buffer.

    See more

    Declaration

    Swift

    public enum BufferLength : Int32