Nerves.UART v0.0.1 Nerves.UART
Find and use UARTs, serial ports, and more.
Summary
Functions
Close the serial port. The GenServer continues to run so that a port can be opened again
Waits until all data has been transmitted. See tcdrain(3) for low level details on Linux or OSX. This is not implemented on Windows
Return a map of available ports with information about each one. The map looks like this:
%{ "ttyS0" -> %{vendor_id: 1234, product_id: 1,
manufacturer: "Acme Corporation", serial_number: "000001"},
"ttyUSB0" -> ${vendor_id: 1234, product_id: 2} }
Depending on the port and the operating system, not all fields may be returned. Informational fields are
Flushes the receive buffer. See tcflush(3) for low level details on
Linux or OSX. This calls PurgeComm on Windows
Open a serial port. Pass one or more of the following options to configure the port
Read data from the UART. This call returns data as soon as it’s available or after timing out
Send a continuous stream of zero bits for a duration in milliseconds. If the duration is zero, then zero bits are transmitted by at least 0.25 seconds, but no more than 0.5 seconds. If non-zero, then zero bits are transmitted for about that many milliseconds depending on the implementation
Start up a UART GenServer
Stop the UART GenServer
Write data to the opened UART with the default timeout
Write data to the opened UART. It’s possible for the write to return before all of the data is actually transmitted. To wait for the data, call drain/1
Functions
Close the serial port. The GenServer continues to run so that a port can be opened again.
Change the serial port configuration after open/3 has been called. See
open/3 for the valid options.
Waits until all data has been transmitted. See tcdrain(3) for low level details on Linux or OSX. This is not implemented on Windows.
Return a map of available ports with information about each one. The map looks like this:
%{ "ttyS0" -> %{vendor_id: 1234, product_id: 1,
manufacturer: "Acme Corporation", serial_number: "000001"},
"ttyUSB0" -> ${vendor_id: 1234, product_id: 2} }
Depending on the port and the operating system, not all fields may be returned. Informational fields are:
vendor_id- The 16-bit USB vendor ID of the device providing the port. Vendor ID to name lists are managed through usb.orgproduct_id- The 16-bit vendor supplied product IDmanufacturer- The manufacturer of the portdescription- A description or product nameserial_number- The device’s serial number if it has one
Flushes the receive buffer. See tcflush(3) for low level details on
Linux or OSX. This calls PurgeComm on Windows.
Open a serial port. Pass one or more of the following options to configure the port:
:active n- where n is true or false (see discussion below):speed n- n is the baudrate of the board (e.g., 115200):data_bits n- n is the number of data bits (e.g., 5, 6, 7, or 8):stop_bits n- n is the number of stop bits (e.g., 1 or 2):parity n- n is:none,:even,:odd,:space, or:mark:spacemeans that the parity bit is always 0:markmeans that the parity bit is always 1
:flow_control n- n is :none, :hardware, or :software
Active mode defaults to true and means that data received on the UART is reported in messages. The messages have the following form:
{:nerves_uart, serial_port_name, data}
or
{:nerves_uart, serial_port_name, {:error, reason}}
When in active mode, flow control can not be used to push back on the
sender and messages will accumulated in the mailbox should data arrive
fast enough. If this is an issue, set :active to false and call
read/2 manually when ready for more data.
On success, open/3 returns :ok. On error, {:error, reason} is returned.
The following are some reasons:
:enoent- the specified port couldn’t be found:eagain- the port is already open:eacces- permission was denied when opening the port
Read data from the UART. This call returns data as soon as it’s available or after timing out.
Returns {:ok, binary}, where binary is a binary data object that contains the
read data, or {:error, reason} if an error occurs.
Typical error reasons:
:ebadf- the UART is closed:einval- the UART is in active mode
Send a continuous stream of zero bits for a duration in milliseconds. If the duration is zero, then zero bits are transmitted by at least 0.25 seconds, but no more than 0.5 seconds. If non-zero, then zero bits are transmitted for about that many milliseconds depending on the implementation.
Write data to the opened UART. It’s possible for the write to return before all of the data is actually transmitted. To wait for the data, call drain/1.
This call blocks until all of the data to be written is in the operating system’s internal buffers. If you’re sending a lot of data on a slow link, supply a longer timeout to avoid timing out prematurely.
Returns :ok on success or {:error, reason} if an error occurs.
Typical error reasons:
:ebadf- the UART is closed