ユーザ用ツール

サイト用ツール


mae3xx_ope:use_socat_service:start

差分

この文書の現在のバージョンと選択したバージョンの差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
mae3xx_ope:use_socat_service:start [2014/12/04 11:09]
admin
mae3xx_ope:use_socat_service:start [2022/11/16 17:17] (現在)
admin [UDP Client mode]
ライン 1: ライン 1:
 +====== TCP/UDP - Serial 変換の利用 ======
 +
 +v2.2.0β7 から、socat を利用した TCP/UDP <-> Serial 変換サービスを追加しました。\\
 +利用できるモードは下記となります。
 +
 +  * TCP Server mode
 +  * UDP Server mode
 +  * TCP Client mode
 +  * UDP Client mode
 +
 +\\
 +
 +===== 設定 =====
 +
 +/​etc/​default/​socat ファイルを編集します。
 +
 +<file conf socat>
 +ttyO1=yes
 +ttyO1_MODE=SERVER
 +ttyO1_PROTO=TCP
 +ttyO1_LCLPORT=54321
 +ttyO1_DSTADDR=
 +ttyO1_DSTPORT=
 +ttyO1_WAIT=60
 +ttyO1_KEEPCNT=3
 +ttyO1_KEEPIDLE=30
 +ttyO1_KEEPINTVL=30
 +ttyO1_SNDTIMEO=10
 +ttyO1_CORK=0
 +ttyO1_BAUD=115200
 +ttyO1_CHAR=8
 +ttyO1_STOP=1
 +ttyO1_PARITY=NONE
 +ttyO1_RTSCTS=yes
 +ttyO1_TIME=1
 +ttyO1_MIN=255
 +
 + 
 +#​tyO1=yes|no
 +#​ttyO1_MODE=SERVER|CLIENT
 +#​ttyO1_PROTO=TCP|UDP
 +#​ttyO1_LCLPORT=54321
 +#​ttyO1_DSTADDR=
 +#​ttyO1_DSTPORT=
 +#​ttyO1_WAIT=60
 +#​ttyO1_KEEPCNT=3
 +#​ttyO1_KEEPIDLE=30
 +#​ttyO1_KEEPINTVL=30
 +#​ttyO1_SNDTIMEO=10
 +#​ttyO1_CORK=1|0
 +#​ttyO1_BAUD=921600|460800|230400|115200|57600|38400|19200|9600|4800|2400|1200
 +#​ttyO1_CHAR=8|7
 +#​ttyO1_STOP=1|2
 +#​ttyO1_PARITY=NONE
 +#​ttyO1_RTSCTS=yes
 +#​ttyO1_TIME=1
 +#​ttyO1_MIN=10
 +</​file>​
 +
 +\\
 +
 +各項目の設定の内容は、下表のとおりとなります(x は 0/1)。
 +
 +^  Key  ^  Info  ^  Default ​ ^  Notes  |
 +|ttyOx|使用する / しない| ​ no  |socat 機能利用有無 (yes / no)|
 +|ttyOx_MODE|動作モード指定| ​ -  |SERVER / CLIENT|
 +|ttyOx_PROTO|TCP / UDP 選択| ​ -  |TCP / UDP|
 +|ttyOx_LCLPORT|ローカルポート番号| ​ -  |SERVER および CLIENT(UDP)|
 +|ttyOx_DSTADDR|接続先アドレス| ​ -  |CLIENT mode のみ|
 +|ttyOx_DSTPORT|接続先ポート番号| ​ -  |CLIENT mode のみ|
 +|**ttyOx_WAIT**|接続失敗時 再接続待ち時間 [s]|  60  |TCP CLIENT mode のみ|
 +|**ttyOx_KEEPCNT**|Keepalive count| ​ 3  |TCP mode のみ|
 +|**ttyOx_KEEPIDLE**|Keepalive send idle time [s]|  30  |TCP mode のみ|
 +|**ttyOx_KEEPINTVL**|Keepalive send interval [s]|  30  |TCP mode のみ|
 +|**ttyOx_SNDTIMEO**|送信タイムアウト時間 [s]|  10  |TCP mode のみ((参照:​ [[http://​patchwork.ozlabs.org/​patch/​62889/​|TCP_USER_TIMEOUT:​ a new socket option to specify max timeout before a TCP connection is aborted]]))|
 +|ttyOx_CORK|socket option "​TCP_CORK"​ 有効/​無効| ​ 1  |TCP mode のみ|
 +|ttyOx_BAUD|UART Baudrate| ​ 115200 ​ |921600 / 460800 / 230400 / 115200 / 57600 / 38400 / 19200 / 9600 / 4800 / 2400 / 1200((RS-232C使用時は、460800bps までとなります。))|
 +|ttyOx_CHAR|UART Character bits|  8  |8 / 7|
 +|ttyOx_STOP|UART Stop bits|  1  |1 / 2|
 +|ttyOx_PARITY|UART Parity| ​ NONE  |NONE / EVEN / ODD|
 +|ttyOx_RTSCTS|UART Hardware flow control| ​ yes  |yes / no|
 +|ttyOx_TIME|Termios option (VTIME)| ​ 1  | |
 +|ttyOx_MIN|Termios option (VMIN)| ​ 10  | |
 +
 +Termios VTIME, VMIN について:​ [[http://​oasynnoum.hatenablog.com/​entry/​2012/​10/​31/​184038|termios.c_cc の VMIN VTIME について(メモ) - 壊れたメガネ ホッチキスの達人の意識の高いブログ。]]\\
 +WAIT, KEEPCNT, KEEPIDLE, KEEPINTVL, SNDTIMEO オプションは、v2.2.2α1 からのサポートとなります。\\
 +CORK オプション(( (man page "tcp - TCP プロトコル"​ より)セットされると、 partial フレームを送信しない。 このオプションが解除されると、 キューイングされた partial フレームが送られる。これは sendfile(2) を呼ぶ前にヘッダを前置したり、 スループットを最適化したい場合に便利である。 現在の実装では、 TCP_CORK で出力を抑えることができる時間の上限は 200 ミリ秒である。 この上限に達すると、キューイングされたデータは自動的に送信される。 Linux 2.5.71 以降においてのみ、このオプションを TCP_NODELAY と同時に用いることができる。 移植性の必要なプログラムではこのオプションを用いるべきではない。))は、v2.4.3β1 からのサポートとなります。
 +
 +\\
 +
 +==== TCP Server mode ====
 +
 +TCP で待ち受け、シリアルポートとの間を中継するモードです。\\
 +設定が必要な項目は下記のとおりです。
 +
 +|< 24em 6em 9em 6em >|
 +^  Key  ^  Value  ^  Notes  |
 +|ttyOx| ​ yes  | |
 +|ttyOx_MODE| ​ SERVER ​ | |
 +|ttyOx_PROTO| ​ TCP  | |
 +|ttyOx_LCLPORT| ​ ポート番号 (bind) ​ | |
 +|  UART 関係は全て設定が必要です ​ |||
 +
 +\\
 +
 +==== UDP Server mode ====
 +
 +UDP で待ち受け、シリアルポートとの間を中継するモードです。\\
 +設定が必要な項目は下記のとおりです。
 +
 +|< 24em 6em 9em 6em >|
 +^  Key  ^  Value  ^  Notes  |
 +|ttyOx| ​ yes  | |
 +|ttyOx_MODE| ​ SERVER ​ | |
 +|ttyOx_PROTO| ​ UDP  | |
 +|ttyOx_LCLPORT| ​ ポート番号 (bind) ​ | |
 +|  UART 関係は全て設定が必要です ​ |||
 +
 +\\
 +
 +==== TCP Client mode ====
 +
 +指定されたアドレス:​ポート番号に TCP で接続し、シリアルポートとの間を中継するモードです。\\
 +設定が必要な項目は下記のとおりです。
 +
 +|< 24em 6em 11em 6em >|
 +^  Key  ^  Value  ^  Notes  |
 +|ttyOx| ​ yes  | |
 +|ttyOx_MODE| ​ CLIENT ​ | |
 +|ttyOx_PROTO| ​ TCP  | |
 +|ttyOx_DSTADDR| ​ 接続先アドレス ​ | |
 +|ttyOx_DSTPORT| ​ 接続先ポート番号 ​ | |
 +|  UART 関係は全て設定が必要です ​ |||
 +
 +\\
 +
 +==== UDP Client mode ====
 +
 +指定されたアドレス:​ポート番号に UDP で接続し、シリアルポートとの間を中継するモードです。\\
 +設定が必要な項目は下記のとおりです。
 +
 +|< 24em 6em 11em 6em >|
 +^  Key  ^  Value  ^  Notes  |
 +|ttyOx| ​ yes  | |
 +|ttyOx_MODE| ​ CLIENT ​ | |
 +|ttyOx_PROTO| ​ UDP  | |
 +|ttyOx_LCLPORT| ​ ポート番号 (bind) ​ |省略可|
 +|ttyOx_DSTADDR| ​ 接続先アドレス ​ | |
 +|ttyOx_DSTPORT| ​ 接続先ポート番号 ​ | |
 +|  UART 関係は全て設定が必要です ​ |||
 +
 +
 +\\
 +
 +===== 起動 =====
 +
 +service コマンドで起動させます。次回起動時以降は、自動で起動されます。
 +
 +<​code>​
 +user1@plum:​~$ sudo service socat-ttyO1 start
 +[sudo] password for user1: ​
 +socat-ttyO1 start/​running,​ process 2630
 +user1@plum:​~$
 +</​code>​
 +
 +<​code>​
 + 2630 ?        Ss     0:00 /bin/bash /​usr/​local/​sbin/​socat_server ttyO1
 + 2632 ?        S      0:00  \_ socat -d TCP-LISTEN:​54321,​reuseaddr,​fork EXEC:"/​usr/​local/​sbin/​socat_child ttyO1 b115200,​cs8,​cstopb=0,​crtscts=1,​raw,​echo=0,​parenb=0,​time=1,​min=10",​pty,​echo=0
 +</​code>​
 +
 +\\
 +
 +今回は、下記のように設定しました。
 +
 +<file conf socat>
 +ttyO1=yes
 +ttyO1_MODE=SERVER
 +ttyO1_PROTO=TCP
 +ttyO1_LCLPORT=54321
 +ttyO1_DSTADDR=
 +ttyO1_DSTPORT=
 +ttyO1_BAUD=115200
 +ttyO1_CHAR=8
 +ttyO1_STOP=1
 +ttyO1_PARITY=NONE
 +ttyO1_RTSCTS=yes
 +ttyO1_TIME=1
 +ttyO1_MIN=10
 +</​file>​
 +
 +\\
 +
 +==== TCP Client mode 時 ====
 +
 +TCP Client mode では、socat 起動前にサーバ側が起動しているかどうかの確認を行います。\\
 +ポートが Open していない場合、Open するまでウェイトします。
 +
 +<​code>​
 +Oct 28 14:24:26 plum socat_server:​ remote (192.168.253.49:​54322) is closed.
 +Oct 28 14:25:31 plum socat_server:​ remote (192.168.253.49:​54322) is closed.
 +</​code>​
 +
 +\\
 +
 +ポートが Open しているのを確認後、socat が起動されます。
 +
 +<​code>​
 +Oct 28 14:27:36 plum socat_server:​ remote (192.168.253.49:​54322) is OK.
 +Oct 28 14:27:36 plum socat_server:​ cmdline: socat -d /​dev/​ttyO1,​b115200,​cs8,​cstopb=0,​crtscts=1,​raw,​echo=0,​parenb=0,​time=1,​min=10 tcp:​192.168.253.49:​54322,​keepalive,​keepcnt=3,​keepidle=30,​keepintvl=30,​setsockopt-int=6:​18:​10000
 +</​code>​
 +
 +\\
 +
 +==== 注意点 ====
 +
 +Kernel console および getty との衝突を避けるため、下記制限があります。
 +
 +  * kernel command line で "​console="​ に指定されているポートでは起動できません。
 +
 +<​code>​
 +user1@plum:​~$ cat /​proc/​cmdline ​
 +ubi.mtd=0 mtdoops.mtddev=OOPS mtdoops.record_size=8192 zswap.enabled=1 ​ root=AREA=0 ​ console=ttyO0,​115200 ​ overlay=tmpfs overlay_driver=aufs ​ rootdelay=1
 +user1@plum:​~$
 +</​code>​
 +
 +この例では、"​ttyO0"​ では起動できません。
 +
 +\\
 +
 +==== おまけ ====
 +
 +RS-232C のポートを RS-485 に変換して本機能を使う場合、この製品で動作しました((本来電源供給用ではない信号線からの微弱な電流でコンバータICを動作させているので、動作保証はできません。))。\\
 +1430円です。
 +
 +[[http://​www.amazon.co.jp/​OEM-%E3%82%B7%E3%83%AA%E3%82%A2%E3%83%AB-RS232toRS485-%E5%A4%89%E6%8F%9B%E3%82%A2%E3%83%80%E3%83%97%E3%82%BF-520197/​dp/​B000V6ITS8|シリアル RS232toRS485 変換アダプタ-520197 - Amazon.co.jp]]
 +
 +{{http://​ecx.images-amazon.com/​images/​I/​41mUtNbR96L.jpg|RS232toRS485 - 1}}
 +{{http://​ecx.images-amazon.com/​images/​I/​512FQRvbJ2L.jpg|RS232toRS485 - 2}}
 +
  
mae3xx_ope/use_socat_service/start.1417658970.txt.bz2 · 最終更新: 2014/12/04 11:09 by admin