baud v0.5.0 Baud

Serial port module.

  tty = case :os.type() do
    {:unix, :darwin} -> "cu.usbserial-FTYHQD9MA"
    {:unix, :linux} -> "ttyUSB0"
    {:win32, :nt} -> "COM12"
  end

  #Try this with a loopback
  {:ok, pid} = Baud.start_link([device: tty])

  Baud.write pid, "01234\n56789\n98765\n43210"
  {:ok, "01234\n"} = Baud.readln pid
  {:ok, "56789\n"} = Baud.readln pid
  {:ok, "98765\n"} = Baud.readln pid
  {:to, "43210"} = Baud.readln pid

  Baud.write pid, "01234\r56789\r98765\r43210"
  {:ok, "01234\r"} = Baud.readch pid, 0x0d
  {:ok, "56789\r"} = Baud.readch pid, 0x0d
  {:ok, "98765\r"} = Baud.readch pid, 0x0d
  {:to, "43210"} = Baud.readch pid, 0x0d

  Baud.write pid, "01234\n56789\n98765\n43210"
  {:ok, "01234\n"} = Baud.readn pid, 6
  {:ok, "56789\n"} = Baud.readn pid, 6
  {:ok, "98765\n"} = Baud.readn pid, 6
  {:to, "43210"} = Baud.readn pid, 6

  Baud.write pid, "01234\n"
  Baud.write pid, "56789\n"
  Baud.write pid, "98765\n"
  Baud.write pid, "43210"
  :timer.sleep 100
  {:ok, "01234\n56789\n98765\n43210"} = Baud.readall pid

Summary

Functions

Reads all available data

Reads until ‘ch’ is received

Reads until ‘nl’ is received

Starts the serial server

Stops the serial server

Writes data to the serial port

Functions

readall(pid, timeout \\ 400)

Reads all available data.

Returns {:ok, data}.

readch(pid, ch, timeout \\ 400)

Reads until ‘ch’ is received.

Returns {:ok, data} | {:to, partial}.

readln(pid, timeout \\ 400)

Reads until ‘nl’ is received.

Returns {:ok, line} | {:to, partial}.

readn(pid, count, timeout \\ 400)

Reads count bytes.

Returns {:ok, data} | {:to, partial}.

start_link(params, opts \\ [])

Starts the serial server.

params must contain a keyword list to be merged with the following defaults:

[
  device: nil,         #serial port name: "COM1", "ttyUSB0", "cu.usbserial-FTYHQD9MA"
  speed: 115200,       #either 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
                       #win32 adds 14400, 128000, 256000
  config: "8N1",       #either "8N1", "7E1", "7O1"
]

opts is optional and is passed verbatim to GenServer.

Returns {:ok, pid}.

Example

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

Stops the serial server.

Returns :ok.

write(pid, data, timeout \\ 400)

Writes data to the serial port.

Returns :ok.