关于使用TCP-S7协议读写西门子PLC字符串的问题

我们可以使用TCP-S7协议读写西门子PLC,

比如PLC中定义一个String[50] 的地址DB300.20

地址DB300.20

DB块编号为300,偏移量【地址】是30

S7协议是西门子PLC自定义的协议,默认端口102,本质仍然是TCP协议的一种具体实现,

如果使用C#读写西门子PLC协议,需要开启一个TcpClient,然后连接102端口。然后发哦送两次握手之后即可读写。

一、PLC需要设置 运行远程对象的Put/GET访问

二、PLC还要手动开启对应DB块的【非优化访问】,DB块默认是【优化访问的,无偏移量】

对西门子PLC字符串的读写逻辑如下:

西门子PLC字符串逻辑 string[50],占用52个字节(偏移量),第一个字节是最大长度,就是50,第二个字节是实际长度24,第三个字节之后就是ASCII码,

PLC解析逻辑为找到第二个字节的长度length.,然后查找length个字符,就结束

示例读写西门子字符串代码如下:

cs 复制代码
        /// <summary>
        /// 写入西门子PLC的字符串,写入的字节长度为length+2,其中第一个字节代表最大长度【PLC设定的】,第二个字节代表时间长度
        /// </summary>
        /// <param name="dbNumber"></param>
        /// <param name="offsetAddress"></param>
        /// <param name="maxLength"></param>
        /// <param name="writeString"></param>
        /// <returns></returns>
        static bool DB_WriteString(short dbNumber, ushort offsetAddress, int maxLength, string writeString) 
        {
            byte[] byteArray = new byte[maxLength + 2];
            byteArray[0] = (byte)maxLength;//第一个字节代表最大长度
            byteArray[1] = (byte)writeString.Length;//第二次字节代表实际长度
            if (byteArray[1] > byteArray[0]) 
            {
                throw new Exception($"实际长度【{byteArray[1]}】不能超过PLC字符串的最大设定长度【{maxLength}】");
            }
            byte[] stringByteArray = System.Text.Encoding.ASCII.GetBytes(writeString);
            Array.Copy(stringByteArray, 0, byteArray, 2, stringByteArray.Length);//第三个字节开始写入字符串的ASCII码
            return WriteSerialByteArray(dbNumber, offsetAddress, byteArray);
        }

        static bool WriteSerialByteArray(short dbNumber, ushort offsetAddress, byte[] byteArray) 
        {
            Console.WriteLine($"这里写入DB{dbNumber}.{offsetAddress}的连续字节数组【{string.Join(",", byteArray.Select(x => x.ToString("X2")))}】");
            return true;
        }

        static bool ReadSerialByteArray(short dbNumber, ushort offsetAddress, out byte[] byteArray)
        {            
            byteArray = new byte[] { 12, 6, 48, 49, 97, 98, 65, 66, 0, 0, 0, 0 };//假设读取12个字节
            Console.WriteLine($"这里读取DB{dbNumber}.{offsetAddress}的连续字节数组【{string.Join(",", byteArray.Select(x => x.ToString("X2")))}】");
            return true;
        }

        /// <summary>
        /// 读取字符串:从第三个字节开始,读取实际长度个字符  actualLength = byteArray[1]
        /// </summary>
        /// <param name="dbNumber"></param>
        /// <param name="offsetAddress"></param>
        /// <param name="readString"></param>
        /// <returns></returns>
        static bool DB_ReadString(short dbNumber, ushort offsetAddress, out string readString)
        {
            ReadSerialByteArray(dbNumber, offsetAddress, out byte[] byteArray);
            if (byteArray.Length < 2) 
            {
                throw new Exception($"读取字符串时,原字节数组长度不能低于2");
            }
            byte maxLength = byteArray[0];
            byte actualLength = byteArray[1];
            if (byteArray.Length - 2 < actualLength) 
            {
                throw new Exception($"读取到的字节长度 小于 实际长度,非法的读取,PLC设定的字符串最大长度为【{maxLength}】");
            }
            readString = System.Text.Encoding.ASCII.GetString(new ArraySegment<byte>(byteArray, 2, actualLength).ToArray());
            return true;
        }
相关推荐
云计算-Security2 小时前
如何理解 IP 数据报中的 TTL?
网络协议·tcp/ip
稳联技术3 小时前
实践提炼,EtherNet/IP转PROFINET网关实现乳企数字化工厂增效
网络·网络协议·tcp/ip
2501_916007479 小时前
绕过 Xcode?使用 Appuploader和主流工具实现 iOS 上架自动化
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_916013749 小时前
使用 Windows 完成 iOS 应用上架:Appuploader对比其他证书与上传方案
websocket·网络协议·tcp/ip·http·网络安全·https·udp
浩浩测试一下1 天前
Authpf(OpenBSD)认证防火墙到ssh连接到SSH端口转发技术栈 与渗透网络安全的关联 (RED Team Technique )
网络·网络协议·tcp/ip·安全·网络安全·php
孤寂大仙v1 天前
【计算机网络】网络层IP协议与子网划分详解:从主机通信到网络设计的底层逻辑
tcp/ip·计算机网络·php
漫步者TZ1 天前
【Netty系列】解决TCP粘包和拆包:LengthFieldBasedFrameDecoder
java·网络协议·tcp/ip·netty
hgdlip1 天前
重启路由器ip不变怎么回事?原因分析与解决方法
网络协议·tcp/ip·智能路由器
hgdlip1 天前
手机上网可以固定ip地址吗?详细解析
网络·tcp/ip·智能手机
2501_915921431 天前
高敏感应用如何保护自身不被逆向?iOS 安全加固策略与工具组合实战(含 Ipa Guard 等)
websocket·网络协议·tcp/ip·http·网络安全·https·udp