Ejemplo n.º 1
0
        private void userButton22_Click(object sender, EventArgs e)
        {
            bool   M100         = melsec_net.ReadBoolFromPLC("M100").Content;        // 读取M100是否通,十进制地址
            bool   X1A0         = melsec_net.ReadBoolFromPLC("X1A0").Content;        // 读取X1A0是否通,十六进制地址
            bool   Y1A0         = melsec_net.ReadBoolFromPLC("Y1A0").Content;        // 读取Y1A0是否通,十六进制地址
            bool   B1A0         = melsec_net.ReadBoolFromPLC("B1A0").Content;        // 读取B1A0是否通,十六进制地址
            short  short_D1000  = melsec_net.ReadShortFromPLC("D1000").Content;      // 读取D1000的short值  ,W3C0,R3C0 效果是一样的
            ushort ushort_D1000 = melsec_net.ReadUShortFromPLC("D1000").Content;     // 读取D1000的ushort值
            int    int_D1000    = melsec_net.ReadIntFromPLC("D1000").Content;        // 读取D1000-D1001组成的int数据
            uint   uint_D1000   = melsec_net.ReadUIntFromPLC("D1000").Content;       // 读取D1000-D1001组成的uint数据
            float  float_D1000  = melsec_net.ReadFloatFromPLC("D1000").Content;      // 读取D1000-D1001组成的float数据
            long   long_D1000   = melsec_net.ReadLongFromPLC("D1000").Content;       // 读取D1000-D1003组成的long数据
            double double_D1000 = melsec_net.ReadDoubleFromPLC("D1000").Content;     // 读取D1000-D1003组成的double数据
            string str_D1000    = melsec_net.ReadStringFromPLC("D1000", 10).Content; // 读取D1000-D1009组成的条码数据



            melsec_net.WriteIntoPLC("M100", true);                        // 写入M100为通
            melsec_net.WriteIntoPLC("Y1A0", true);                        // 写入Y1A0为通
            melsec_net.WriteIntoPLC("X1A0", true);                        // 写入X1A0为通
            melsec_net.WriteIntoPLC("B1A0", true);                        // 写入B1A0为通
            melsec_net.WriteIntoPLC("D1000", (short)1234);                // 写入D1000  short值  ,W3C0,R3C0 效果是一样的
            melsec_net.WriteIntoPLC("D1000", (ushort)45678);              // 写入D1000  ushort值
            melsec_net.WriteIntoPLC("D1000", 1234566);                    // 写入D1000  int值
            melsec_net.WriteIntoPLC("D1000", (uint)1234566);              // 写入D1000  uint值
            melsec_net.WriteIntoPLC("D1000", 123.456f);                   // 写入D1000  float值
            melsec_net.WriteIntoPLC("D1000", 123.456d);                   // 写入D1000  double值
            melsec_net.WriteIntoPLC("D1000", 123456661235123534L);        // 写入D1000  long值
            melsec_net.WriteAsciiStringIntoPLC("D1000", "K123456789");    // 写入D1000  string值
        }
Ejemplo n.º 2
0
 private void button_read_ushort_Click(object sender, EventArgs e)
 {
     // 读取ushort变量
     readResultRender(melsec_net.ReadUShortFromPLC(textBox3.Text), textBox3.Text, textBox4);
 }
Ejemplo n.º 3
0
        public bool GetInfo(bool checkPingSuccess, PlcCompany plcCompany, bool isRead, string address, ushort value, out ushort output, out string msg)
        {
            output = 0;
            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 <ushort> result = melsec_net.ReadUShortFromPLC(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)//读
                    {
                        HslCommunice533.OperateResult <ushort> result = siemensS7Net.ReadUInt16(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//写
                    {
                        HslCommunice533.OperateResult result = siemensS7Net.Write(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);
        }