关于使用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;
        }
相关推荐
m0_748236582 小时前
本地部署轻量级web开发框架Flask并实现无公网ip远程访问开发界面
前端·tcp/ip·flask
yourkin6661 天前
TCP...
服务器·网络·tcp/ip
人工干智能2 天前
科普:你的笔记本电脑中有三个IP:127.0.0.1、无线网 IP 和局域网 IP;两个域名:localhost和host.docker.internal
网络协议·tcp/ip·电脑
查理养殖场2 天前
计算机网络之TCP的可靠传输
网络·tcp/ip·计算机网络
六六六六六66662 天前
企业组网IP规划与先关协议分析
服务器·网络·tcp/ip
AIGC安琪2 天前
【Stable Diffusion】SD迎来动画革命,AnimateDiff快速出图
人工智能·网络协议·tcp/ip·stable diffusion·aigc
我们的五年3 天前
【Linux网络】TCP/IP地址的有机结合(有能力VS100%???),IP地址的介绍
linux·运维·网络·tcp/ip
忆源3 天前
SOME/IP--协议英文原文讲解11
网络·网络协议·tcp/ip
baowxz3 天前
tcp协议连接,和传输数据
网络·网络协议·tcp/ip
小菠萝09083 天前
PLC通信交互系统技术分享
c++·算法·plc