关于使用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;
        }
相关推荐
国中之林7 小时前
【qt】如何获取本机的IP地址?
服务器·qt·网络协议·学习·tcp/ip
iphttp20 小时前
国外使用代理IP的安全风险
服务器·tcp/ip·安全
落子摘星21 小时前
Raw Socket(一)实现TCP三次握手
网络·网络协议·tcp/ip·raw socket
听楷哥说跨境1 天前
UDP协议:独特之处及其在网络通信中的应用
网络·网络协议·tcp/ip·ip
cdsn123jian1 天前
linux 发送报文的几种方式
linux·服务器·网络·tcp/ip
从后端到QT2 天前
QT TCP网络通信编程
开发语言·qt·tcp/ip
爱编程的小猴2 天前
【面试题】Reactor模型
开发语言·网络·网络协议·tcp/ip
科学的发展-只不过是读大自然写的代码2 天前
ubuntu 如何查看某一个网卡的ip地址
linux·tcp/ip·ubuntu
洛神灬殇2 天前
分布式技术专题 | TCP在分布式网络中的通信机制与底层实现
网络·分布式·tcp/ip
Ares-Wang2 天前
FTP、http 、tcp
网络协议·tcp/ip·http