View Source serctl (srly v0.6.5)
Link to this section Summary
Functions
Set the input speed of a serial device
Set the input speed of the serial device.
Explicitly close a serial device
Map of atoms representing terminal attribute constants to integers
Get/set serial device flow control
Returns the file descriptor associated with the NIF resource
Returns whether a flag is enabled
Perform operations controlling a serial device
return the input speed of a serial device
Enable raw mode
Open a serial device
return the output speed of a serial device
Read from a serial device
Read the specified number of bytes from a serial device
Returns an Erlang termios record used for setting the attributes of a serial device
discards data written but not transmitted or received but not read
Get the terminal attributes of a serial device
Sets the terminal attributes of a serial device
Write data to a serial device
Link to this section Types
-type dev() :: iodata() | {fd, integer()}.
-type errno() :: {error, file:posix()}.
-type fd() :: any().
-type termios() :: #termios{} | binary().
Link to this section Functions
-spec cfsetispeed(termios(), atom() | integer()) -> binary().
Set the input speed of a serial device
See the warning for tcsetattr/2.
Failure: badarg if Speed is an invalid atom.-spec cfsetospeed(termios(), atom() | integer()) -> binary().
Set the input speed of the serial device.
See the warning for tcsetattr/2.
Failure: badarg if Speed is an invalid atom.Explicitly close a serial device
The device is automatically closed if the process holding open the serial device exits.-spec constant() -> proplists:proplist().
Map of atoms representing terminal attribute constants to integers
Varies across platforms.-spec constant(atom()) -> integer() | undefined.
-spec flow(<<_:64, _:_*8>> | #termios{}) -> boolean().
Get/set serial device flow control
flow/1 indicates whether flow control is enabled in a serial device's terminal attributes. flow/2 returns a termios structure that can be used for setting a serial device's flow control.-spec flow(<<_:64, _:_*8>> | #termios{}, boolean()) -> #termios{}.
-spec getfd(fd()) -> integer().
Returns the file descriptor associated with the NIF resource
The file descriptor can be used with erlang:open_port/2.-spec getflag(<<_:64, _:_*8>> | #termios{}, cflag | iflag | lflag | oflag, atom()) -> boolean().
Returns whether a flag is enabled
Opt is one of the atoms returned using serctl:constant/0.Perform operations controlling a serial device
The In argument is a binary holding the input parameter to the device request. The Out parameter will hold the result of the request if the ioctl is in/out.
ioctl/3 can be used for implementing most serial operations.
examples
Examples
-define(TCXONC, 16#540A).
tcflow(FD, Action) when is_atom(Action) ->
case serctl:constant(Action) of
undefined ->
{error, unsupported};
N ->
serctl:ioctl(
fd,
?TCFLSH,
<<N:4/native-unsigned-integer-unit:8>>
)
end.
-spec ispeed(binary() | #termios{}) -> non_neg_integer().
return the input speed of a serial device
Note the speed returned is the constant defined for the system and may differ between platforms.
ispeed/2 returns an Erlang termios record that can be used for setting the input speed of the serial device.
Failure: badarg if Speed is an invalid atom.-spec ispeed(<<_:64, _:_*8>> | #termios{}, atom() | integer()) -> <<_:8, _:_*8>> | #termios{}.
Enable raw mode
Returns an Erlang termios record with attributes that can be used to put the serial device into raw mode.Open a serial device
A serial device is a character device such as /dev/ttyUSB0.
A previously opened file descriptor can also be used. The fd should be opened with the O_NONBLOCK|O_NOCTTY flags.-spec ospeed(binary() | #termios{}) -> non_neg_integer().
return the output speed of a serial device
Note the speed returned is the constant defined for the system and may differ between platforms.
ospeed/2 returns an Erlang termios record that can be used for setting the output speed of the serial device.
Failure: badarg if Speed is an invalid atom.-spec ospeed(<<_:64, _:_*8>> | #termios{}, atom() | integer()) -> <<_:8, _:_*8>> | #termios{}.
Read from a serial device
Size is an unsigned long.Read the specified number of bytes from a serial device
readx/2 will block forever.
readx/3 accepts a timeout value. The behaviour of readx/3 when the timeout is reached is to throw away any buffered data and return {error, eintr} to the caller, e.g., the caller will not be returned the contents of a partial read. (The justification for this behaviour: the caller has stated they require a fixed number of bytes so the contents of a partial read represents unspecified behaviour.)-spec setflag(binary() | #termios{}, proplists:proplist()) -> #termios{}.
Returns an Erlang termios record used for setting the attributes of a serial device
For example, to create attributes that can be used to enable hardware flow control on a serial device:
{ok, FD} = serctl:open("/dev/ttyUSB0"),
{ok, Termios} = serctl:tcgetattr(FD),
Termios1 = serctl:setflag(Termios, [{cflag, [{crtscts, true}]}]),
ok = serctl:tcsetattr(FD, tcsanow, Termios1).
discards data written but not transmitted or received but not read
The second argument determines whether to flush input, output, or bothGet the terminal attributes of a serial device
Returns the contents of the system struct termios as a binary.-spec tcsetattr(fd(), [atom()] | atom() | integer(), termios()) -> ok | errno() | {error, unsupported}.
Sets the terminal attributes of a serial device
'tcsasoft' is a non-portable, BSD action. tcsetattr/3 will return{error,unsupported}
on other platforms. Warning: the contents of Termios are passed directly to tcsettr(3). If the system tcsettr(3) does not perform any validation of the structure, it is possible the Erlang VM may crash.
-spec termios(binary() | #termios{}) -> binary() | #termios{}.
Write data to a serial device
Partial writes return the number of bytes written.