You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
5.7 KiB
C#
142 lines
5.7 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using ZJ_BYD.Untils;
|
|
|
|
namespace ZJ_BYD.Common
|
|
{
|
|
public class PlcHelper
|
|
{
|
|
/// <summary>
|
|
/// 获取PLC值
|
|
/// </summary>
|
|
/// <param name="dataType"></param>
|
|
/// <param name="address"></param>
|
|
/// <param name="length"></param>
|
|
/// <returns></returns>
|
|
public static (bool ok, string val) GetPlcVal(string dataType, string address, string length = "40")
|
|
{
|
|
var strVal = string.Empty;
|
|
try
|
|
{
|
|
|
|
switch (dataType)
|
|
{
|
|
case "bool":
|
|
if (Program.siemensS7Net == null)
|
|
{
|
|
Program.communicationstatus = "异常";
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var operateBoolResult = Program.siemensS7Net.ReadBool(address);
|
|
if (!operateBoolResult.IsSuccess)
|
|
{
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var boolVal = operateBoolResult.Content;
|
|
strVal = boolVal.ToString();
|
|
return (true, strVal);
|
|
}
|
|
}
|
|
case "short":
|
|
if (Program.siemensS7Net == null)
|
|
{
|
|
Program.communicationstatus = "异常";
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var operateShortResult = Program.siemensS7Net.ReadInt16(address);
|
|
if (!operateShortResult.IsSuccess)
|
|
{
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var shortVal = operateShortResult.Content;
|
|
strVal = shortVal.ToString();
|
|
return (true, strVal);
|
|
}
|
|
}
|
|
case "ushort":
|
|
if (Program.siemensS7Net == null)
|
|
{
|
|
Program.communicationstatus = "异常";
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var operateShortResult = Program.siemensS7Net.ReadUInt16(address);
|
|
if (!operateShortResult.IsSuccess)
|
|
{
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var shortVal = operateShortResult.Content;
|
|
strVal = shortVal.ToString();
|
|
return (true, strVal);
|
|
}
|
|
}
|
|
case "float":
|
|
if (Program.siemensS7Net == null)
|
|
{
|
|
Program.communicationstatus = "异常";
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var operateFloatResult = Program.siemensS7Net.ReadFloat(address);
|
|
if (!operateFloatResult.IsSuccess)
|
|
{
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var floatVal = operateFloatResult.Content;
|
|
strVal = floatVal.ToString("f3");
|
|
return (true, strVal);
|
|
}
|
|
}
|
|
case "string":
|
|
if (Program.siemensS7Net == null)
|
|
{
|
|
Program.communicationstatus = "异常";
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
var operateFloatResult = Program.siemensS7Net.ReadString(address, ushort.Parse(length), Encoding.UTF8);
|
|
if (!operateFloatResult.IsSuccess)
|
|
{
|
|
return (false, strVal);
|
|
}
|
|
else
|
|
{
|
|
strVal = operateFloatResult.Content;
|
|
if (!string.IsNullOrEmpty(strVal))
|
|
{
|
|
strVal = strVal.Substring(2, strVal.Length - 2).Trim();
|
|
}
|
|
return (true, strVal);
|
|
}
|
|
}
|
|
default:
|
|
LogHelper.WriteLog("未知数据类型!");
|
|
return (false, strVal);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var errMsg = ex == null ? "未知异常" : ex.Message;
|
|
MessageBox.Show($"GetPlcVal方法出发异常:{errMsg},请联系管理员!");
|
|
return (false, strVal);
|
|
}
|
|
}
|
|
}
|
|
}
|