查看PLC IP 端口_PLC实现TCP通讯

首先设置PLC的IP地址,设置为ASSII码通讯。

f7a001156bd421d99feee87abfadb9a6.png

设置PLCIP地址

打开PLC中TCP MC协议,并设置端口

ba5cd716a290b5505800b06da1b10e1c.png

打开MC协议

然后重新启动PLC,就可以通过IP进行PLC的访问了

2de27218d0b68e9bd4463c934cb99761.png

IP访问PLC

我们对PLC通过TCP编程进行通讯访问。在VS2010中新建类,建立TcpClient实例,传入IP地址及端口参数,测试其连接状态。

        ///         /// 连接PLC        ///         /// IP地址        /// port1接口        ///         public string connect_PLC(string IpAddress, int port1Num)        {            string s = "OK";            TcpClient Client = new TcpClient();            try            {                Client.Connect(IpAddress, port1Num);                Client.GetStream().Close();                Client.Close();            }            catch (Exception ex)            {                s = "连接服务器失败!原因:" + ex.ToString();            }            return s;        }
c141ff350099b6fed351270749bd797b.png

测试PLC通讯连接

PLC实现了TCP通讯后,接下来就可以传命令实现读取或修改PLC内数值了。按照说明书的MC协议指令进行拼接指令就可以了。

cfcce289c75e08240de50d7eeb6ac764.png

MC协议说明

实现写入PLC指定区域数据命令

        ///         /// 写入PLC数据        ///         /// IP地址        /// port接口        /// 软件元名称(DXC..)        /// 开始位置        /// 写入数量        /// 写入的数值(int数组与数量匹配)        /// 延后时间(time*250ms)        ///         public string write_PLC(string plc_IP1, int port1, string memory, int qishi, int cnt, int[] s, int time)        {            byte[] buffer;            byte[] inbuff = new byte[1532];            string RxResponse;            string key = "";            TcpClient Client = new TcpClient();            try            {                Client.Connect(plc_IP1, port1);            }            catch (Exception ex)            {                key = "连接服务器失败!原因:" + ex.ToString();            }            if (key == "")            {                StringBuilder str = new StringBuilder();                str.Append("03FF" + time.ToString("x4"));                if (memory.ToUpper() == "C")                {                    str.Append("434E");                }                else if (memory.ToUpper() == "D")                {                    str.Append("4420");                }                else                { }                str.Append(qishi.ToString("x8"));                str.Append(cnt.ToString("x2") + "00");                for (int i = 0; i < s.Length; i++)                    str.Append(s[i].ToString("x4"));                buffer = System.Text.Encoding.Default.GetBytes(str.ToString());                Client.GetStream().Write(buffer, 0, buffer.Length);                System.Threading.Thread.Sleep(1500);                if (Client.GetStream().DataAvailable)                {                    Client.GetStream().Read(inbuff, 0, inbuff.Length);                    RxResponse = System.Text.Encoding.Default.GetString(inbuff);                    key = RxResponse;                }                Client.GetStream().Close();                Client.Close();            }            return key;        }

读取文本处理后,写入数据库功操作,这里设置了Timer定时器,自动读取PLC的数值

033a9fe4aa3de98cf4b3d12888df06b1.png

自动读取PLC数值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值