baud v0.2.0 Baud
Standard serial port module.
["COM1", "ttyUSB0", "cu.usbserial-FTYHQD9MA"] = Baud.Enum.list()
#Do not prepend /dev/ to the port name
{:ok, pid} = Baud.start_link([portname: "cu.usbserial-FTYHQD9MA"])
:ok = Baud.write(pid, "Hello!\n");
{:ok, "Hi!\n"} = Baud.read(pid);
{:ok, "Hi!\n"} = Baud.readln(pid, 400);
{:ok, 24} = Baud.available(pid);
:ok = Baud.wait4data(pid, 400)
:ok = Baud.discard(pid)
:ok = Baud.echo(pid)
:ok = Baud.close(pid)
Summary
Functions
Returns the number of bytes available in the serial port input buffer
Closes the serial port and waits for confirmation
Discards all data in the input and output buffers of the serial port
Tests if the native port has compleated all previous commands
Reads all available data from the serial port up to the buffer size
Reads available data returning when either \n is received,
the timeout has expired or the buffer is full. Partial lines may
be returned without warning so check for trailing \n on received data
Starts the GenServer
Stops the GenServer with :normal
Waits for the specified timeout and returns :ok as soon
as there is data available or :timeout if not
Writes data to the serial port
Functions
Returns the number of bytes available in the serial port input buffer.
Returns {:ok, number_of_bytes}.
Closes the serial port and waits for confirmation.
Stopping the GenServer will close the port stdio pipes the native port will exit upon detection of a closed stdio and the OS will release the serial port. All this happens automatically but also within an undefined time frame.
There are times when releasing the OS resources in a timely manner is required. In unit testing for example, you want the previous test to release the serial port before attemping to open it again in the next test (and fail).
Returns :ok.
Reads available data returning when either \n is received,
the timeout has expired or the buffer is full. Partial lines may
be returned without warning so check for trailing \n on received data.
Returns {:ok, line}.
Starts the GenServer.
state must contain a keyword list to be merged with the following defaults:
%{
portname: "TTY", #the port name: "COM1", "ttyUSB0", "cu.usbserial-FTYHQD9MA"
baudrate: "115200", #either 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
bitconfig: "8N1", #either 8N1, 7E1, 7O1
bufsize: 255, #the buffer size. 255 is the POSIX maximum.
packto: 0 #the packetization timeout in millis. 100 in the POSIX minimum.
}
opts is optional and is passed verbatim to GenServer.
Returns {:ok, pid}.
Example
Baud.Server.start_link([portname: "COM8"])
Waits for the specified timeout and returns :ok as soon
as there is data available or :timeout if not.
Returns :ok | :timeout.