Baud (baud v0.6.0)
Serial port module.
# this echo sample requires a loopback plug
tty =
case :os.type() do
{:unix, :darwin} -> "/dev/tty.usbserial-FTYHQD9MA"
{:unix, :linux} -> "/dev/ttyUSB0"
{:win32, :nt} -> "COM5"
end
{: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.readcr(pid)
{:ok, "56789\r"} = Baud.readcr(pid)
{:ok, "98765\r"} = Baud.readcr(pid)
{:to, "43210"} = Baud.readcr(pid)
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)
{:ok, ""} = Baud.readn(pid, 0)
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)
Baud.stop(pid)Uses:
Link to this section Summary
Functions
Reads all available data.
Reads until 'ch' is received.
Reads until 'cr' (0x0D) is received.
Reads until 'nl' (0x0A) is received.
Reads count bytes.
Starts the serial server.
Stops the serial server.
Writes data to the serial port.
Link to this section Functions
readall(pid)
Reads all available data.
Returns {:ok, data} | {:error, reason}.
readch(pid, ch, timeout \\ 400)
Reads until 'ch' is received.
Returns {:ok, data} | {:to, partial} | {:error, reason}.
readcr(pid, timeout \\ 400)
Reads until 'cr' (0x0D) is received.
Returns {:ok, line} | {:to, partial} | {:error, reason}.
readln(pid, timeout \\ 400)
Reads until 'nl' (0x0A) is received.
Returns {:ok, line} | {:to, partial} | {:error, reason}.
readn(pid, count, timeout \\ 400)
Reads count bytes.
Returns {:ok, data} | {:to, partial} | {:error, reason}.
start_link(opts)
Starts the serial server.
params must contain a keyword list to be merged with the following defaults:
[
device: nil, #serial port name: "COM1", "/dev/ttyUSB0", "/dev/tty.usbserial-FTYHQD9MA"
speed: 9600, #either 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
#win32 adds 14400, 128000, 256000
config: "8N1", #either "8N1", "7E1", "7O1"
]Returns {:ok, pid} | {:error, reason}.
Example
Baud.start_link(device: "/dev/ttyUSB0")
stop(pid)
Stops the serial server.
Returns :ok.
write(pid, data)
Writes data to the serial port.
Returns :ok | {:error, reason}.