手把手教你开发 二

本文已参与[新人创作礼]活动,一起来开启掘金创作之路。 忙了好长时间‘’‘’‘’‘’‘’‘’‘’‘ 在这里插入图片描述主界面做了适当的修改,这样更加显示的好看。 ,画个这样的界面应该没啥。本次我们采用的通讯时西门子S7通讯。这个通讯方式winform有这开发包。在这里插入图片描述 下载下来,就好了。 至于通讯方式:

```csharp #region 与PLC使用S7通讯方式交互的函数 ///

/// 链接PLC /// /// IP地址 /// PLC的型号 /// PLC的机台号 /// PLC的查抄号 private void ClickPLC(string ip ,string xinghao ,string jitaihao ,string chacaohao ) { string IPText = ip; string XHText = xinghao; string JTHText = jitaihao; string CCHText = chacaohao; try { CpuType cpuType = (CpuType)Enum.Parse(typeof(CpuType), XHText, true);

myplc = new Plc(cpuType, IPText, short.Parse(JTHText), short.Parse(CCHText));

            myplc.Open();

            if (myplc.IsConnected == false)
            {


                UINotifier.Show("PLC链接失败", UINotifierType.ERROR, "错误:", false, 0, null, null);
                Footer.FillColor = Color.Red;
                return;
            }
            else
            {
                UINotifier.Show("PLC链接成功", UINotifierType.OK, "提示:", true, 0, null, null);
                Footer.FillColor = Color.Green;
            }
        }
        catch 
        {
           UINotifier.Show("PLC链接失败", UINotifierType.ERROR, "错误:", false, 0, null, null);
            Footer.FillColor = Color.Red;
        }
    }

    /// <summary>
    /// PLC断开连接
    /// </summary>
    private void CloesdPlc()
    {
        try
        {
            if (myplc != null)
            {
                myplc.Close();
                UINotifier.Show("PLC断开成功", UINotifierType.ERROR, "提示:", true, 0, null, null);
            }
            else
            {
                UINotifier.Show("PLC无链接", UINotifierType.ERROR, "提示:", true, 0, null, null);
            }
        }
        catch (Exception ex)
        {

            UIMessageTip.ShowOk(ex.Message);
        }



    }

    /// <summary>
    /// 读取DB数据
    /// </summary>
    /// <param name="dBShuJu">DB数据块的位置</param>
    /// <param name="startDBShuJu">DB数据的起始位置</param>
    /// <param name="dBCount">读取的DB的长度</param>
    private string  ReadDB (int dBShuJu , int startDBShuJu , int dBCount)
    {
        string PLCDB = "";
        try
        {
             PLCDB = (myplc.Read(DataType.DataBlock, dBShuJu, startDBShuJu, VarType.String, dBCount)).ToString();

        }
        catch (Exception ex)
        {

            UIMessageTip.ShowError(ex.ToString ());
        }

        return PLCDB;
    }
    /// <summary>
    /// 根据DB名称读取数据
    /// </summary>
    /// <param name="dBMingZi">"DB1.DBW0"DB块的变量的名字</param>
    private string  ReadDB(string dBMingZi)
    {
        string result = "";
        try
        {
            //result = (myplc.Read("DB1.DBW0")).ToString();
            result = (myplc.Read(dBMingZi)).ToString();
        }
        catch (Exception ex)
        {
            UIMessageTip.ShowError(ex.ToString());
        }
        return result;
    }

    /// <summary>
    /// 写入DB数据
    /// </summary>
    /// <param name="db">底部位置</param>
    /// <param name="startByteAdr">开始的位置</param>
    /// <param name="value">数据信息</param>
    private void WriteDB(int db, int startByteAdr, object value)
    {
        try
        {
            myplc.Write(DataType.DataBlock, db, startByteAdr, value);
        }
        catch (Exception)
        {

            throw;
        }

    }
    /// <summary>
    /// 写数据
    /// </summary>
    /// <param name="DB">DB变量  "DB1.DBW0"</param>
    /// <param name="val">写入的值</param>
    private void WriteDB(string DB, string val) 
    {
        try
        {
            myplc.Write(DB, val);
        }
        catch (Exception)
        {

            throw;
        }

    }

    #endregion

``` 我在里面加了个表,用来实时判断链接状态。注意:本程序的BUG应该是会有的,我们后续如果有BUG也会做个文章慢慢来解决。

```csharp ///

/// 运行中检测链接信号 /// /// /// private void timer1_Tick(object sender, EventArgs e) { try { if (myplc.IsConnected == false) { ClickPLC(IP, PLCxinghao, jitai, chachaohao); } } catch (Exception) {

}


    }

在这还有一些INI和文件夹的操作。 csharp

#region 文件和文件夹的操作

    /// <summary>
    /// 判断文件夹是否存在,如果不在则建立新的文件夹
    /// </summary>
    /// <param name="visionPath"></param>
    private void WenJianJia(string visionPath )
    {
        //spath:文件夹路径名

        if (Directory.Exists(visionPath))
        {

        }
        else
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(visionPath);
            directoryInfo.Create();
        }
    }
    /// <summary>
    /// 判断文件是否存在
    /// </summary>
    /// <param name="wenjianPath"></param>
    private void WenJian(string wenjianPath)
    {
        // filePath 文件路径名

        if (!File.Exists(wenjianPath))
        {
            //MessageBox.Show(filePath + "  not exists!");
            FileStream fs = File.Create(wenjianPath);//创建文件
            fs.Close();
            return;
        }

    }
    /// <summary>
    /// 写入ini文件中的数据
    /// </summary>
    /// <param name="iniPath">ini路径</param>
    /// <param name="biaoshi">标识</param>
    /// <param name="key">建</param>
    /// <param name="valur">值</param>
    private void INIWrite(string iniPath ,string  biaoshi ,string key,string valur  )
    {
        try
        {
            WenJian(iniPath);
            IniFile ini = new IniFile(iniPath);
            ini.Write(biaoshi, key, valur);
            // ini.Write("Setup", "Age", 18);
            ini.UpdateFile();
        }
        catch (Exception)
        {

            UIMessageTip.ShowError("配置文件写入失败");
        }

    }
    /// <summary>
    /// 读取ini的数据
    /// </summary>
    /// <param name="inipath"></param>
    /// <param name="biaoshi"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    private string INIRead(string inipath,string biaoshi ,string key )
    {
        string name = "";
        try
        {
            WenJian(inipath);
            IniFile ini = new IniFile(inipath);
             name = ini.ReadString(biaoshi , key, "");
            // int age = ini.ReadInt("Setup", "Age", 0);

        }
        catch (Exception)
        {
            UIMessageTip.ShowError("配置文件读取失败");
        }
        return name;
    }

    #endregion```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋小童

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值