winform 连接plc成功,但读不到数据?

请问各位,想winform连接西门子s71500,写了一个类库,在form中调用这个类库,在timer中调用这个类的read读方法,传递的参数就form的lable的text //地址,和textbox的text//输出到这里, 显示连接成功plc,但读不到数据,是空的。
如果把plc 的new和open直接写到窗体中,参数直接赋值,都是可以读到的,但问题是,项目有好几个窗体,其他窗体都读不到。
哪位给看一下,谢谢了。

新建一个类库,在form引用这个库 方法全部写成静态的
public class Class1
{
public static Plc plc = new Plc( CpuType.S71200, "192.168.0.11", 0, 1);
public static void openplc()
{
try
{
plc.Open();
}
catch (Exception)
{
MessageBox.Show("不能连接,请开plc电源或者检查网线");

        }
    }
    public static void closeplc()
    {
        plc.Close();
    }
    public static void read(string addr,string result)   //labletext地址,输出到textbox的text
    {

        
         try
            {
                string[] arr = (addr.ToUpper()).Split('.');
                string valuetype = arr[1].Substring(0, 3);
                                                          
                                                         
                if (valuetype == "DBX")
                {
                    bool test1 = (bool)plc.Read(addr.ToUpper());
                    result=test1.ToString();
                   
                }

                else if (valuetype == "DBW")
                {
                    short test2 = ((ushort)plc.Read(addr.ToUpper())).ConvertToShort();
                   result=test2.ToString();
                }

                else if (valuetype == "DBD")
                {
                    double test3 = ((uint)plc.Read(addr.ToUpper())).ConvertToFloat();
                    result=test3.ToString();
                }

                
            }
            catch (Exception Ex)
            {
               
            }
        }

在form的timer中调用读的方法
private void timer1_Tick(object sender, EventArgs e)
{
Class1. read(this.Lable.Text,this.TextBox.Text)
}

题主要设置textbox的值,给read函数返回result的值就行了,然后设置textbox的text属性,要不题主只是设置传入的string变量的值,这不会显示在textbox中的,改下面这样


    public class Class1
    {
        public static Plc plc = new Plc(CpuType.S71200, "192.168.0.11", 0, 1);
        public static void openplc()
        {
            try
            {
                plc.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("不能连接,请开plc电源或者检查网线");

            }
        }
        public static void closeplc()
        {
            plc.Close();
        }
        public static string read(string addr)   //labletext地址,输出到textbox的text
        {
            string result = null;

            try
            {
                string[] arr = (addr.ToUpper()).Split('.');
                string valuetype = arr[1].Substring(0, 3);


                if (valuetype == "DBX")
                {
                    bool test1 = (bool)plc.Read(addr.ToUpper());
                    result = test1.ToString();

                }

                else if (valuetype == "DBW")
                {
                    short test2 = ((ushort)plc.Read(addr.ToUpper())).ConvertToShort();
                    result = test2.ToString();
                }

                else if (valuetype == "DBD")
                {
                    double test3 = ((uint)plc.Read(addr.ToUpper())).ConvertToFloat();
                    result = test3.ToString();
                }


            }
            catch (Exception Ex)
            {
                result = Ex.Message;
            }
            return result;////////////
        }
    }

    //这样调用this.TextBox.Text=Class1. read(this.Lable.Text);

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632