初始化上传
This commit is contained in:
218
常用工具集/ViewModels/01PLC通信调试/欧姆龙Fins调试ViewModel.cs
Normal file
218
常用工具集/ViewModels/01PLC通信调试/欧姆龙Fins调试ViewModel.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using 常用工具集.Base;
|
||||
using OmronLib;
|
||||
using MES.Utility.Core;
|
||||
using Ursa.Controls;
|
||||
|
||||
namespace 常用工具集.ViewModel._01PLC通信调试
|
||||
{
|
||||
public class 欧姆龙Fins调试ViewModel : ViewModelBase
|
||||
{
|
||||
private Omron omron;
|
||||
public bool Enabled1 { get; set; } = true;
|
||||
public bool Enabled2 { get; set; } = false;
|
||||
public string IpAddress { get; set; } = "192.168.1.3";
|
||||
public int Port { get; set; } = 9600;
|
||||
|
||||
public List<string> AreaList { get; set; }
|
||||
public List<Omron.AreaType> AreaTypeList { get; set; }
|
||||
public int WriteAreaIndex { get; set; } = 0;
|
||||
public int ReadAreaIndex { get; set; } = 0;
|
||||
public int ReadAddress { get; set; } = 1042;
|
||||
public int WriteAddress { get; set; } = 1042;
|
||||
public int ReadCount { get; set; } = 1;
|
||||
public string ReadResult { get; set; } = "";
|
||||
|
||||
public int StrLength { get; set; } = 50;
|
||||
public string WriteValue { get; set; } = "";
|
||||
public DelegateCommand ConnectCmd { get; set; }
|
||||
public DelegateCommand DisconnectCmd { get; set; }
|
||||
public DelegateCommand ReadCmd { get; set; }//1读取 2字符串 3Float 4Short
|
||||
public DelegateCommand WriteCmd { get; set; }//1写入 2.Float 3Short 4字符串
|
||||
|
||||
public 欧姆龙Fins调试ViewModel()
|
||||
{
|
||||
AreaList = new List<string>
|
||||
{
|
||||
"CIO_Bit","WR_Bit","HR_Bit","AR_Bit",
|
||||
"DM_Bit","CIO_Word","WR_Word","HR_Word","AR_Word","DM_Word"
|
||||
};
|
||||
AreaTypeList = new List<Omron.AreaType>
|
||||
{
|
||||
Omron.AreaType.CIO_Bit,Omron.AreaType.WR_Bit,Omron.AreaType.HR_Bit,Omron.AreaType.AR_Bit,
|
||||
Omron.AreaType.DM_Bit,Omron.AreaType.CIO_Word,Omron.AreaType.WR_Word,Omron.AreaType.HR_Word,Omron.AreaType.AR_Word,Omron.AreaType.DM_Word
|
||||
};
|
||||
ReadAreaIndex = 9;
|
||||
WriteAreaIndex = 9;
|
||||
ConnectCmd = new DelegateCommand(ConnectCmdFunc);
|
||||
DisconnectCmd = new DelegateCommand(DisconnectCmdFunc);
|
||||
ReadCmd = new DelegateCommand(ReadCmdFunc);
|
||||
WriteCmd = new DelegateCommand(WriteCmdFunc);
|
||||
}
|
||||
|
||||
private void ConnectCmdFunc(object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
omron = new Omron();
|
||||
omron.IPAddr = System.Net.IPAddress.Parse(IpAddress);
|
||||
omron.Port = Port;
|
||||
string info;
|
||||
if (!omron.PlcConnect(out info))
|
||||
{
|
||||
GlobalValues.Error($"连接失败");
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalValues.Success("连接成功");
|
||||
Enabled1 = false;
|
||||
Enabled2 = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
GlobalValues.Error($"连接失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void DisconnectCmdFunc(object obj)
|
||||
{
|
||||
omron.DisConnect();
|
||||
omron = null;
|
||||
Enabled1 = true;
|
||||
Enabled2 = false;
|
||||
}
|
||||
|
||||
private void ReadCmdFunc(object obj)
|
||||
{
|
||||
string cmd = obj.ToString();
|
||||
if (cmd == "1")
|
||||
{
|
||||
string[] res;
|
||||
bool isSucess = omron.Read(AreaTypeList[ReadAreaIndex], ReadAddress, 0, ReadCount, out res);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("读取失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
ReadResult = res.ToList().GetStrArray(" ");
|
||||
}
|
||||
else if (cmd == "2")
|
||||
{
|
||||
string res;
|
||||
bool isSucess = omron.ReadString(ReadAddress, ReadCount, out res);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("读取失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
ReadResult = res;
|
||||
}
|
||||
else if (cmd == "3")
|
||||
{
|
||||
float[] res;
|
||||
bool isSucess = omron.ReadFloats(ReadAddress, ReadCount, out res);
|
||||
if (!isSucess)
|
||||
{
|
||||
|
||||
GlobalValues.Error("读取失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
ReadResult = res.Select(it => Convert.ToString(it)).ToList().GetStrArray(" ");
|
||||
}
|
||||
else if (cmd == "4")
|
||||
{
|
||||
short[] res;
|
||||
bool isSucess = omron.ReadShorts(ReadAddress, ReadCount, out res);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("读取失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
ReadResult = res.Select(it => Convert.ToString(it)).ToList().GetStrArray(" ");
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteCmdFunc(object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
//1写入 2.Float 3Short 4字符串
|
||||
string cmd = obj.ToString();
|
||||
if (cmd == "1")
|
||||
{
|
||||
int[] intValues = WriteValue.Split(' ').Where(it => !it.IsNullOrEmpty()).Select(it => Convert.ToInt32(it)).ToArray();
|
||||
if (WriteAreaIndex < 5)
|
||||
{
|
||||
bool[] values = intValues.Select(it => it == 1 ? true : false).ToArray();
|
||||
bool isSucess = omron.WriteBits(AreaTypeList[WriteAreaIndex], WriteAddress, 0, values.Length, values);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("写入失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
GlobalValues.Success("写入成功");
|
||||
}
|
||||
else
|
||||
{
|
||||
bool isSucess = omron.WriteWords(AreaTypeList[WriteAreaIndex], WriteAddress, intValues.Length, intValues);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("写入失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
GlobalValues.Success("写入成功");
|
||||
}
|
||||
|
||||
}
|
||||
else if (cmd == "2")
|
||||
{
|
||||
float[] floatValues = WriteValue.Split(' ').Where(it => !it.IsNullOrEmpty()).Select(it => Convert.ToSingle(it)).ToArray();
|
||||
bool isSucess = omron.WriteFloat(WriteAddress, floatValues);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("写入失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
GlobalValues.Success("写入成功");
|
||||
}
|
||||
else if (cmd == "3")
|
||||
{
|
||||
short[] floatValues = WriteValue.Split(' ').Where(it => !it.IsNullOrEmpty()).Select(it => Convert.ToInt16(it)).ToArray();
|
||||
bool isSucess = omron.WriteShort(WriteAddress, floatValues);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("写入失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
GlobalValues.Success("写入成功");
|
||||
}
|
||||
else if (cmd == "4")
|
||||
{
|
||||
bool isSucess = omron.WriteString(WriteAddress, WriteValue, StrLength);
|
||||
if (!isSucess)
|
||||
{
|
||||
GlobalValues.Error("写入失败");
|
||||
DisconnectCmdFunc(obj);
|
||||
return;
|
||||
}
|
||||
GlobalValues.Success("写入成功");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.ShowAsync($"操作异常:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user