Commit 928036f2 张东亮

1

1 个父辈 b3aef1cd
正在显示 37 个修改的文件 包含 1469 行增加69 行删除
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 17
VisualStudioVersion = 16.0.30225.117 VisualStudioVersion = 17.3.32811.315
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "source\Common\Common.csproj", "{43CDD09E-FCF3-4960-A01D-3BBFE9933122}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "source\Common\Common.csproj", "{43CDD09E-FCF3-4960-A01D-3BBFE9933122}"
EndProject EndProject
...@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoadCSVLibrary", "source\Lo ...@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoadCSVLibrary", "source\Lo
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeederLineClient", "source\FeederLineClient\FeederLineClient.csproj", "{0D2542F5-DD62-4352-82D0-383D9A045E74}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeederLineClient", "source\FeederLineClient\FeederLineClient.csproj", "{0D2542F5-DD62-4352-82D0-383D9A045E74}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DL.IOModule", "source\DL.IOModule\DL.IOModule.csproj", "{F85A7412-B5B3-4291-A448-A10564602E1A}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
...@@ -39,6 +41,10 @@ Global ...@@ -39,6 +41,10 @@ Global
{0D2542F5-DD62-4352-82D0-383D9A045E74}.Debug|Any CPU.Build.0 = Debug|Any CPU {0D2542F5-DD62-4352-82D0-383D9A045E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D2542F5-DD62-4352-82D0-383D9A045E74}.Release|Any CPU.ActiveCfg = Release|Any CPU {0D2542F5-DD62-4352-82D0-383D9A045E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D2542F5-DD62-4352-82D0-383D9A045E74}.Release|Any CPU.Build.0 = Release|Any CPU {0D2542F5-DD62-4352-82D0-383D9A045E74}.Release|Any CPU.Build.0 = Release|Any CPU
{F85A7412-B5B3-4291-A448-A10564602E1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F85A7412-B5B3-4291-A448-A10564602E1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F85A7412-B5B3-4291-A448-A10564602E1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F85A7412-B5B3-4291-A448-A10564602E1A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL"> <Reference Include="log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\dll\log4net.dll</HintPath> <HintPath>..\..\dll\log4net.dll</HintPath>
</Reference> </Reference>
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
...@@ -66,6 +67,9 @@ ...@@ -66,6 +67,9 @@
<ItemGroup> <ItemGroup>
<WCFMetadata Include="Service References\" /> <WCFMetadata Include="Service References\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.15" targetFramework="net40" />
</packages>
\ No newline at end of file \ No newline at end of file
...@@ -13,13 +13,13 @@ namespace OnlineStore.Common ...@@ -13,13 +13,13 @@ namespace OnlineStore.Common
{ {
public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog"); public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog");
private static LogUtil instance = new LogUtil(); private static LogUtil instance = new LogUtil();
public delegate void ShowLog(string msg, Color color); public delegate void ShowLog(string msg, Color color);
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>(); public static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>();
public static System.Windows.Forms.RichTextBox logBox = null; public static System.Windows.Forms.RichTextBox logBox = null;
public static int showCount = 20; public static int showCount = 20;
public static bool debug_opened = false; public static bool debug_opened = false;
public static void info(ILog log, string msg) public static void info(ILog log, string msg)
{ {
if (log == null) if (log == null)
...@@ -50,7 +50,16 @@ namespace OnlineStore.Common ...@@ -50,7 +50,16 @@ namespace OnlineStore.Common
{ {
AddToBox(msg, Color.Gray); AddToBox(msg, Color.Gray);
} }
} }
public static void error(ILog log, string errorMsg)
{
if (errorMsg.Trim().Equals(""))
{
return;
}
log.Error(errorMsg);
AddToBox(errorMsg, Color.Red);
}
public static void error(string errorMsg, int type) public static void error(string errorMsg, int type)
{ {
if (lastErrorLogTime.ContainsKey(type)) if (lastErrorLogTime.ContainsKey(type))
...@@ -69,7 +78,8 @@ namespace OnlineStore.Common ...@@ -69,7 +78,8 @@ namespace OnlineStore.Common
error(LOGGER, errorMsg); error(LOGGER, errorMsg);
} }
} }
public static void error(ILog log, string errorMsg, Exception ex = null)
public static void error(ILog log, string errorMsg, Exception ex)
{ {
if (errorMsg.Trim().Equals("")) if (errorMsg.Trim().Equals(""))
{ {
...@@ -180,6 +190,6 @@ namespace OnlineStore.Common ...@@ -180,6 +190,6 @@ namespace OnlineStore.Common
public static void info(string msg) public static void info(string msg)
{ {
info(LOGGER, msg); info(LOGGER, msg);
} }
} }
} }
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace DL.Com
{
public class ComHelper
{
static UdpClient broadcastClient; //广播客户端
static IPEndPoint broadcastEndPoint; //广播远程节点
static byte[] broadcastBuffer; //远程返回数据
/// <summary>
/// 自动获取IP地址,未连接前使用,必须在同一网段
/// </summary>
/// <param name="localIP">本地IP地址</param>
/// <returns></returns>
public static bool AutoIP(string localIP,out string ip)
{
ip = "";
try
{
IPEndPoint local = new IPEndPoint(IPAddress.Parse(localIP), 55654);
broadcastClient = new UdpClient(local);
broadcastBuffer = null;
Thread tTemp = new Thread(new ThreadStart(GetIP));
tTemp.Start();
broadcastEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1024);
byte[] dgram = new byte[] { 0x05, 0x00, 0x07, 0x00, 0x00, 0xC1, 0x59 };
broadcastClient.Send(dgram, dgram.Length, broadcastEndPoint);
Thread.Sleep(1000);
tTemp.Abort();
broadcastClient.Close();
//ErrInfo = "无法访问";
if (broadcastBuffer == null) return false;
if (broadcastBuffer[0] != 0x3B) return false;
byte[] buff = new byte[broadcastBuffer[7]];
Array.Copy(broadcastBuffer, 9, buff, 0, buff.Length);
ip = buff[buff.Length - 12] + "." + buff[buff.Length - 11] + "." + buff[buff.Length - 10] + "." + buff[buff.Length - 9];
//ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 获取目标IP地址
/// </summary>
static void GetIP()
{
broadcastBuffer = broadcastClient.Receive(ref broadcastEndPoint);
}
}
}
<?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>{F85A7412-B5B3-4291-A448-A10564602E1A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DL.IOModule</RootNamespace>
<AssemblyName>DL.IOModule</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</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="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\3D-DoubleLineClient\dll\log4net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\dll\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<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="AIOBOX\AIOBOX.cs" />
<Compile Include="CommHelper.cs" />
<Compile Include="HttpHelper.cs" />
<Compile Include="IOState.cs" />
<Compile Include="IOType.cs" />
<Compile Include="JsonHelper.cs" />
<Compile Include="LogUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Protocol\Modbus\CoilRegister.cs" />
<Compile Include="Protocol\Modbus\DataFrame.cs" />
<Compile Include="Protocol\Modbus\DiscreteInputRegister.cs" />
<Compile Include="Protocol\Modbus\HoldingRegister.cs" />
<Compile Include="Protocol\Modbus\InputRegister.cs" />
<Compile Include="Protocol\Modbus\Register.cs" />
<Compile Include="StringHelper.cs" />
<Compile Include="TCP\ModbusTCPMaster.cs" />
<Compile Include="TCP\TcpClient.cs" />
<Compile Include="TCP\TcpServer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Protocol\Modbus\_system~.ini" />
<None Include="Protocol\_system~.ini" />
<None Include="TCP\_system~.ini" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj">
<Project>{43CDD09E-FCF3-4960-A01D-3BBFE9933122}</Project>
<Name>Common</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file \ No newline at end of file
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DL.Utils
{
public class HttpHelper
{
/// <summary>
/// 检查IP是否合法
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool CheckIP(string ip,string name="")
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(ip, pattern);
if (!rtn)
{
LogUtil.Error($"【{name}】非法的IP地址:" + ip);
return false;
}
return true;
}
/// <summary>
/// Ping指定IP
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool PingIP(string ip)
{
//Ping服务端
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
ping.Dispose();
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
return false;
}
return true;
}
catch (Exception ex)
{
LogUtil.Error($"PingIP{ip}:", ex);
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DL.IOModule
{
public enum Box_Sta : byte
{
/// <summary>
/// 断开,关闭,低电平
/// </summary>
Off,
/// <summary>
/// 闭合,打开,高电平
/// </summary>
On
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DL.IOModule
{
/// <summary>
/// IO模块类型
/// </summary>
public enum IOType : byte
{
/// <summary>
/// 数字信号输入
/// </summary>
DI,
/// <summary>
/// 数字信号输出
/// </summary>
DO,
/// <summary>
/// 模拟量输入
/// </summary>
AI,
/// <summary>
/// 模拟量输出
/// </summary>
AO
}
}
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
namespace DL.Utils
{
/// <summary>
/// Json帮助类
/// </summary>
public class JsonHelper
{
/// <summary>
/// 将对象序列化为JSON格式
/// </summary>
/// <param name="o">对象</param>
/// <returns>json字符串</returns>
public static string SerializeObject(object o)
{
string json = JsonConvert.SerializeObject(o);
return json;
}
/// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
/// <returns>对象实体</returns>
public static T DeserializeJsonToObject<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T;
return t;
}
/// <summary>
/// 解析JSON数组生成对象实体集合
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
/// <returns>对象实体集合</returns>
public static List<T> DeserializeJsonToList<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = o as List<T>;
return list;
}
/// <summary>
/// 反序列化JSON到给定的匿名对象.
/// </summary>
/// <typeparam name="T">匿名对象类型</typeparam>
/// <param name="json">json字符串</param>
/// <param name="anonymousTypeObject">匿名对象</param>
/// <returns>匿名对象</returns>
public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
{
T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
return t;
}
}
///// <summary>
///// 与服务器通信用对象
///// </summary>
//public class Operation
//{
// private string _cid = "";
// public string cid
// {
// get { return _cid; }
// set { _cid = value; }
// }
// public int seq { get; set; }
// public int op { get; set; }
// public int status { get; set; }
// private string _error = "";
// public string error
// {
// get { return _error; }
// set { _error = value; }
// }
// private Dictionary<string, string> _data = new Dictionary<string,string>();
// public Dictionary<string, string> data {
// get { return _data; }
// set { _data = value; }
// }
//}
}
using log4net;
using System;
using System.Reflection;
using System.Drawing;
using log4net.Repository;
using log4net.Config;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace DL.Utils
{
public class LogUtil
{
static Dictionary<string, ILog> LogMap = new Dictionary<string, ILog>();
public delegate void ShowMsg(string msg);
public static event ShowMsg ShowDebug;
public static event ShowMsg ShowInfo;
public static event ShowMsg ShowWarn;
public static event ShowMsg ShowError;
static readonly ILog defaultLog = LogManager.GetLogger("AIOBOX");
public static void Init()
{
XmlConfigurator.Configure(new FileInfo("log4net.config"));
LogMap.Add("AIOBOX", defaultLog);
}
public static void AddLogMap(string key, ILog log)
{
LogMap.Add(key, log);
}
#region Main log
public static void Debug(string msg, ILog log = null)
{
ShowDebug?.Invoke(msg);
if (log == null)
defaultLog.Debug(msg);
else
log.Debug(msg);
}
public static void Info(string msg, ILog log = null)
{
ShowInfo?.Invoke(msg);
if (log == null)
defaultLog.Info(msg);
else
log.Info(msg);
}
public static void Info(string msg, string logName)
{
ShowInfo?.Invoke(msg);
if (string.IsNullOrEmpty(logName))
defaultLog.Info(msg);
else if (LogMap.ContainsKey(logName) && LogMap[logName] != null)
LogMap[logName].Info(msg);
}
public static void Warn(string msg, ILog log = null)
{
ShowWarn?.Invoke(msg);
if (log == null)
defaultLog.Warn(msg);
else
log.Warn(msg);
}
public static void Error(string msg, Exception exception, ILog log)
{
StringBuilder sb = new StringBuilder(msg + "\r\n");
if (log == null)
{
if (exception == null)
defaultLog.Error(msg);
else
{
defaultLog.Error(msg, exception);
sb.Append(exception.StackTrace);
}
}
else
{
if (exception == null)
log.Error(msg);
else
{
log.Error(msg, exception);
sb.Append(exception.StackTrace);
}
}
ShowError?.Invoke(sb.ToString());
}
public static void Error(string msg, ILog log, Exception exception)
{
StringBuilder sb = new StringBuilder(msg + "\r\n");
if (log == null)
{
if (exception == null)
defaultLog.Error(msg);
else
{
defaultLog.Error(msg, exception);
sb.Append(exception.StackTrace);
}
}
else
{
if (exception == null)
log.Error(msg);
else
{
log.Error(msg, exception);
sb.Append(exception.StackTrace);
}
}
ShowError?.Invoke(sb.ToString());
}
public static void Error(string msg, ILog log)
{
StringBuilder sb = new StringBuilder(msg + "\r\n");
if (log == null)
{
defaultLog.Error(msg);
}
else
{
log.Error(msg);
}
ShowError?.Invoke(sb.ToString());
}
public static void Error(string msg, Exception exception)
{
StringBuilder sb = new StringBuilder(msg + "\r\n");
if (exception == null)
defaultLog.Error(msg);
else
{
defaultLog.Error(msg, exception);
sb.Append(exception.StackTrace);
}
ShowError?.Invoke(sb.ToString());
}
public static void Error(string msg)
{
defaultLog.Error(msg);
ShowError?.Invoke(msg);
}
#endregion
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("DL.IOModule")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DL.IOModule")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("f85a7412-b5b3-4291-a448-a10564602e1a")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DL.Com.Protocol.Modbus
{
/// <summary>
/// 线圈寄存器:可读可写。
/// 支持的功能码:
/// 0x01:读线圈
/// 0x05:写线圈
/// 0x0F:写多个线圈
/// </summary>
public class CoilRegister : Register
{
public CoilRegister() : base()
{
FunctionCodes.Add(RegisterFunction.Read, 0x01);
FunctionCodes.Add(RegisterFunction.WriteSingle, 0x05);
FunctionCodes.Add(RegisterFunction.WriteMultiple, 0x0F);
}
/// <summary>
/// PLC地址
/// </summary>
public new string PLCAddress
{
get { return $"0{FrameStruct.PDU.Data.Addr.GetIntValue()}"; }
}
/// <summary>
/// 解析写单个线圈
/// </summary>
/// <returns>写入成功</returns>
protected override bool ParseWriteSingle(byte[] respoonse)
{
if (GetResponseResult(respoonse, out byte[] data))
{
//数据部分,发送与接收一致
byte[] senddata = FrameStruct.PDU.Data.ToBytes();
if (data != null && data.Length != senddata.Length)
return false;
for (int i = 0; i < senddata.Length; i++)
{
if (senddata[i] != data[i])
{
return false;
}
}
return true;
}
return false;
}
/// <summary>
/// 解析写多个线圈
/// </summary>
/// <returns>写入成功/returns>
protected override bool ParseWriteMultiple(byte[] response)
{
if (GetResponseResult(response, out byte[] data))
{
byte[] senddata = FrameStruct.PDU.Data.AddrValueToBytes();
if (data != null && data.Length != senddata.Length)
return false;
for (int i = 0; i < senddata.Length; i++)
{
if (senddata[i] != data[i])
{
return false;
}
}
return true;
}
return false;
}
protected override void SetParamPDU(CmdByte cmd)
{
if (cmd.Function.Equals(RegisterFunction.WriteSingle))
{
SetPDU(cmd.Function, cmd.StartAddr, cmd.Count.Equals(new Byte2 { ByteH = 0x00, ByteL = 0x00 }) ? cmd.Count : new Byte2 { ByteH = 0xFF, ByteL = 0x00 }, cmd.ByteLength, cmd.ValuesOfMultiple);
}
else if (cmd.Function.Equals(RegisterFunction.WriteMultiple))
{
SetPDU(cmd.Function, cmd.StartAddr, cmd.Count, cmd.ByteLength, cmd.ValuesOfMultiple);
}
else
{
SetPDU(RegisterFunction.Read, cmd.StartAddr, cmd.Count);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DL.Com.Protocol.Modbus
{
/// <summary>
/// 离散输入寄存器
/// 功能码0x02
/// </summary>
public class DiscreteInputRegister:Register
{
/// <summary>
/// 离散输入寄存器
/// 功能码0x02
/// </summary>
public DiscreteInputRegister() :base()
{
FunctionCodes.Add(RegisterFunction.Read, 0x02);
}
/// <summary>
/// PLC地址
/// </summary>
public new string PLCAddress
{
get { return $"1{FrameStruct.PDU.Data.Addr.GetIntValue()}"; }
}
protected override bool ParseWriteMultiple(byte[] response)
{
throw new NotImplementedException();
}
protected override bool ParseWriteSingle(byte[] response)
{
throw new NotImplementedException();
}
protected override void SetParamPDU(CmdByte cmdByte)
{
SetPDU(RegisterFunction.Read, cmdByte.StartAddr, cmdByte.Count);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DL.Com.Protocol.Modbus
{
/// <summary>
/// 保持寄存器
/// 0x03
/// </summary>
public class HoldingRegister : Register
{
public HoldingRegister() : base()
{
FunctionCodes.Add(RegisterFunction.Read, 0x03);
FunctionCodes.Add(RegisterFunction.WriteSingle, 0x06);
FunctionCodes.Add(RegisterFunction.WriteMultiple, 0x10);
}
/// <summary>
/// PLC地址
/// </summary>
public new string PLCAddress
{
get { return $"4{FrameStruct.PDU.Data.Addr.GetIntValue()}"; }
}
public override byte[] GetRequestBytes(RegisterFunction func, ushort startAddr, ushort countOrValue,byte equipAddr=1, ushort[] ValuesOfMultiple = null)
{
CmdByte cmdByte = new CmdByte();
cmdByte.StartAddr = Byte2.GetStru2Byte(startAddr);
cmdByte.Function = func;
cmdByte.Count = Byte2.GetStru2Byte(countOrValue);
if (func.Equals(RegisterFunction.WriteMultiple))
{
if (ValuesOfMultiple == null)
return null;
List<byte> values = new List<byte>();
foreach (var item in ValuesOfMultiple)
{
values.AddRange(Byte2.GetStru2Byte(item).ToBytes());
}
cmdByte.ByteLength = (byte)(countOrValue * 2);
cmdByte.ValuesOfMultiple = values.ToArray();
}
SetMBAP(_serialnum, equipAddr);
SetParamPDU(cmdByte);
return DataFrame;
}
protected override void SetParamPDU(CmdByte cmdByte)
{
if (cmdByte.Function.Equals(RegisterFunction.WriteSingle))
{
SetPDU(cmdByte.Function, cmdByte.StartAddr, cmdByte.Count, cmdByte.ByteLength, cmdByte.ValuesOfMultiple);
}
else if (cmdByte.Function.Equals(RegisterFunction.WriteMultiple))
{
SetPDU(cmdByte.Function, cmdByte.StartAddr, cmdByte.Count, cmdByte.ByteLength, cmdByte.ValuesOfMultiple);
}
else
SetPDU(RegisterFunction.Read, cmdByte.StartAddr, cmdByte.Count);
}
protected override bool ParseWriteSingle(byte[] response)
{
if (GetResponseResult(response, out byte[] data))
{
//数据部分,发送与接收一致
byte[] senddata = FrameStruct.PDU.Data.ToBytes();
if (data != null && data.Length != senddata.Length)
return false;
for (int i = 0; i < senddata.Length; i++)
{
if (senddata[i] != data[i])
{
return false;
}
}
return true;
}
return false;
}
protected override bool ParseWriteMultiple(byte[] response)
{
if (GetResponseResult(response, out byte[] data))
{
byte[] senddata = FrameStruct.PDU.Data.AddrValueToBytes();
if (data != null && data.Length != senddata.Length)
return false;
for (int i = 0; i < senddata.Length; i++)
{
if (senddata[i] != data[i])
{
return false;
}
}
return true;
}
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DL.Com.Protocol.Modbus
{
/// <summary>
/// 输入寄存器
/// 支持的功能码0x04
/// </summary>
public class InputRegister : Register
{
public InputRegister() : base()
{
FunctionCodes.Add(RegisterFunction.Read, 0x04);
}
/// <summary>
/// PLC地址
/// </summary>
public new string PLCAddress
{
get { return $"3{FrameStruct.PDU.Data.Addr.GetIntValue()}"; }
}
protected override bool ParseWriteMultiple(byte[] response)
{
throw new NotImplementedException();
}
protected override bool ParseWriteSingle(byte[] response)
{
throw new NotImplementedException();
}
protected override void SetParamPDU(CmdByte cmdByte)
{
SetPDU(RegisterFunction.Read, cmdByte.StartAddr, cmdByte.Count);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DL.Utils
{
public class StringHelper
{
/// <summary>
/// 字节转十六进制字符串
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
public static string ToHexString(byte[] buff)
{
StringBuilder sb = new StringBuilder("");
if (buff == null || buff.Length<1) return sb.ToString();
for (int i = 0; i < buff.Length; i++)
{
sb.Append(buff[i].ToString("X2"));
sb.Append(" ");
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
public static string ToBinaryString(byte[] data)
{
StringBuilder stringBuilder = new StringBuilder("");
if (data == null || data.Length < 1) return stringBuilder.ToString();
stringBuilder.Append("【");
foreach (var item in data)
{
stringBuilder.Append(Convert.ToString(item, 2).PadLeft(8, '0'));
stringBuilder.Append(" ");
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append("】");
return stringBuilder.ToString();
}
public static string ToHexString(byte data)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("【");
stringBuilder.Append(data.ToString("x2"));
stringBuilder.Append("】");
return stringBuilder.ToString();
}
public static byte[] StrToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i]=Convert.ToByte(hexString.Substring(i*2,2),16);
}
return returnBytes;
}
/// <summary>
/// 获取类的属性名及其值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GetPropertiesStr<T>(T t)
{
StringBuilder sb = new StringBuilder("");
if (t == null)
{
return sb.ToString();
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return sb.ToString();
}
sb.Append("{");
foreach (System.Reflection.PropertyInfo property in properties)
{
string name = property.Name;
object value = property.GetValue(t, null);
if (property.PropertyType.IsValueType || property.PropertyType.Name.StartsWith("String"))
{
sb.Append($"{name}:{value},");
}
else if(property.PropertyType.IsGenericType)
{
var listVal = property.GetValue(t, null) as IEnumerable<object>;
if (listVal == null) continue;
sb.Append("[");
foreach (var item1 in listVal)
{
sb.Append(GetPropertiesStr(item1));
}
sb.Append("]");
}
else if(property.PropertyType.IsArray)
{
var listVal = property.GetValue(t, null) as IEnumerable<object>;
if (listVal == null) continue;
sb.Append("[");
foreach (var item1 in listVal)
{
sb.Append(GetPropertiesStr(item1));
}
sb.Append("]");
}
else
{
sb.Append(GetPropertiesStr(value));
}
}
sb.Append("}");
return sb.ToString();
}
}
}
using DL.Utils;
using log4net;
using System;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
namespace DL.Com.TCP
{
public class TcpClient
{
public delegate void HandleMessage(string message, byte[] data);
private Socket m_clientSocket = null;
private byte[] m_receiveBuffer = new byte[1024];
private HandleMessage onReceived;
public int TimeOutTime = 2000;
public string IP { get; set; }
public int Port { get; set; }
public TcpClient()
{
}
/// <summary>
/// 当前连接状态
/// </summary>
public bool IsConnected()
{
if (m_clientSocket == null)
{
return false;
}
return m_clientSocket.Connected;
//if (m_clientSocket.Connected)
//{
// return true;
//}
#region remarks
/********************************************************************************************
* 当Socket.Conneted为false时, 如果您需要确定连接的当前状态,请进行非阻塞、零字节的 Send 调用。
* 如果该调用成功返回或引发 WAEWOULDBLOCK 错误代码 (10035),则该套接字仍然处于连接状态;
* 否则,该套接字不再处于连接状态。
* Depending on http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.connected.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
********************************************************************************************/
#endregion
try
{
#region 过程
// This is how you can determine whether a socket is still connected.
bool connectState = true;
bool blockingState = m_clientSocket.Blocking;
try
{
byte[] tmp = new byte[1];
m_clientSocket.Blocking = false;
m_clientSocket.Send(tmp, 0, 0);
//Console.WriteLine("Connected!");
connectState = true; //若Send错误会跳去执行catch体,而不会执行其try体里其之后的代码
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
{
connectState = true;
}
else
{
connectState = false;
}
}
finally
{
if (m_clientSocket != null && m_clientSocket.Connected)
{
m_clientSocket.Blocking = blockingState;
}
}
//Console.WriteLine("Connected: {0}", client.Connected);
return connectState;
#endregion
}
catch (Exception ex)
{
//Log.Error("IsConnected 出错", log, ex);
return false;
}
}
/// <summary>
/// 连接服务器
/// </summary>
public bool Connect(string serverIP, int serverPort, HandleMessage HandleMessage)
{
IP=serverIP;
Port=serverPort;
m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (TimeOutTime <= 0)
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
m_clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
try
{
if (!m_clientSocket.Connected)
{
m_clientSocket.Connect(remoteEndPoint);
}
if (m_clientSocket.Connected)
{
m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
onReceived = HandleMessage;
LogUtil.Info("Connect to " + serverIP + ":" + serverPort + " success!");
return true;
}
else
{
LogUtil.Info("Connect to " + serverIP + ":" + serverPort + " fail!");
}
}
catch (Exception ex)
{
LogUtil.Error("Connect to " + serverIP + ":" + serverPort + " fail!", ex);
//m_clientSocket = null;
}
}
else
{
m_clientSocket.ReceiveTimeout = TimeOutTime;
m_clientSocket.SendTimeout = TimeOutTime;
IAsyncResult connResult = m_clientSocket.BeginConnect(serverIP, serverPort, null, null);
connResult.AsyncWaitHandle.WaitOne(this.TimeOutTime, true); //等待2秒
if (!connResult.IsCompleted || (!m_clientSocket.Connected))
{
LogUtil.Info("Connect to " + serverIP + ":" + serverPort + " fail!");
m_clientSocket.Close();
//处理连接不成功的动作
return false;
}
else
{
//处理连接成功的动作
m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
onReceived = HandleMessage;
LogUtil.Info("Connect to " + serverIP + ":" + serverPort + " success!");
return true;
}
}
return false;
}
/// <summary>
/// 断开连接
/// </summary>
public void Close()
{
try
{
if (m_clientSocket != null && m_clientSocket.Connected)
{
m_clientSocket.Shutdown(SocketShutdown.Both);
//Thread.Sleep(300);
//m_clientSocket.Disconnect(true);
//Thread.Sleep(300);
m_clientSocket.Close();
//m_clientSocket = null;
LogUtil.Info("Socket closed!");
}
else
{
LogUtil.Error("No socket is running!");
}
}
catch (Exception ex)
{
LogUtil.Error("close error", ex);
}
}
/// <summary>
/// 发送信息
/// </summary>
public void Send(string strSendData)
{
byte[] sendBuffer = new byte[1024];
sendBuffer = Encoding.UTF8.GetBytes(strSendData);
if (m_clientSocket != null && m_clientSocket.Connected)
{
m_clientSocket.Send(sendBuffer);
LogUtil.Debug("Send >> " + strSendData);
}
}
public bool Send(byte[] bytes)
{
if (m_clientSocket != null && m_clientSocket.Connected)
{
m_clientSocket.Send(bytes);
LogUtil.Debug("Send >> " + StringHelper.ToHexString(bytes));
return true;
}
return false;
}
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
if (m_clientSocket != null && m_clientSocket.Connected)
{
int REnd = m_clientSocket.EndReceive(ar);
string strReceiveData = Encoding.Default.GetString(m_receiveBuffer, 0, REnd);
byte[] tmp = new byte[REnd];
Array.Copy(m_receiveBuffer, tmp, REnd);
onReceived(strReceiveData, tmp);
Thread.Sleep(100);
m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
}
}
catch (Exception ex)
{
LogUtil.Error("socket received error", ex);
}
}
}
}

using DL.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DL.Com
{
public class UdpServer
{
static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.DeclaringType);
private Thread m_serverThread;
private Socket udpServer;
public delegate void ReviceMsg(EndPoint address, string Msg);
/// <summary>
/// 接受到数据事件
/// </summary>
public event ReviceMsg ReviceMsgEvent;
private bool isRun = true;
private int serverPort;
private void logLocalIp()
{
string[] addresses = GetLocalAddresses();
string iplist = "本机IP:[";
if (addresses.Length > 0)
{
for (int i = 0; i < addresses.Length; i++)
{
if (addresses.Length != 0)
{
iplist = iplist + " " + addresses[i];
}
}
}
LogUtil.Info(iplist + "]",log);
}
public UdpServer(int m_serverPort)
{
serverPort = m_serverPort;
}
/// <summary>
/// 开始服务
/// </summary>
public bool Start()
{
try
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, serverPort);//定义一网络端点
udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//定义一个Socket
udpServer.Bind(ipep);//Socket与本地的一个终结点相关联
m_serverThread = new Thread(new ThreadStart(ReceiveData));
m_serverThread.Start();
logLocalIp();
isRun = true;
return true;
}
catch (Exception ex)
{
LogUtil.Error("启动udpserver出错" ,log,ex);
}
return false;
}
/// <summary>
/// 停止服务
/// </summary>
public bool Stop()
{
try
{
isRun = false;
m_serverThread.Interrupt(); // 线程终止
udpServer.Close(); // Socket Close
return true;
}
catch (Exception ex)
{
LogUtil.Error("Stop Udpserver出错",log,ex);
}
return false;
}
private StringBuilder sb = new StringBuilder(); //这个是用来保存:接收到了的,但是还没有结束的消息
private int receiveBufferSize = 1024;
/// <summary>
/// 结束符
/// </summary>
public string terminateString = "\r";
private void ReceiveData()
{
try
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//定义要发送的计算机的地址
EndPoint Remote = (EndPoint)(sender);//
while (true)
{
byte[] buffer = new byte[receiveBufferSize];
int receivedSize = udpServer.ReceiveFrom(buffer, ref Remote);
if (receivedSize > 0)
{
string rawMsg = Encoding.ASCII.GetString(buffer, 0, receivedSize);
int rnFixLength = terminateString.Length; //这个是指消息结束符的长度,此处为\r\n
for (int i = 0; i < rawMsg.Length;) //遍历接收到的整个buffer文本
{
if (i <= rawMsg.Length - rnFixLength)
{
if (rawMsg.Substring(i, rnFixLength) != terminateString)//非消息结束符,则加入sb
{
sb.Append(rawMsg[i]);
i++;
}
else
{
this.ReviceMsgEvent.Invoke(Remote, sb.ToString());//找到了消息结束符,触发消息接收完成事件
sb = new StringBuilder();
i += rnFixLength;
}
}
else
{
sb.Append(rawMsg[i]);
i++;
}
}
}
Thread.Sleep(500);
}
}
catch (Exception ex)
{
LogUtil.Error("启动udpserver出错:" ,log,ex);
}
}
/// <summary>
/// 获取本机地址列表
/// </summary>
public string[] GetLocalAddresses()
{
// 获取主机名
string strHostName = Dns.GetHostName();
// 根据主机名进行查找
IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);
string[] retval = new string[iphostentry.AddressList.Length];
int i = 0;
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
if (ipaddress.AddressFamily != AddressFamily.InterNetworkV6)
{
retval[i] = ipaddress.ToString();
i++;
}
}
return retval;
}
public bool IsOnline(string addr = "")
{
return isRun;
}
public string GetAddr()
{
return $"{string.Join(",",GetLocalAddresses())}:{serverPort}";
}
}
}
...@@ -36,9 +36,6 @@ ...@@ -36,9 +36,6 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Asa.IOModule.AIOBOX">
<HintPath>..\..\dll\Asa.IOModule.AIOBOX.dll</HintPath>
</Reference>
<Reference Include="Asa.RFID.HiStation"> <Reference Include="Asa.RFID.HiStation">
<HintPath>..\..\dll\Asa.RFID.HiStation.dll</HintPath> <HintPath>..\..\dll\Asa.RFID.HiStation.dll</HintPath>
</Reference> </Reference>
...@@ -48,15 +45,12 @@ ...@@ -48,15 +45,12 @@
<Reference Include="AsaPL.AgvClient"> <Reference Include="AsaPL.AgvClient">
<HintPath>..\..\dll\AsaPL.AgvClient.dll</HintPath> <HintPath>..\..\dll\AsaPL.AgvClient.dll</HintPath>
</Reference> </Reference>
<Reference Include="HFReader9CSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64"> <Reference Include="log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\dll\RFID\HFReader9CSharp.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\dll\log4net.dll</HintPath> <HintPath>..\..\dll\log4net.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Web" /> <Reference Include="System.Web" />
...@@ -88,6 +82,10 @@ ...@@ -88,6 +82,10 @@
<Project>{43cdd09e-fcf3-4960-a01d-3bbfe9933122}</Project> <Project>{43cdd09e-fcf3-4960-a01d-3bbfe9933122}</Project>
<Name>Common</Name> <Name>Common</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\DL.IOModule\DL.IOModule.csproj">
<Project>{f85a7412-b5b3-4291-a448-a10564602e1a}</Project>
<Name>DL.IOModule</Name>
</ProjectReference>
<ProjectReference Include="..\LoadCVSLibrary\LoadCSVLibrary.csproj"> <ProjectReference Include="..\LoadCVSLibrary\LoadCSVLibrary.csproj">
<Project>{064bebf5-8faa-4ea2-a5f3-a06e6e7d9251}</Project> <Project>{064bebf5-8faa-4ea2-a5f3-a06e6e7d9251}</Project>
<Name>LoadCSVLibrary</Name> <Name>LoadCSVLibrary</Name>
...@@ -95,9 +93,11 @@ ...@@ -95,9 +93,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
<ItemGroup> <ItemGroup>
<None Include="app.config" />
<None Include="LineConfig\FeederLineConfig.csv"> <None Include="LineConfig\FeederLineConfig.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Include="packages.config" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
......
using log4net; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
...@@ -8,23 +7,21 @@ using System.Threading; ...@@ -8,23 +7,21 @@ using System.Threading;
using OnlineStore.Common; using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary; using OnlineStore.LoadCSVLibrary;
using System.Threading.Tasks; using System.Threading.Tasks;
using Asa.IOModule; //using Asa.IOModule;
using System.Windows.Forms; using System.Windows.Forms;
using DL.IOModule;
using System.Runtime.InteropServices;
using log4net;
namespace OnlineStore.DeviceLibrary namespace OnlineStore.DeviceLibrary
{ {
public class AIOBOXManager : IOManager public class AIOBOXManager : IOManager
{ {
//public static uint DefaultDICount = 16; //public static uint DefaultDICount = 16;
//public static uint DefaultDOCount = 16; //public static uint DefaultDOCount = 16;
public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public Dictionary<string, AIOBOX> AIOMap = new Dictionary<string, AIOBOX>(); public Dictionary<string, AIOBOX> AIOMap = new Dictionary<string, AIOBOX>();
public Dictionary<string, List<Box_Sta>> DIValueMap = new Dictionary<string, List<Box_Sta>>(); public Dictionary<string, List<Box_Sta>> DIValueMap = new Dictionary<string, List<Box_Sta>>();
public Dictionary<string, List<Box_Sta>> DOValueMap = new Dictionary<string, List<Box_Sta>>(); public Dictionary<string, List<Box_Sta>> DOValueMap = new Dictionary<string, List<Box_Sta>>();
private object DIMapLock = "";
private object DOMapLock = "";
private object DILock = ""; private object DILock = "";
private object DOLock = ""; private object DOLock = "";
...@@ -85,21 +82,16 @@ namespace OnlineStore.DeviceLibrary ...@@ -85,21 +82,16 @@ namespace OnlineStore.DeviceLibrary
try try
{ {
aioBox = new AIOBOX(); aioBox = new AIOBOX();
aioBox.LogPath(Application.StartupPath + @"\logs\aio\", LogType.OnlyError);
aioBox.IP = ioIp; aioBox.IP = ioIp;
aioBox.SetType(IOType.DI, DILength, IOType.DO, DOLength);
aioBox.SetInput(Asa.IOModule.Box_Type.DI, DILength);
aioBox.SetOutput(Asa.IOModule.Box_Type.DO, DOLength);
//DI主动上传 //DI主动上传
aioBox.AutoReadInput(true); aioBox.Upload = true;
aioBox.AutoReadOutput(false, DOMS);
aioBox.DI_Changed_Event += AioBox_DI_Changed_Event; ; aioBox.DI_Changed_Event += AioBox_DI_Changed_Event; ;
aioBox.DO_Changed_Event += AioBox_DO_Changed_Event; aioBox.DO_Changed_Event += AioBox_DO_Changed_Event;
LogUtil.info("开始连接:" + logName + ":" + aioBox.ErrInfo); LogUtil.info("开始连接:" + logName);
aioBox.Connect(); aioBox.Connect();
AIOMap.Add(ioIp, aioBox); AIOMap.Add(ioIp, aioBox);
...@@ -111,20 +103,12 @@ namespace OnlineStore.DeviceLibrary ...@@ -111,20 +103,12 @@ namespace OnlineStore.DeviceLibrary
} }
catch (Exception error) catch (Exception error)
{ {
LogUtil.error(LOGGER, "连接IO模块 " + logName + " 出错:" + error.ToString()); LogUtil.error("连接IO模块 " + logName + " 出错", error);
} }
} }
private DateTime lastLogTime = DateTime.Now; private DateTime lastLogTime = DateTime.Now;
private void AioBox_Log_Out_Event(AIOBOX box, string[] s)
{
foreach (string str in s)
{
LogUtil.AIOLog.Debug("[" + box.IP + "]" + str);
}
}
private void AioBox_DI_Changed_Event(AIOBOX box, Box_Sta[] sta) private void AioBox_DI_Changed_Event(AIOBOX box, Box_Sta[] sta)
{ {
try try
...@@ -268,17 +252,17 @@ namespace OnlineStore.DeviceLibrary ...@@ -268,17 +252,17 @@ namespace OnlineStore.DeviceLibrary
bool result = aioBox.WriteDO(StartAddress, GetBox_Sta(onOff)); bool result = aioBox.WriteDO(StartAddress, GetBox_Sta(onOff));
if (!result) if (!result)
{ {
LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 失败:" + aioBox.ErrInfo); LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 失败");
} }
} }
else else
{ {
LogUtil.error(LOGGER, "WriteSingleDO出错 没有连接IO模块:" + ioIp); LogUtil.error( $"WriteSingleDO出错 没有连接IO模块:{ioIp}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
LOGGER.Error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 出错啦:", ex); LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 出错啦:", ex);
} }
} }
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds) public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds)
...@@ -298,11 +282,11 @@ namespace OnlineStore.DeviceLibrary ...@@ -298,11 +282,11 @@ namespace OnlineStore.DeviceLibrary
try try
{ {
aioBox.WriteDO(StartAddress, aioBox.ReverseStatus(currBox_Sta)); aioBox.WriteDO(StartAddress, aioBox.ReverseStatus(currBox_Sta));
LogUtil.debug(LOGGER, "**********定时回写入 IO [" + ioIp + "] [" + StartAddress + "]值" + aioBox.ReverseStatus(currBox_Sta) + "】:"); LogUtil.debug("**********定时回写入 IO [" + ioIp + "] [" + StartAddress + "]值" + aioBox.ReverseStatus(currBox_Sta) + "】:");
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER, "**********定时回写入 出错:", ex); LogUtil.error("**********定时回写入 出错:", ex);
} }
}; };
mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次; mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
...@@ -311,7 +295,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -311,7 +295,7 @@ namespace OnlineStore.DeviceLibrary
} }
else else
{ {
LogUtil.error(LOGGER, "AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 出错 没有连接IO模块:" + ioIp); LogUtil.error($"AIO WriteSingleDO [{ioIp}][{StartAddress}]出错,没有连接IO模块:{ioIp}");
} }
} }
catch (Exception ex) catch (Exception ex)
...@@ -439,7 +423,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -439,7 +423,7 @@ namespace OnlineStore.DeviceLibrary
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER, " GetIOValue [" + configIO.IO_IP + "] [" + configIO.GetIOAddr() + "] 获取数据出错:", ex); LogUtil.error(" GetIOValue [" + configIO.IO_IP + "] [" + configIO.GetIOAddr() + "] 获取数据出错:", ex);
} }
return value; return value;
} }
......
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.15.0" newVersion="2.0.15.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.15" targetFramework="net461" />
</packages>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<configSections> <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
...@@ -29,12 +29,12 @@ ...@@ -29,12 +29,12 @@
<file type="log4net.Util.PatternString" value="logs/FeederLine.log" /> <file type="log4net.Util.PatternString" value="logs/FeederLine.log" />
<param name="Encoding" value="UTF-8" /> <param name="Encoding" value="UTF-8" />
<appendToFile value="true" /> <appendToFile value="true" />
<param name="MaxSizeRollBackups" value="30"/> <param name="MaxSizeRollBackups" value="30" />
<!-- 切割最多文件数 --> <!-- 切割最多文件数 -->
<param name="MaximumFileSize" value="100MB"/> <param name="MaximumFileSize" value="100MB" />
<!-- 每个文件的大小 --> <!-- 每个文件的大小 -->
<param name="RollingStyle" value="Size"/> <param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true"/> <param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout"> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%t]%-5p %m%n" /> <conversionPattern value="[%date][%t]%-5p %m%n" />
</layout> </layout>
...@@ -43,12 +43,12 @@ ...@@ -43,12 +43,12 @@
<file value="logs/TheRFID-FeederLine.log" /> <file value="logs/TheRFID-FeederLine.log" />
<param name="Encoding" value="UTF-8" /> <param name="Encoding" value="UTF-8" />
<appendToFile value="true" /> <appendToFile value="true" />
<param name="MaxSizeRollBackups" value="30"/> <param name="MaxSizeRollBackups" value="30" />
<!-- 切割最多文件数 --> <!-- 切割最多文件数 -->
<param name="MaximumFileSize" value="50MB"/> <param name="MaximumFileSize" value="50MB" />
<!-- 每个文件的大小 --> <!-- 每个文件的大小 -->
<param name="RollingStyle" value="Size"/> <param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true"/> <param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout"> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%t]%-5p %m%n" /> <conversionPattern value="[%date][%t]%-5p %m%n" />
</layout> </layout>
...@@ -81,4 +81,12 @@ ...@@ -81,4 +81,12 @@
</providers> </providers>
</roleManager> </roleManager>
</system.web> </system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.15.0" newVersion="2.0.15.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration> </configuration>
\ No newline at end of file \ No newline at end of file
...@@ -53,17 +53,15 @@ ...@@ -53,17 +53,15 @@
<ApplicationIcon>DfIcon.ico</ApplicationIcon> <ApplicationIcon>DfIcon.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Client, Version=1.0.0.1, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\dll\Client.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>..\..\dll\log4net.dll</HintPath> <HintPath>..\..\dll\log4net.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.ServiceModel" /> <Reference Include="System.ServiceModel" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" /> <Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
...@@ -108,6 +106,7 @@ ...@@ -108,6 +106,7 @@
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
</Compile> </Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
......
...@@ -23,7 +23,6 @@ namespace OnlineStore.FeederLineClient ...@@ -23,7 +23,6 @@ namespace OnlineStore.FeederLineClient
{ {
private FeederLineBean feederLine; private FeederLineBean feederLine;
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public FrmFeederClient() public FrmFeederClient()
{ {
Control.CheckForIllegalCrossThreadCalls = false; Control.CheckForIllegalCrossThreadCalls = false;
...@@ -41,7 +40,7 @@ namespace OnlineStore.FeederLineClient ...@@ -41,7 +40,7 @@ namespace OnlineStore.FeederLineClient
this.feederLine = LineManager.InitStore(); this.feederLine = LineManager.InitStore();
if (feederLine == null) if (feederLine == null)
{ {
LogUtil.error(LOGGER, "加载设备失败"); LogUtil.error("加载设备失败");
this.Close(); this.Close();
return; return;
} }
...@@ -168,7 +167,7 @@ namespace OnlineStore.FeederLineClient ...@@ -168,7 +167,7 @@ namespace OnlineStore.FeederLineClient
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER, "出错:" + ex.StackTrace); LogUtil.error("出错:" + ex.StackTrace);
} }
} }
...@@ -230,7 +229,7 @@ namespace OnlineStore.FeederLineClient ...@@ -230,7 +229,7 @@ namespace OnlineStore.FeederLineClient
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogUtil.error(LOGGER, ex.StackTrace); LogUtil.error(ex.StackTrace);
} }
} }
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.15" targetFramework="net461" />
</packages>
\ No newline at end of file \ No newline at end of file
...@@ -33,12 +33,14 @@ ...@@ -33,12 +33,14 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL"> <Reference Include="log4net, Version=2.0.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\dll\log4net.dll</HintPath> <HintPath>..\..\dll\log4net.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
...@@ -63,6 +65,9 @@ ...@@ -63,6 +65,9 @@
<Name>Common</Name> <Name>Common</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.15" targetFramework="net40" />
</packages>
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!