Baud (baud v0.5.6)
Serial port module.
tty = case :os.type() do
{:unix, :darwin} -> "/dev/tty.usbserial-FTYHQD9MA"
{:unix, :linux} -> "/dev/ttyUSB0"
{:win32, :nt} -> "COM5"
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
Link to this section Summary
Functions
Reads all available data.
Reads until 'ch' is received.
Reads until 'nl' is received.
Reads count bytes.
Starts the serial server.
Stops the serial server.
Writes data to the serial port.
Link to this section Functions
Link to this function
readall(pid, timeout \\ 400)
Reads all available data.
Returns {:ok, data}.
Link to this function
readch(pid, ch, timeout \\ 400)
Reads until 'ch' is received.
Returns {:ok, data} | {:to, partial}.
Link to this function
readln(pid, timeout \\ 400)
Reads until 'nl' is received.
Returns {:ok, line} | {:to, partial}.
Link to this function
readn(pid, count, timeout \\ 400)
Reads count bytes.
Returns {:ok, data} | {:to, partial}.
Link to this function
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"
]opts is optional and is passed verbatim to GenServer.
Returns {:ok, pid}.
Example
Baud.start_link(device: "COM8")
Link to this function
stop(pid)
Stops the serial server.
Returns :ok.
Link to this function
write(pid, data, timeout \\ 400)
Writes data to the serial port.
Returns :ok.