关于使用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;
        }
相关推荐
一只小鱼儿吖3 小时前
进程代理单窗口单IP技术:原理、应用与实现
网络·网络协议·tcp/ip
稳联技术3 小时前
Ethernet IP与Profinet共舞:网关驱动绿色工业的智慧脉动
网络·网络协议·tcp/ip
计算机毕设定制辅导-无忧学长3 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
游戏开发爱好者85 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915106325 小时前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
00后程序员张12 小时前
免Mac上架实战:全平台iOS App上架流程的工具协作经验
websocket·网络协议·tcp/ip·http·网络安全·https·udp
醉方休16 小时前
TCP、HTTP/1.1 和HTTP/2 协议
网络协议·tcp/ip·http
创小匠16 小时前
创客匠人解析创始人 IP 定位:从专业度到用户心智的占领之道
网络·网络协议·tcp/ip
车载测试工程师17 小时前
车载以太网网络测试-29【SOME/IP-SD】-SD状态机
网络·网络协议·tcp/ip·车载系统·php
在下Z.18 小时前
利用TCP协议,创建一个多人聊天室
网络·网络协议·tcp/ip