baud v0.1.0 Baud

Server module.

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

available(pid)

Returns the number of bytes available in the serial port input buffer.

Returns {:ok, number_of_bytes}.

close(pid)

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.

discard(pid)

Discards all data in the input and output buffers of the serial port.

Returns :ok.

echo(pid)

Tests if the native port has compleated all previous commands.

Returns :ok.

read(pid)

Reads all available data from the serial port up to the buffer size.

Returns :ok.

readln(pid, timeout)

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}.

start_link(state, opts \\ [])

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"])
stop(pid)

Stops the GenServer with :normal.

Returns :ok.

wait4data(pid, timeout)

Waits for the specified timeout and returns :ok as soon as there is data available or :timeout if not.

Returns :ok | :timeout.

write(pid, data)

Writes data to the serial port.

Returns :ok.