change - 集成HSL PLC读取模块

master
wenjy 1 year ago
parent 10f3db53bf
commit 30661be234

@ -1,4 +1,5 @@
using System;
using Mesnac.PlcUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -16,6 +17,8 @@ namespace Mesnac.Action.ChemicalWeighing.FinishBatch.SaveHelper
public static object senderExtend = null;
private static int WordLength = 16; //定义字长
private static PlcBusiness plcBusiness = PlcBusiness.Instance;
/// <summary>
/// 保存报警信息
/// </summary>
@ -32,21 +35,24 @@ namespace Mesnac.Action.ChemicalWeighing.FinishBatch.SaveHelper
foreach (Entity.AlarmInfo info in _alarmInfoList)
{
Int16[] buff = null;
if (BasePlcHelper.Instance.PlcRead(info.AlarmPLC, info.AlarmBlock, info.Start, info.Length, out buff))
{
foreach(Entity.SimpleAlarmInfo sai in _alarmBlockWordList)
{
if (sai.AlramPLC == info.AlarmPLC && sai.AlarmBlock == info.AlarmBlock) //匹配数据块
{
sai.AlarmData = buff[sai.AlarmAddress - info.Start]; //对起始字偏移,为当前字的报警值
sai.AlarmData = Mesnac.Basic.DataProcessor.Swap(sai.AlarmData); //注意西门子PLC需要高低位转换
}
}
}
else
{
ICSharpCode.Core.LoggingService<AlarmSaveHelper>.Warn(String.Format("从PLC读取报警数据失败:PLC={0},Block={1},Start={2},Len={3}", info.AlarmPLC, info.AlarmBlock, info.Start, info.Length));
}
//if (BasePlcHelper.Instance.PlcRead(info.AlarmPLC, info.AlarmBlock, info.Start, info.Length, out buff))
//{
// foreach(Entity.SimpleAlarmInfo sai in _alarmBlockWordList)
// {
// if (sai.AlramPLC == info.AlarmPLC && sai.AlarmBlock == info.AlarmBlock) //匹配数据块
// {
// sai.AlarmData = buff[sai.AlarmAddress - info.Start]; //对起始字偏移,为当前字的报警值
// sai.AlarmData = Mesnac.Basic.DataProcessor.Swap(sai.AlarmData); //注意西门子PLC需要高低位转换
// }
// }
//}
//else
//{
// ICSharpCode.Core.LoggingService<AlarmSaveHelper>.Warn(String.Format("从PLC读取报警数据失败:PLC={0},Block={1},Start={2},Len={3}", info.AlarmPLC, info.AlarmBlock, info.Start, info.Length));
//}
bool result = plcBusiness.readBoolValue("DB3.DBX2.2");
ICSharpCode.Core.LoggingService<AlarmSaveHelper>.Debug($"DB3.DBX2.2读取结果:{result}");
}
#endregion

@ -729,6 +729,10 @@
<Project>{28acacf1-9936-4e97-a866-f84366ec5286}</Project>
<Name>Mesnac.Basic</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Mesnac.PlcUtils\Mesnac.PlcUtils.csproj">
<Project>{88eac8d1-8783-478c-ad9d-f916673b7004}</Project>
<Name>Mesnac.PlcUtils</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

@ -251,6 +251,10 @@
<Project>{d0c0324f-434e-47eb-8f7c-ba6cd7f233b4}</Project>
<Name>Mesnac.Controls.Feeding</Name>
</ProjectReference>
<ProjectReference Include="..\..\Mesnac.PlcUtils\Mesnac.PlcUtils.csproj">
<Project>{88eac8d1-8783-478c-ad9d-f916673b7004}</Project>
<Name>Mesnac.PlcUtils</Name>
</ProjectReference>
<ProjectReference Include="..\Mensac.Gui.Run.Global\Mesnac.Gui.Run.Global.csproj">
<Project>{CC98CB3B-F532-4510-AC08-7DDBD520CB06}</Project>
<Name>Mesnac.Gui.Run.Global</Name>

@ -11,6 +11,12 @@ namespace MCRun
{
using Mesnac.Core.Service;
using Mesnac.Gui.Workbench;
using Mesnac.PlcUtils;
using Mesnac.PlcUtils.common;
using Mesnac.PlcUtils.enumInfo;
using Mesnac.PlcUtils.Impl;
using System.Drawing.Drawing2D;
class Program
{
[STAThread]
@ -113,6 +119,8 @@ namespace MCRun
};
WorkbenchSingleton.InitializeWorkbench();
PlcBusiness.Instance.InitPlcConnect(PlcType.SiemensPlc, "127.0.0.1", 9600);
ICSharpCode.Core.LoggingService<Program>.Debug("starting workbench...");
Form frmMain = WorkbenchSingleton.Workbench as Form;
@ -146,5 +154,7 @@ namespace MCRun
Application.ExitThread();
Application.Exit();
}
}
}

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.PlcUtils
{
/// <summary>
/// PLC 工具类接口
/// </summary>
public interface IPlc
{
bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
bool Connect(string IP, int port);
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
bool DisConnect();
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
byte[] readValueByAddress(int len, string address);
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
bool writeValueByAddress(int value, string address);
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
bool resetByAddress(string address, int len);
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
string readEaByAddress(string address);
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
int readInteractiveSignal(string address);
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
int readInt32ByAddress(string address);
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeInt32ByAddress(string address, int value);
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
string readStringByAddress(string address, ushort length);
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="SFC"></param>
/// <returns></returns>
bool writeStringByAddress(string address, string value);
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
bool readBoolByAddress(string address);
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
bool writeBoolByAddress(string address, bool value);
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeDoubleByAddress(string address, int value);
}
}

@ -0,0 +1,441 @@
using HslCommunication;
using HslCommunication.Profinet.Omron;
using Mesnac.PlcUtils.common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.PlcUtils.Impl
{
public class OmronNJPlc
{
private StringChange stringChange = StringChange.Instance;
private OmronFinsNet omronFinsNet = null;
public OmronNJPlc()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("HslCommunication 11.0.6.0激活失败...");
return;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("HslCommunication 11.0.6.0激活成功");
this.omronFinsNet = new OmronFinsNet();
this.omronFinsNet.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("欧姆龙NJ系列PLC连接开始");
omronFinsNet.IpAddress = IP;
omronFinsNet.Port = 9600;
omronFinsNet.SA1 = (byte)192;
omronFinsNet.DA1 = (byte)239;
omronFinsNet.DA2 = (byte)0;
try
{
OperateResult connect = omronFinsNet.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("欧姆龙NJ系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("欧姆龙NJ系列PLC建立连接失败");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("欧姆龙NJ系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return omronFinsNet.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("开始通过PLC地址和长度读取PLC数据");
try
{
OperateResult<byte[]> read = omronFinsNet.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = stringChange.ConvertFloatToINt(read.Content);
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过地址和长度读取PLC数据成功{0}", stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("通过地址和长度读取PLC数据失败");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(int value, string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址写入int类型数据", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool resetByAddress(string address, int len)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开发通过PLC地址{0}清零数据", address));
try
{
byte[] write = new byte[len * 2];
for (int i = 0; i < len * 2; i++)
{
write[i] = 0;
}
OperateResult operateResult = omronFinsNet.Write(address, write);
if (operateResult.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}清零数据成功", address));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}清零数据失败!!!", address));
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}清零数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readEaByAddress(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取EA值", address));
try
{
OperateResult<Byte[]> read = omronFinsNet.Read(address, (ushort)(8));
if (read.IsSuccess && read.Content != null)
{
string result = Convert.ToString(read.Content);
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取EA值成功{1}", address, result));
return result;
}
else
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取EA值失败", address));
this.IsConnected = false;
return "";
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取EA值异常", ex);
this.IsConnected = false;
return "";
}
}
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInteractiveSignal(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取交互信号", address));
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取交互信号失败!!!", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取交互信号异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt32ByAddress(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取int32类型数据",address));
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取int32类型数据成功{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取int32类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeInt32ByAddress(string address, int value)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (operateResult.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = omronFinsNet.ReadString(address, length);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取int32类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult write = omronFinsNet.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
}
}

@ -0,0 +1,429 @@
using HslCommunication.Profinet.Siemens;
using HslCommunication;
using Mesnac.PlcUtils.common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.Profinet.Omron;
namespace Mesnac.PlcUtils.Impl
{
internal class SiemensPlc : IPlc
{
private StringChange stringChange = StringChange.Instance;
private const SiemensPLCS type = SiemensPLCS.S200Smart;
private SiemensS7Net s7 = new SiemensS7Net(type);
public SiemensPlc()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("30c15272-3960-4853-9fab-3087392ee5cd"))
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("HslCommunication激活失败");
return;
}
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("西门子S7系列PLC连接开始");
s7.IpAddress = IP;
s7.Port = 102;
try
{
OperateResult connect = s7.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("西门子S7系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("西门子S7系列PLC建立连接失败");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("西门子S7系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("开始通过PLC地址和长度读取PLC数据");
try
{
OperateResult<byte[]> read = s7.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = stringChange.ConvertFloatToINt(read.Content);
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过地址和长度读取PLC数据成功{0}", stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("通过地址和长度读取PLC数据失败");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(int value, string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = s7.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址写入int类型数据", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool resetByAddress(string address, int len)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开发通过PLC地址{0}清零数据", address));
try
{
byte[] write = new byte[len * 2];
for (int i = 0; i < len * 2; i++)
{
write[i] = 0;
}
OperateResult operateResult = s7.Write(address, write);
if (operateResult.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}清零数据成功", address));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}清零数据失败!!!", address));
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}清零数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readEaByAddress(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取EA值", address));
try
{
OperateResult<Byte[]> read = s7.Read(address, (ushort)(8));
if (read.IsSuccess && read.Content != null)
{
string result = Convert.ToString(read.Content);
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取EA值成功{1}", address, result));
return result;
}
else
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取EA值失败", address));
this.IsConnected = false;
return "";
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取EA值异常", ex);
this.IsConnected = false;
return "";
}
}
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInteractiveSignal(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取交互信号", address));
try
{
OperateResult<short> read = s7.ReadInt16(address);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取交互信号失败!!!", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取交互信号异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt32ByAddress(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取int32类型数据",address));
try
{
OperateResult<short> read = s7.ReadInt16(address);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取int32类型数据成功{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取int32类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeInt32ByAddress(string address, int value)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = s7.Write(address, value);
if (operateResult.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = s7.ReadString(address, length);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取int32类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = s7.ReadBool(address);
if (read.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
//ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, short.Parse(stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
public bool DisConnect()
{
return s7.ConnectClose().IsSuccess;
}
public bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult write = s7.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
}
}

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{88EAC8D1-8783-478C-AD9D-F916673B7004}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Mesnac.PlcUtils</RootNamespace>
<AssemblyName>Mesnac.PlcUtils</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="HslCommunication">
<HintPath>Z:\Desktop\日常代码\榄菊项目\程序设计\lj_plc\PlugInPlatform\HslCommunication.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.Core">
<HintPath>Z:\Desktop\日常代码\榄菊项目\程序设计\lj_plc\PlugInPlatform\ICSharpCode.Core.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.Data.Core">
<HintPath>Z:\Desktop\日常代码\榄菊项目\程序设计\lj_plc\PlugInPlatform\ICSharpCode.Data.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="common\EnumExtensions.cs" />
<Compile Include="common\StringChange.cs" />
<Compile Include="enumInfo\PlcType.cs" />
<Compile Include="Impl\OmronNJPlc.cs" />
<Compile Include="Impl\SiemensPlc.cs" />
<Compile Include="IPlc.cs" />
<Compile Include="PlcBusiness.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -0,0 +1,78 @@
using Mesnac.PlcUtils.common;
using Mesnac.PlcUtils.enumInfo;
using Mesnac.PlcUtils.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.PlcUtils
{
public sealed class PlcBusiness
{
private static readonly Lazy<PlcBusiness> lazy = new Lazy<PlcBusiness>(() => new PlcBusiness());
public static PlcBusiness Instance
{
get
{
return lazy.Value;
}
}
private PlcBusiness() { }
private static IPlc plcInstance = null;
public void InitPlcConnect(PlcType plcType, string plcAddress, int plcPort)
{
try
{
string str = System.Environment.CurrentDirectory;
str = str + "\\Mesnac.PlcUtils.dll";
Assembly assembly = Assembly.LoadFile(str); // 加载程序集EXE 或 DLL
string AssemName = "JSSD.PolarBind.Plc.Impl." + plcType.GetDescription();
var obj = assembly.CreateInstance(AssemName, true);
plcInstance = obj as IPlc;
if (plcInstance == null)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("PLC初始化失败");
return;
}
else
{
if (!plcInstance.IsConnected)
{
bool connectResult = plcInstance.Connect(plcAddress, plcPort);
if (connectResult)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Debug("PLC连接成功");
}
else
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error("PLC连接失败");
}
}
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<OmronNJPlc>.Error($"PLC初始化异常{ex.Message}");
}
}
public void Dispose()
{
plcInstance.DisConnect();
}
public bool readBoolValue(string address)
{
return plcInstance.readBoolByAddress(address);
}
}
}

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Mesnac.PlcUtils")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Mesnac.PlcUtils")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("88eac8d1-8783-478c-ad9d-f916673b7004")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.PlcUtils.common
{
public static class EnumExtensions
{
public static string GetDescription<T>(this T enumerationValue) where T : struct
{
var type = enumerationValue.GetType();
if (!type.IsEnum)
{
throw new ArgumentException($"{nameof(enumerationValue)} must be of Enum type", nameof(enumerationValue));
}
var memberInfo = type.GetMember(enumerationValue.ToString());
if (memberInfo.Length > 0)
{
var descriptionAttribute = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (descriptionAttribute != null && descriptionAttribute.Length > 0)
{
return descriptionAttribute[0].Description;
}
}
return enumerationValue.ToString();
}
}
}

@ -0,0 +1,215 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.PlcUtils.common
{
internal sealed class StringChange
{
private static readonly Lazy<StringChange> lazy = new Lazy<StringChange>(() => new StringChange());
public static StringChange Instance
{
get
{
return lazy.Value;
}
}
private StringChange() { }
/// <summary>
/// 将字符串强制转换成int转换失败则返回0
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public int ParseToInt(string str)
{
int returnInt = 0;
if (str == null || str.Trim().Length < 1)
{
return returnInt;
}
if (int.TryParse(str, out returnInt))
{
return returnInt;
}
else
{
return 0;
}
}
/// <summary>
/// char数组转Array
/// </summary>
/// <param name="cha"></param>
/// <param name="len"></param>
/// <returns></returns>
public string CharArrayToString(char[] cha, int len)
{
string str = "";
for (int i = 0; i < len; i++)
{
str += string.Format("{0}", cha[i]);
}
return str;
}
public string bytesToHexStr(byte[] bytes, int iLen)//e.g. { 0x01, 0x01} ---> " 01 01"
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < iLen; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
public byte[] HexStrTorbytes(string strHex)//e.g. " 01 01" ---> { 0x01, 0x01}
{
strHex = strHex.Replace(" ", "");
if ((strHex.Length % 2) != 0)
strHex += " ";
byte[] returnBytes = new byte[strHex.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(strHex.Substring(i * 2, 2), 16);
return returnBytes;
}
public string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s); //按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++) //逐字节变为16进制字符以%隔开
{
result += "%" + Convert.ToString(b[i], 16);
}
return result;
}
public string HexStringToString(string hs, Encoding encode)
{
//以%分割字符串,并去掉空字符
string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
byte[] b = new byte[chars.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < chars.Length; i++)
{
b[i] = Convert.ToByte(chars[i], 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}
public byte[] Swap16Bytes(byte[] OldU16)
{
byte[] ReturnBytes = new byte[2];
ReturnBytes[1] = OldU16[0];
ReturnBytes[0] = OldU16[1];
return ReturnBytes;
}
/// <param name="strbase64">64Base码</param>
/// <param name="path">保存路径</param>
/// <param name="filename">文件名称</param>
/// <returns></returns>
public bool Base64ToImage(string strbase64, string path, string filename)
{
bool Flag = false;
try
{
//base64编码的文本 转为 图片
//图片名称
byte[] arr = Convert.FromBase64String(strbase64);//将指定的字符串(它将二进制数据编码为 Base64 数字)转换为等效的 8 位无符号整数数组。
using (MemoryStream ms = new MemoryStream(arr))
{
Bitmap bmp = new Bitmap(ms);//加载图像
if (!Directory.Exists(path))//判断保存目录是否存在
{
Directory.CreateDirectory(path);
}
bmp.Save((path + "\\" + filename + ".png"), System.Drawing.Imaging.ImageFormat.Png);//将图片以JPEG格式保存在指定目录(可以选择其他图片格式)
ms.Close();//关闭流并释放
if (File.Exists(path + "\\" + filename + ".png"))//判断是否存在
{
Flag = true;
}
}
}
catch (Exception ex)
{
Console.WriteLine("图片保存失败:" + ex.Message);
}
return Flag;
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public long GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
public byte[] ConvertFloatToINt(byte[] floatBytes)
{
byte[] intBytes = new byte[floatBytes.Length / 2];
for (int i = 0; i < intBytes.Length; i++)
{
intBytes[i] = floatBytes[i * 2 + 1];
}
return intBytes;
}
//CRC异或校验
public byte CalculateVerify(byte[] pMessage, int iLength)
{
UInt16 i;
byte iVerify = 0;
iVerify = pMessage[0];
for (i = 1; i < iLength; i++)
{
iVerify = (byte)(iVerify ^ pMessage[i]);
}
return iVerify;
}
public int HexStringToNegative(string strNumber)
{
int iNegate = 0;
int iNumber = Convert.ToInt32(strNumber, 16);
if (iNumber > 127)
{
int iComplement = iNumber - 1;
string strNegate = string.Empty;
char[] binchar = Convert.ToString(iComplement, 2).PadLeft(8, '0').ToArray();
foreach (char ch in binchar)
{
if (Convert.ToInt32(ch) == 48)
{
strNegate += "1";
}
else
{
strNegate += "0";
}
}
iNegate = -Convert.ToInt32(strNegate, 2);
}
return iNegate;
}
}
}

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mesnac.PlcUtils.enumInfo
{
public enum PlcType
{
/// <summary>
/// 欧姆龙Nj
/// </summary>
[Description("OmronNJPlc")]
OmronNJPlc,
/// <summary>
/// 西门子S7
/// </summary>
[Description("SiemensPlc")]
SiemensPlc,
}
}

@ -1,10 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31515.178
# Visual Studio Version 17
VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PlugInPlatform", "PlugInPlatform", "{1D5422C6-0466-40F5-9B79-4D3470A1765D}"
ProjectSection(SolutionItems) = preProject
Z:\Desktop\日常代码\HighWayIot\HighWayIot.Plc\bin\Debug\HslCommunication.dll = Z:\Desktop\日常代码\HighWayIot\HighWayIot.Plc\bin\Debug\HslCommunication.dll
PlugInPlatform\ICSharpCode.Core.dll = PlugInPlatform\ICSharpCode.Core.dll
PlugInPlatform\ICSharpCode.Data.Core.dll = PlugInPlatform\ICSharpCode.Data.Core.dll
PlugInPlatform\Mesnac.Core.dll = PlugInPlatform\Mesnac.Core.dll
@ -157,6 +158,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mesnac.Controls.Feeding", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mesnac.Action.Default", "Actions\Default\Mesnac.Action.Default\Mesnac.Action.Default.csproj", "{6A2190C6-B530-4D5B-BD34-29EFB0431F67}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PlcUtils", "PlcUtils", "{DE8C4FC3-17D6-4217-8AB1-72ADE8D85810}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mesnac.PlcUtils", "Mesnac.PlcUtils\Mesnac.PlcUtils.csproj", "{88EAC8D1-8783-478C-AD9D-F916673B7004}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -311,6 +316,18 @@ Global
{6A2190C6-B530-4D5B-BD34-29EFB0431F67}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{6A2190C6-B530-4D5B-BD34-29EFB0431F67}.Release|x86.ActiveCfg = Release|Any CPU
{6A2190C6-B530-4D5B-BD34-29EFB0431F67}.Release|x86.Build.0 = Release|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Debug|x86.ActiveCfg = Debug|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Debug|x86.Build.0 = Debug|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Release|Any CPU.Build.0 = Release|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Release|x86.ActiveCfg = Release|Any CPU
{88EAC8D1-8783-478C-AD9D-F916673B7004}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -339,6 +356,7 @@ Global
{91C803A6-90D1-4361-8B92-9AFD9E766411} = {8314C6B1-6D76-4441-85DB-562354BA4578}
{D0C0324F-434E-47EB-8F7C-BA6CD7F233B4} = {DE938080-4A00-4686-9CE4-1C33FCA86346}
{6A2190C6-B530-4D5B-BD34-29EFB0431F67} = {66F3B234-E56C-4EF4-AB6D-BB6D91467855}
{88EAC8D1-8783-478C-AD9D-F916673B7004} = {DE8C4FC3-17D6-4217-8AB1-72ADE8D85810}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {125BF7F5-E6EF-487C-B939-0631965DB3C1}

Loading…
Cancel
Save