fangli1119 2022-02-28 18:01 采纳率: 33.3%
浏览 57
已结题

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)
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-02-28 20:56
    关注

    题主要设置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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 有问必答小助手 2022-03-01 20:19
    关注
    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
    PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月9日
  • 已采纳回答 3月1日
  • 创建了问题 2月28日

悬赏问题

  • ¥15 求局部放电案例库,用于预测局部放电类型
  • ¥100 QT Open62541
  • ¥15 stata合并季度数据和日度数据
  • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应
  • ¥15 Vue3 中使用 `vue-router` 只能跳转到主页面?
  • ¥15 用QT,进行QGIS二次开发,如何在添加栅格图层时,将黑白的矢量图渲染成彩色
  • ¥50 监控摄像头 乐橙和家亲版 保存sd卡的文件怎么打开?视频怎么播放?
  • ¥15 Python的Py-QT扩展库开发GUI
  • ¥60 提问一下周期性信信号的问题
  • ¥15 jtag连接不上fpga怎么办