OpenWrt 7: SER2NET – tunelujeme seriovy port (RS-232) po siti

Ne vzdy je moznost provozovat ridici program primo na pocitaci se seriovym portem a pripojenym hardwarem. Prikladem jsou treba WiFi routery s OpenWrt. Do 4MB flash musite narvat system, ovladace i svoji aplikaci. Misto prepisovani programu do C a setreni mistem je casto pohodlnejsi seriovy port tunelovat na vykonny server.

Jak nasdilet seriovy port?

Jde to vice zpusoby, ale asi nejjednodussi je program ser2net. Ser2net ma po zakladni instalaci v konfiguraku /etc/ser2net.conf docela prehlednou dokumentaci:

#
# This is the configuration file for ser2net.  It has the following format:
#  ::::
#     TCP port
#            Name   or  number of the TCP/IP port to accept con-
#            nections from for this device.  A port number may
#            be of the form [host,]port, such as 127.0.0.1,2000
#            or localhost,2000.  If this is specified, it will
#            only bind to the IP address specified. Otherwise
#            it will bind to all the ports on the machine.
#
#     state  Either raw or rawlp or telnet or off.  off disables
#            the  port  from  accepting  connections.  It can be
#            turned on later from the control port.  raw enables
#            the port and  transfers  all data as-is between the
#            port  and  the long.  rawlp  enables  the port  and
#            transfers  all input data to device, device is open
#            without  any  termios  setting.  It  allow  to  use
#            /dev/lpX  devices  and  printers connected to them.
#            telnet enables the port and runs the telnet  proto-
#            col  on the port to set up telnet parameters.  This
#            is most useful for using telnet.
#
#     timeout
#            The time (in seconds) before the port will be  dis-
#            connected  if  there  is no activity on it.  A zero
#            value disables this funciton.
#
#     device The  name  of  the  device   to  connect  to.  This
#            must be in the form of /dev/.
#
#     options
#            Sets  operational  parameters  for the serial port.
#            Options 300, 1200, 2400, 4800, 9600, 19200, 38400,
#            57600, 115200 set the various baud rates.  EVEN,
#            ODD, NONE set the parity.  1STOPBIT, 2STOPBITS set
#            the number of stop bits.  7DATABITS, 8DATABITS set
#            the number of data bits.  [-]XONXOFF turns on (-
#            off) XON/XOFF support.  [-]RTSCTS turns on (- off)
#            hardware flow control, [-]LOCAL turns off (- on)
#            monitoring of the modem lines, and
#            [-]HANGUP_WHEN_DONE turns on (- off) lowering the
#            modem control lines when the connextion is done. 
#	     NOBREAK disables automatic setting of the break
#	     setting of the serial port.
#            The "remctl" option allow remote control (ala RFC
#            2217) of serial-port configuration.  A banner name
#            may also be specified, that banner will be printed
#            for the line.  If no banner is given, then no
#            banner is printed.
#
# or...

#  BANNER::banner
#    This will create a banner, if the banner name is given in the
#    options of a line, that banner will be printed.  This takes the
#    standard "C" \x characters (\r is carraige return, \n is newline,
#    etc.).  It also accepts \d, which prints the device name, \p,
#    which prints the TCP port number, and \s which prints the serial
#    parameters (eg 9600N81).  Banners can span lines if the last
#    character on a line is '\'.  Note that you *must* use \r\n to
#    start a new line.
#
# Note that the same device can be listed multiple times under different
# ports, this allows the same serial port to have both telnet and raw
# protocols.

# The original config file shipped with the upstream sources can be
# found in /usr/share/doc/ser2net/examples

BANNER:banner:\r\nser2net port \p device \d [\s] (Debian GNU/Linux)\r\n\r\n

2000:telnet:600:/dev/ttyS0:9600 8DATABITS NONE 1STOPBIT banner
2001:telnet:600:/dev/ttyS1:9600 8DATABITS NONE 1STOPBIT banner
3000:telnet:600:/dev/ttyS0:19200 8DATABITS NONE 1STOPBIT banner
3001:telnet:600:/dev/ttyS1:19200 8DATABITS NONE 1STOPBIT banner

Pro zacatek vsechno zakomentujeme. A hned potom zacneme pridavat vlastni radky.

Priklad (dale uvazujeme IP adresu serveru 192.168.1.1):

3333:raw:60:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT # klasika...
4444:raw:60:/dev/ttyUSB1:115200 8DATABITS NONE 1STOPBIT # maximalni rychlost...
1234:raw:60:/dev/ttyS0:19200 8DATABITS NONE 2STOPBIT # 19200 8N2 - modbus rtu
To budou jedine nezakomentovane radky v konfigu. Pote spustime ser2net.
Od teto chvile se PC chova jako server a po siti zpristupni seriove porty.
 
Na vytvoreni jednoho naslouchajici portu je mozne spustit ser2net primo z prikazove radky. Parametr -C definuje radek z konfiguracniho souboru:
# ser2net -C "3333:raw:60:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT"
 
Na OpenWrt je nutne nainstalovat ser2net a pokud pouzivame USB-RS232 prevodnik, tak i jeho ovladac:
# opkg update
# opkg install ser2net    # + volitelne jmeno ovladace usb prevodniku
 
V prohlizeci si vyhledame „kmod-usb„. 
Podpora prevodiku v aktualnim OpenWrt 14.07 je lepsi, nez v nejnovejsich Windows:
kmod-usb-acm
kmod-usb-serial-ark3116
kmod-usb-serial-belkin
kmod-usb-serial-ch341
kmod-usb-serial-cp210x
kmod-usb-serial-cypress-m8
kmod-usb-serial-ftdi
kmod-usb-serial-ipw
kmod-usb-serial-keyspan
kmod-usb-serial-mct
kmod-usb-serial-mos7720
kmod-usb-serial-motorola-phone
kmod-usb-serial-option
kmod-usb-serial-oti6858
kmod-usb-serial-pl2303
kmod-usb-serial-qualcomm
kmod-usb-serial-sierrawireless
kmod-usb-serial-ti-usb
kmod-usb-serial-visor
kmod-usb-serial-wwan
kmod-usb-serial
Pozor! OpenWrt pravdepodobne v dost verzich neresi automaticke spusteni ser2net po nabootovani. Je potreba zapnout init skript a pokud neexistuje, tak si ho napsat.
Ukazkovy init skript muze mit podobu treba: 
#!/bin/sh /etc/rc.common
START=99
STOP=10

start() {
	ser2net
}

stop(){
	killall ser2net
	sleep 1   # to je asi zbytecne
	killall -9 ser2net   # to je asi zbytecne

}
ulozime ho do /etc/init.d/ser2net a zapneme:
# /etc/init.d/ser2net enable
 

Jak se k tomu pripojit?

Prvni a nejjednodussi moznosti je klasicky telnet. Muzeme pouzit PuTTY (Windowsaci), nebo klasicky telnet klient.
$ telnet 192.168.1.1 3333
nebo lepe 
$ nc 192.168.1.1 3333
a muzeme psat. Vse se bude prenaset po siti az k zarizeni na RS-232. Vyuziti je skvele treba pri programovani. Nase naprogramovana aplikace si povida s hardwarem pres TCP socket a nic jineho ji nezajima. Pohodlne je to obzvlast u Javy. Vsichni, kdo ladili multipatformni Java aplikaci s RXTX knihovnou na seriovy port asi souhlasi…
 
Co kdyz ale potrebujeme mit „klasicky hardware“? Tedy bezny soubor zarizeni, ktery maji v linuxu vsechny seriove porty. K tomu se da pouzit program socat. Socat vytvori soubor zarizeni a data do/z nej sype pres TCP socket. Vse zcela transparentne. Uzivatel, ani aplikace nic nepoznaji:
# socat pty,link=/tmp/ttyV0,waitslave tcp:192.168.1.1:3333
# chmod 777 /tmp/ttyV0 
Tim nam vzniklo nove zarizeni /tmp/ttyV0. Na praci je identicke s /dev/ttyS0. Klidne ho muzeme umistit do /dev, ale teoreticky se pak muze tlouct s jinym fyzicky pripojenym zarizenim. Pokud ma s nove vytvorenym zarizenim pracovat bezny uzivatel, je potreba nastavit zarizeni prava (777 neni vhodny priklad zabezpeceni). Ser2net-socat prenos neni sifrovany! Pokud potrebujeme sifrovat, muzeme komunikaci obalit treba stunellem.
 
Sam takhle tuneluji seriovy port z TP-Link WR741ND WiFi routeru s OpenWrt. K WiFi routeru je pres MAX485 (RS-485 sbernice) pripojene nekolik Arduino zarizeni. Protoze se ridici app do routeru nevesla (4MB flash neni moc), tak ovladani dela vzdaleny server.
 
 
Dalsi clanky o OpenWrt.

 

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *