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
Returns the number of bytes available in the serial port input buffer.
Returns {:ok, number_of_bytes}.
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.
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)
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"])