测试"/>
树莓派 .net core3.1 读取S7 200 Smart 数据测试
目标
使用 core3.1 通过树莓派网关(armv7且具备以太网通讯,zero不行,处理器是armv6的)读取西门子PLC的数据。
测试工具
- 树莓派,具备以太网口;
- 西门子PLC,我用的是S7 200 Smart;
- 4口交换机,一个给plc,一个给树莓派,一个给上位机;
- core 3.1,PC上安装的是vs2019带的,树莓派上安装需要先下载 32位 sdk和runtime;
- Sharp7 代码,文档在doc/sharp7-refman.pdf,里面描述了各类西门子plc的连接参数;
- Putty用于远程登陆树莓派;
- FillZilla用于向树莓派传文件。
安装 core
mkdir dotnet
tar zxf aspnetcore-runtime-3.1.9-linux-arm.tar.gz -C $HOME/dotnet
tar zxf dotnet-sdk-3.1.403-linux-arm.tar.gz -C $HOME/dotnet
修改环境变量
nano ~/.bashrc
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet
ctrl+o保存,回车,ctrl+x
source ~/.bashrc
验证安装有效
dotnet --info
配置网络
设置树莓派、plc、pc有线网络在一个网段。
其中,plc ip配置:
树莓派ip配置:
ifconfigsudo nano /etc/network/interfacesauto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.2.101
netmask 255.255.255.0
gateway 192.168.2.254
pc ip配置:
配置完成后在树莓派上测试ping plc ip看是否通过。
PC上编写程序
在vs2019中,建立一个 core3.1 console application,将 Sharp7库中的Sharp7.cs 文件复制进来,然后在program.cs中加入测试代码。
static void Main(string[] args)
{Sharp7.S7Client client = new Sharp7.S7Client();client.SetConnectionType(3);//用basic模式连接,2 OP应该也可以,详细请参考sharp7-refman.pdfvar error = client.ConnectTo("192.168.2.1", 0, 0);//if(error == 0)Console.WriteLine("连接成功");elseConsole.WriteLine("连接失败: "+ error);byte[] buffer = new byte[1024];int start = 500;int byteread = 0;error = client.ReadArea(Sharp7.S7Area.DB, 1, start, 15, Sharp7.S7WordLength.DWord, buffer, ref byteread);//S7 200 Smart 的 V 地址空间对应于 DB 1,参见Sharp7-refman.pdf Page 9if (error == 0){for(int i = 0; i < 16; i++){var f = Sharp7.S7Helper.GetRealAt(buffer, 4 * i);Console.WriteLine(f.ToString());//Console.Write(buffer[i].ToString("X2"));//if ((i+1) % 4 == 0) { Console.WriteLine(); }}}elseConsole.WriteLine("连接失败: " + error);error = client.Disconnect();if (error == 0)Console.WriteLine("连接已断开");elseConsole.WriteLine("断开连接失败: " + error);
}
在树莓派上运行程序
用FillZilla将console application 工程 debug/netcoreapp3.1 文件夹 中的 ***.dll 文件 和 ***.runtimeconfig.json 复制到树莓派上。
Putty到树莓派上,cd 到 ***.dll文件所在目录,执行:dotnet ***.dll,然后就大功告成了!
补充1:地址空间映射关系
sharp7-refman文档第9页中,提到s7 200的 “The entire memory is mapped in the V Area accessible as DB 1 from the outside.”,也就是V地址空间映射到Area=S7Area.DB,DBNumber=1。同时,表格中给出s7 200支持DB、EB、AB和MK的读写,那么EB、AB的地址(即模拟量和数字量的输入输出)如何映射呢?根据snap7论坛上一篇文档我找到了答案:模拟量和数字量的输入对应 S7Area.PE,模拟量和数字量的输出对应S7Area.PA。测试读了一下,返回都是0,一想也对,没有接外设读到0是正常的。
The areas for the analog inputs and outputs are the same of the digital inputs and outputs.
For IW64: area=S7AreaPE, start at 64 and lenght=2 (word) with read_area function.
For QW80: area=S7AreaPA, start at 80 and lenght=2 (word) with read_area function.
更多推荐
树莓派 .net core3.1 读取S7 200 Smart 数据测试
发布评论