制造商博客
- 奥利弗沃恩克
- 制造商博客
- Hits: 752
通过伪TTY将PLCNext PLC连接到远程RS232 / RS485
在本制造商博客文章中,我们讨论了COM服务器为PLCNext平台提供的可能性和优点。
介绍
Phoenix联系方式的扩展模块可以例如是串行适配器:
所有扩展模块都具有共同点:可以通过ProcessData循环访问它们。 这提供了直接与我们的实时应用程序直接与串行设备交互的可能性。
在某些情况下,这不是我们想要的。 相反,我们要重新使用现有(C / C ++)库,不需要实时上下文。 但是,我们会发现这些库通常被编程为直接访问COM或TTY接口。
There are two options to create a TTY interface for a PLCNext. Control
:
- Implement some functions (C++ / C#) that create a TTY interface for the AXL or IL serial adapters from inside the
PLCNext. Runtime
. - 使用COM-Server设备(如) GW设备服务器
The GW设备服务器
provides a transparent TCP to Serial
interface.
With the help of a tool like Socat
we can forward the Serial/TCP traffic directly to a pseudo TTY interface.
Using GW设备服务器
also provides the possibility to communicate with the serial device on remote locations via a TLS encrypted channel.
笔记
In this tutorial, the PLC performs as client and establishes connection to a GW设备服务器
.
(However, it is also possible to switch the PLC Socat
to a listening option and have the GW as client actively connect to the PLC.)
设置
设置GW设备服务器
We connect to the GW设备服务器
via the Web Based Management.
The default IP address of the GW设备服务器
is 192.168.254.254.
我们使用以下设置:
LAN设置 - 安全性
- 启用SSH服务器
- 启用Telnet服务器
- 禁用安全数据模式
- 禁用TFTP服务器
串行设置
- 端口1配置(COM1):在端口8000上启用TCP连接
- 端口2配置(COM2):在端口8001上启用TCP连接
测试GW设备服务器
To test if we have set up the GW设备服务器
correctly, first we connect our PLC to the GW设备服务器
via Ethernet.
Then we connect the two DSUB adapters of the GW设备服务器
with each other.
We can quickly test the setup using the tool Telnet
.
With this tool, we can send data from the PLC to the GW设备服务器
at Port8000/COM1 through the serial cable to Port8001/COM2 and finally recieve it back at the PLC.
我们使用以下命令:
ssh This email address is being protected from spambots. You need JavaScript enabled to view it.
sudo passwd root
su root
ip addr add 192.168.254.10 dev eth0
telnet 192.168.254.254 8000
## open another shell session
telnet 192.168.254.254 8001
# Type some messages
建立Socat.
To build Socat
easily on a Linux OS, we use the following commands:
mkdir tmp
cd tmp
git clone git://repo.or.cz/socat.git
cd socat
## TODO: Modify to your SDK location
source /opt/pxc/release/axcf2152/2020/0/environment-setup-cortexa9t2hf-neon-pxc-linux-gnueabi
. /opt/pxc/release/axcf2152/2020/0/site-config-cortexa9t2hf-neon-pxc-linux-gnueabi
mkdir install
autoconf
./configure $CONFIGURE_FLAGS --prefix=$(pwd)/install --enable-openssl-method
make -j2
make install
部署和安装SOCAT
To deploy the Socat
installation, we execute the following script:
cd tmp
mkdir -p deploy
cd deploy
mkdir -p usr
cp -r ../../Daemon/* .
cp -r ../socat/install/* usr/
# Create package
tar -cf ../socat-binaries.tar .
cd ..
scp socat-binaries.tar This email address is being protected from spambots. You need JavaScript enabled to view it.:~/
ssh -ttt This email address is being protected from spambots. You need JavaScript enabled to view it. \
"mkdir -p /opt/plcnext/socat-binaries && \
tar -xf /opt/plcnext/socat-binaries.tar -C /opt/plcnext/socat-binaries"
部署后,我们现在可以SSH到我们的PLC。 因此,我们首先通过运行以下命令来设置目录和配置文件来准备PLC:
deploy$ ssh This email address is being protected from spambots. You need JavaScript enabled to view it.
This email address is being protected from spambots. You need JavaScript enabled to view it.:~$ su root
This email address is being protected from spambots. You need JavaScript enabled to view it.$ mkdir -p /etc/default/socat
This email address is being protected from spambots. You need JavaScript enabled to view it.$ touch /etc/default/socat/socat.conf
This email address is being protected from spambots. You need JavaScript enabled to view it.$ touch /etc/init.d/SocatDaemon
Now, we modify the socat.conf
file to configure the parameters of the serial interface to our needs.
nano /etc/default/socat/socat.conf
文件的内容如下所示。
TARGETIP="192.168.254.254"
TARGETPORT="8000"
TTYNAME="/dev/ttyGWDeviceServer"
OPTIONS="pty,link=$TTYNAME tcp-connect:$TARGETIP:$TARGETPORT,forever,interval=15"
BAUD="9600"
PARITY=""
DATABITS="csN 8"
STOPBITS="-cstopb"
FLOWCONTROLL=""
If we need to modify further parameters, we will need to also modify the SocatDaemon
file.
有关TTY参数的更多信息,请参见StTY手册页。
Now we create a SocatDaemon
file to automatically connect to the GW设备服务器
using a Socat
daemon.
笔记:
此Socatdaemon文件是一个没有错误/断开管理等的示例。
nano /etc/init.d/SocatDaemon
该文件具有以下内容。
(点击查看/隐藏代码)
```bash #! /bin/sh ### BEGIN INIT INFO # Provides: socat # Required-Start: $local_fs $time $network # Required-Stop: $local_fs $time $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Daemon to setup Socat connection to GWd # # Description: The Socat init script will start/stop Socat as specified in /etc/default/socat/socat.conf # Logs can be found at /var/log/socat.log ### END INIT INFO NAME=socat DAEMON=/usr/bin/socat SOCAT_DEFAULTS='-d -d -d -lf /var/log/socat.log' . /etc/default/socat/socat.conf PATH=/bin:/usr/bin:/sbin:/usr/sbin [ -x $DAEMON ] || exit 0 start_socat() { start-stop-daemon --oknodo --quiet --start \ --pidfile /var/run/socat.pid \ --background --make-pidfile \ --exec $DAEMON -- $SOCAT_DEFAULTS $OPTIONS < /dev/null do_wait_tty chown admin:plcnext_firmware $TTYNAME init_tty } init_tty(){ ## For Reference ## //man7.org/linux/man-pages/man1/stty.1.html stty -F $TTYNAME echo "-----before-------" #BAUD=9600 stty -F $TTYNAME $BAUD #PARITY="" # "" or parodd or -parodd stty -F $PARITY #DATABITS="csN 8" stty -F $TTYNAME $DATABITS #STOPBITS="-cstopb" stty -F $TTYNAME $STOPBITS #FLOWCONTROLL=none #RTS handshake crtscts echo "-----initialised-------" stty -F $TTYNAME } do_wait_tty() { TIMEOUT_COUNTER=0 TIMEOUT=25 TIMEOUTREACHED=0; ttyFOUND=1; echo "Wait for TTYNAME Files" while [ 0 -eq $TIMEOUTREACHED -a 1 -eq $ttyFOUND ]; do [ -a $TTYNAME ] ttyFOUND=$? [ $TIMEOUT_COUNTER -lt $TIMEOUT ] TIMEOUTREACHED=$? TIMEOUT_COUNTER=$[$TIMEOUT_COUNTER + 1] sleep 1 done } stop_socat() { start-stop-daemon --oknodo --stop --quiet --pidfile /var/run/socat.pid --exec $DAEMON rm -f /var/run/socat.pid } start () { start_socat return $? } stop () { for PIDFILE in `ls /var/run/socat.pid 2> /dev/null`; do NAME=`echo $PIDFILE | cut -c16-` NAME=${NAME%%.pid} stop_socat done } case "$1" in start) echo "Starting socat" if start ; then echo $? else echo $? fi ;; stop) echo "Stopping socat" if stop ; then echo $? else echo $? fi ;; restart) echo "Restarting socat" stop if start ; then echo $? else echo $? fi ;; *) echo "Usage: /etc/init.d/$NAME {start|stop|restart}" exit 3 ;; esac exit 0 ```
Now that all config files are in place, we can proceed by setting up the Socat
binaries and registering the Socat
daemon.
chmod +x /etc/init.d/SocatDaemon
cd /opt/plcnext/socat-binaries
chmod +x usr/bin/*
cp -r usr /
update-rc.d -s -v SocatDaemon 99
/etc/init.d/SocatDaemon start
If everything is set up correctly, we can see the running daemon using a command like ps -e | grep socat
.
We can also check the Socat
logs located at cat /var/log/socat.log
.
从终端访问TTY接口
现在我们可以通过TTY接口发送消息。
sudo echo "PLC to GW $var" > /dev/GWDeviceServer
要持续测试伪tty,我们可以使用命令
while true; do sleep 5 && sudo echo "PLC to GW $var" >/dev/ttyUSB0 && var=$((var+1)); done
Incoming traffic can be displayed by executing cat /dev/GWDeviceServer
.
现在,任何应用程序都可以访问连接到GW设备服务器的串行设备,就像它直接连接到PLC一样。