Пример #1
0
        private void userButton40_Click(object sender, EventArgs e)
        {
            // 读取操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            bool   M100_7      = siemensTcpNet.ReadBoolFromPLC("M100.7").Content;     // 读取M100.7是否通断
            byte   byte_M100   = siemensTcpNet.ReadByteFromPLC("M100").Content;       // 读取M100的值
            short  short_M100  = siemensTcpNet.ReadShortFromPLC("M100").Content;      // 读取M100-M101组成的字
            ushort ushort_M100 = siemensTcpNet.ReadUShortFromPLC("M100").Content;     // 读取M100-M101组成的无符号的值
            int    int_M100    = siemensTcpNet.ReadIntFromPLC("M100").Content;        // 读取M100-M103组成的有符号的数据
            uint   uint_M100   = siemensTcpNet.ReadUIntFromPLC("M100").Content;       // 读取M100-M103组成的无符号的值
            float  float_M100  = siemensTcpNet.ReadFloatFromPLC("M100").Content;      // 读取M100-M103组成的单精度值
            long   long_M100   = siemensTcpNet.ReadLongFromPLC("M100").Content;       // 读取M100-M107组成的大数据值
            ulong  ulong_M100  = siemensTcpNet.ReadULongFromPLC("M100").Content;      // 读取M100-M107组成的无符号大数据
            double double_M100 = siemensTcpNet.ReadDoubleFromPLC("M100").Content;     // 读取M100-M107组成的双精度值
            string str_M100    = siemensTcpNet.ReadStringFromPLC("M100", 10).Content; // 读取M100-M109组成的ASCII字符串数据

            // 写入操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            siemensTcpNet.WriteIntoPLC("M100.7", true);                  // 写位
            siemensTcpNet.WriteIntoPLC("M100", (byte)0x33);              // 写单个字节
            siemensTcpNet.WriteIntoPLC("M100", (short)12345);            // 写双字节有符号
            siemensTcpNet.WriteIntoPLC("M100", (ushort)45678);           // 写双字节无符号
            siemensTcpNet.WriteIntoPLC("M100", 123456789);               // 写双字有符号
            siemensTcpNet.WriteIntoPLC("M100", (uint)3456789123);        // 写双字无符号
            siemensTcpNet.WriteIntoPLC("M100", 123.456f);                // 写单精度
            siemensTcpNet.WriteIntoPLC("M100", 1234556434534545L);       // 写大整数有符号
            siemensTcpNet.WriteIntoPLC("M100", 523434234234343UL);       // 写大整数无符号
            siemensTcpNet.WriteIntoPLC("M100", 123.456d);                // 写双精度
            siemensTcpNet.WriteAsciiStringIntoPLC("M100", "K123456789"); // 写ASCII字符串
        }
Пример #2
0
 private void button_read_byte_Click(object sender, EventArgs e)
 {
     // 读取byte变量
     readResultRender(siemensTcpNet.ReadByteFromPLC(textBox3.Text), textBox3.Text, textBox4);
 }
Пример #3
0
        public bool GetInfo(bool checkPingSuccess, PlcCompany plcCompany, bool isRead, string address, int value, out int output, out string msg)
        {
            output = -1;
            msg    = string.Empty;

            if (checkPingSuccess)
            {
                if (!IsPingSuccess)
                {
                    IsAlive = false;
                    msg     = string.Format("无法连接到【{0}】,IP:{1}", this.Name, this.IP);
                    return(false);
                }
            }

            try
            {
                if (plcCompany == PlcCompany.Mitsubishi)
                {
                    if (isRead)//读
                    {
                        OperateResult <int> result = melsec_net.ReadIntFromPLC(address);
                        if (result.IsSuccess)
                        {
                            output  = result.Content;
                            IsAlive = true;
                            return(true);
                        }
                        else
                        {
                            msg     = string.Format("从{0}中读取数据出现错误,代码:{1}", address, result.ErrorCode);
                            IsAlive = false;
                            return(false);
                        }
                    }
                    else//写
                    {
                        OperateResult result = melsec_net.WriteIntoPLC(address, value);
                        if (result.IsSuccess)
                        {
                            IsAlive = true;
                            return(true);
                        }
                        else
                        {
                            msg     = string.Format("{0} 中写入 {1} 出现错误,代码:{2}", address, value, result.ErrorCode);
                            IsAlive = false;
                            return(false);
                        }
                    }
                }
                else if (plcCompany == PlcCompany.Siemens)
                {
                    if (isRead)//读
                    {
                        OperateResult <byte> result = siemens_net.ReadByteFromPLC(address);
                        if (result.IsSuccess)
                        {
                            output  = result.Content;
                            IsAlive = true;
                            return(true);
                        }
                        else
                        {
                            msg     = string.Format("从{0}中读取数据出现错误,代码:{1}", address, result.ErrorCode);
                            IsAlive = false;
                            return(false);
                        }
                    }
                    else//写
                    {
                        OperateResult result = siemens_net.WriteIntoPLC(address, value);
                        if (result.IsSuccess)
                        {
                            IsAlive = true;
                            return(true);
                        }
                        else
                        {
                            msg     = string.Format("{0} 中写入 {1} 出现错误,代码:{2}", address, value, result.ErrorCode);
                            IsAlive = false;
                            return(false);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msg = string.Format("和{0}通信出现异常!原因:{1}", Name, ex.Message);
            }

            IsAlive = false;
            return(false);
        }