本文已参与[新人创作礼]活动,一起来开启掘金创作之路。 忙了好长时间‘’‘’‘’‘’‘’‘’‘’‘ 主界面做了适当的修改,这样更加显示的好看。 ,画个这样的界面应该没啥。本次我们采用的通讯时西门子S7通讯。这个通讯方式winform有这开发包。 下载下来,就好了。 至于通讯方式:
```csharp #region 与PLC使用S7通讯方式交互的函数 ///
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 ///
}
}
在这还有一些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```