sersock v0.1.0 Sersock
Server module to export individual serial ports thru a socket.
A single remote socket connection is supported. Each new socket connection closes and replaces the previous one. Native port is open until a connection is made and will be closed when the connection closes. Serial ports should be free while there is no connection.
Summary
Functions
Starts the GenServer.
state
is a keyword list to be merged with the following defaults:
%{
mode: :raw,
ip: {0,0,0,0},
port: 0,
portname: "TTY",
baudrate: "115200",
bitconfig: "8N1",
bufsize: "255",
packto: "0",
}
mode
can be :text
, :modbus
or :raw
. Text mode buffers data
from the serial port and forwards complete lines terminated in \n
. Modbus mode
buffers data from the serial port and forwards complete packets. Modbus also
translates bidirectionally from Modbus TCP to Modbus RTU. Raw mode forwards
data received within the packetization timeout.
ip
can be any valid IP with {0,0,0,0}
and {127,0,0,1}
meaning any interface
or
local loop interface
respectively.
port
is the tcp port number where the serial port will be served.
portname
is the name of the serial port to be exported.
baudrate
can be any of 1200
, 2400
, 4800
, 9600
, 19200
, 38400
, 57600
, 115200
.
bitconfig
can be any of 8N1
, 7E1
, 7O1
.
packto
is a packetization timeout in milliseconds mainly for the raw mode. Serial ports are slow
and you will be forwarding lots of partial packets if no packetization strategy is used.
Packets are \n
and CRC
terminated in text and modbus mode respectively.
Raw mode can use a timeout for that effect. Modbus and text modes may benefit as well
on certain scenarios but should work ok without it (zero value).
bufsize
is the buffer size. Handling of packets larger that this will crash the native port.
opts
is optional and is passed verbatim to GenServer.
Returns {:ok, pid}
.
Example
Sersock.start_link([portname: "ttyUSB0", port: 5000], [name: Sersock])