baud v0.3.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.wait4rx(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

Enables or disables native port debug output to stderr

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

Tests if the native port has compleated all previous commands

Changes the packetization timeout

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

Reads count bytes

Reads a line including its trailing \n

Sends an RTU command where cmd is formatted according to modbus package

Starts the GenServer

Stops the GenServer with :normal

Waits for at least count bytes to be available

Waits for all data to be transmitted

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.

debug(pid, debug)

Enables or disables native port debug output to stderr.

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.

packto(pid, packto)

Changes the packetization timeout.

Returns :ok.

read(pid)

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

Returns {:ok, data}.

read(pid, count, timeout)

Reads count bytes.

Returns {:ok, data}.

readln(pid, timeout)

Reads a line including its trailing \n.

Returns {:ok, line}.

rtu(pid, cmd, timeout)

Sends an RTU command where cmd is formatted according to modbus package.

Returns {:ok, response} where response is formatted according to modbus package.

Example:

#write 1 to coil at slave 2 address 3200
:ok = Baud.rtu(pid, {:wdo, 2, 3200, 1}, 400)
#write 0 to coil at slave 2 address 3200
:ok = Baud.rtu(pid, {:wdo, 2, 3200, 0}, 400)
#read 1 coil at slave 2 address 3200
{:ok, [1]} = Baud.rtu(pid, {:rdo, 2, 3200, 1}, 400)
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.
                          #use 0 for non-blocking reads
}

opts is optional and is passed verbatim to GenServer.

Returns {:ok, pid}.

Example

  Baud.start_link([portname: "COM8"])
stop(pid)

Stops the GenServer with :normal.

Returns :ok.

wait4rx(pid, count, timeout)

Waits for at least count bytes to be available.

Returns :ok.

wait4tx(pid, timeout)

Waits for all data to be transmitted.

Returns :ok.

write(pid, data)

Writes data to the serial port.

Returns :ok.