baud v0.4.0 Baud

Serial port module.

  alias Baud.Enum
  ["COM1", "ttyUSB0", "cu.usbserial-FTVFV143"] = Enum.list()
  #Do not prepend /dev/ to the port name
  #Try this with a loopback
  {:ok, pid} = Baud.start_link([portname: "cu.usbserial-FTYHQD9MA"])
  #Send data
  :ok = Baud.write(pid, "Hello");
  #Wait data is transmitted
  :ok = Baud.wait4tx(pid, 400)
  #Wait at least 5 bytes are available
  :ok = Baud.wait4rx(pid, 5, 400)
  #Check at least 5 bytes are available
  {:ok, 5} = Baud.available(pid);
  #Read 4 bytes of data
  {:ok, "Hell"} = Baud.read(pid, 4, 400);
  #Read all available data
  {:ok, "o"} = Baud.read(pid);
  #Send more data
  :ok = Baud.write(pid, "World!\n...");
  #Wait at least 1 byte is available
  :ok = Baud.wait4rx(pid, 1, 400)
  #Read all data up to first newline
  {:ok, "World!\n"} = Baud.readln(pid, 400);
  #Discard the trailing ...
  :ok = Baud.discard(pid)
  #Check nothing is available
  {:ok, 0} = Baud.available(pid);
  #Check the native port is responding
  :ok = Baud.echo(pid)
  #Close the native serial port
  :ok = Baud.close(pid)
  #Stop the server
  :ok = Baud.stop(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

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.

cmd is one of:

  • {:rc, slave, address, count} read count coils.
  • {:ri, slave, address, count} read count inputs.
  • {:rhr, slave, address, count} read count holding registers.
  • {:rir, slave, address, count} read count input registers.
  • {:fc, slave, address, value} force single coil.
  • {:phr, slave, address, value} preset single holding register.
  • {:fc, slave, address, values} force multiple coils.
  • {:phr, slave, address, values} preset multiple holding registers.

Returns :ok | {:ok, [values]}.

Example:

{:ok, pid} = Baud.start_link([portname: "cu.usbserial-FTVFV143", baudrate: 57600])
#force 0 to coil at slave 1 address 3000
:ok = Baud.rtu(pid, {:fc, 1, 3000, 0}, 400)
#read 0 from coil at slave 1 address 3000
{:ok, [0]} = Baud.rtu(pid, {:rc, 1, 3000, 1}, 400)
#force 10 to coils at slave 1 address 3000 to 3001
:ok = Baud.rtu(pid, {:fc, 1, 3000, [1, 0]}, 400)
#read 10 from coils at slave 1 address 3000 to 3001
{:ok, [1, 0]} = Baud.rtu(pid, {:rc, 1, 3000, 2}, 400)
#preset 55AA to holding register at slave 1 address 3300
:ok = Baud.rtu(pid, {:phr, 1, 3300, 0x55AA}, 400)
#read 55AA from holding register at slave 1 address 3300 to 3301
{:ok, [0x55AA]} = Baud.rtu(pid, {:rhr, 1, 3300, 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.

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.