Commit 74205d8b by liulongfei

1. 添加串口通讯

parent d3cd7a2e
...@@ -30,5 +30,10 @@ namespace VIZ.Framework.Connection ...@@ -30,5 +30,10 @@ namespace VIZ.Framework.Connection
/// 远程端口 /// 远程端口
/// </summary> /// </summary>
public int RemotePort { get; internal set; } public int RemotePort { get; internal set; }
/// <summary>
/// 串口名称
/// </summary>
public string PortName { get; internal set; }
} }
} }
...@@ -32,6 +32,11 @@ namespace VIZ.Framework.Connection ...@@ -32,6 +32,11 @@ namespace VIZ.Framework.Connection
public int RemotePort { get; internal set; } public int RemotePort { get; internal set; }
/// <summary> /// <summary>
/// 串口名称
/// </summary>
public string PortName { get; internal set; }
/// <summary>
/// 数据 /// 数据
/// </summary> /// </summary>
public byte[] Data { get; set; } public byte[] Data { get; set; }
......
...@@ -20,7 +20,12 @@ namespace VIZ.Framework.Connection ...@@ -20,7 +20,12 @@ namespace VIZ.Framework.Connection
/// <summary> /// <summary>
/// 一般用作UDP消息,不需要考虑连包情况 /// 一般用作UDP消息,不需要考虑连包情况
/// </summary> /// </summary>
UDP UDP,
/// <summary>
/// 一般用作串口消息,不需要考虑连包情况
/// </summary>
SerialPort
} }
/// <summary> /// <summary>
......
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Connection
{
/// <summary>
/// 串口连接
/// </summary>
public class SerialPortConnection : IDisposable
{
/// <summary>
/// 日志
/// </summary>
private static ILog log = LogManager.GetLogger(typeof(SerialPortConnection));
/// <summary>
/// 销毁
/// </summary>
public void Dispose()
{
}
}
}
using log4net;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Connection
{
/// <summary>
/// 串口终结点管理器
/// </summary>
public class SerialPortEndpointManager
{
/// <summary>
/// 日志
/// </summary>
private static ILog log = LogManager.GetLogger(typeof(SerialPortEndpointManager));
/// <summary>
/// 接收数据数组大小
/// </summary>
public const int RECV_BUFFER_SIZE = 1024 * 10;
/// <summary>
/// 串口终结点管理器
/// </summary>
/// <param name="key">键</param>
/// <param name="portName">端口</param>
/// <param name="baudRate">波特率</param>
/// <param name="parity">对象的奇偶校验位</param>
/// <param name="dataBits">数据位值</param>
/// <param name="stopBits">使用停止位</param>
public SerialPortEndpointManager(string key, string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
{
this.Key = key;
this.SerialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
this.SerialPort.DataReceived += SerialPort_DataReceived;
}
/// <summary>
/// 键
/// </summary>
public string Key { get; private set; }
/// <summary>
/// 串口
/// </summary>
public SerialPort SerialPort { get; private set; }
/// <summary>
/// 包处理器
/// </summary>
public IConnPackageProvider PackageProvider { get; set; }
/// <summary>
/// 打开串口
/// </summary>
public void Open()
{
this.SerialPort.Open();
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="buffer">数据</param>
public void Send(byte[] buffer)
{
this.SerialPort.Write(buffer, 0, buffer.Length);
}
/// <summary>
/// 接收数据触发
/// </summary>
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (true)
{
byte[] buffer = new byte[RECV_BUFFER_SIZE];
int read = this.SerialPort.Read(buffer, 0, RECV_BUFFER_SIZE);
if (read <= 0)
break;
ConnPackageInfo package = new ConnPackageInfo();
package.PortName = this.SerialPort.PortName;
package.Data = buffer;
package.DataSize = read;
if (this.PackageProvider == null)
continue;
try
{
this.PackageProvider.Execute(package);
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Connection
{
/// <summary>
/// 串口终结点管理器扩展
/// </summary>
public static class SerialPortEndpointManagerExpand
{
}
}
...@@ -86,20 +86,6 @@ namespace VIZ.Framework.Connection ...@@ -86,20 +86,6 @@ namespace VIZ.Framework.Connection
/// </summary> /// </summary>
public IConnPackageProvider PackageProvider { get; set; } public IConnPackageProvider PackageProvider { get; set; }
#if DEBUG
/// <summary>
/// 调试消息委托
/// </summary>
/// <param name="msg">消息</param>
public delegate void DebugMessageDelegate(string msg);
/// <summary>
/// 调试消息时触发
/// </summary>
public DebugMessageDelegate OnDebugMessage;
#endif
/// <summary> /// <summary>
/// 锁对象 /// 锁对象
/// </summary> /// </summary>
......
...@@ -11,7 +11,7 @@ namespace VIZ.Framework.Connection ...@@ -11,7 +11,7 @@ namespace VIZ.Framework.Connection
/// <summary> /// <summary>
/// 固定长度Buffer包解析器 /// 固定长度Buffer包解析器
/// </summary> /// </summary>
[ConnPackageProvider(ConnPackageProviderType.TCP | ConnPackageProviderType.UDP)] [ConnPackageProvider(ConnPackageProviderType.TCP | ConnPackageProviderType.UDP | ConnPackageProviderType.SerialPort)]
public class ConnFixedBufferPackageProvider : IConnPackageProvider public class ConnFixedBufferPackageProvider : IConnPackageProvider
{ {
/// <summary> /// <summary>
...@@ -51,11 +51,17 @@ namespace VIZ.Framework.Connection ...@@ -51,11 +51,17 @@ namespace VIZ.Framework.Connection
/// <param name="package">数据包</param> /// <param name="package">数据包</param>
public void Execute(ConnPackageInfo package) public void Execute(ConnPackageInfo package)
{ {
int copy = Math.Min(package.DataSize, (buffer_cache.Length - buffer_index)); int package_index = 0;
Array.Copy(package.Data, 0, this.buffer_cache, buffer_index, copy);
while (package_index < package.DataSize)
{
int copy = Math.Min(buffer_cache.Length - buffer_index, package.DataSize - package_index);
Array.Copy(package.Data, package_index, this.buffer_cache, buffer_index, copy);
this.buffer_index += copy; this.buffer_index += copy;
package_index += copy;
if (this.buffer_index < this.FixedBufferSize) if (this.buffer_index < this.FixedBufferSize)
return; continue;
// 处理器处理消息 // 处理器处理消息
ConnFixedBufferInfo info = new ConnFixedBufferInfo(); ConnFixedBufferInfo info = new ConnFixedBufferInfo();
...@@ -65,9 +71,6 @@ namespace VIZ.Framework.Connection ...@@ -65,9 +71,6 @@ namespace VIZ.Framework.Connection
info.RemotePort = package.RemotePort; info.RemotePort = package.RemotePort;
info.Buffer = this.buffer_cache; info.Buffer = this.buffer_cache;
this.buffer_index = 0;
this.buffer_cache = new byte[this.FixedBufferSize];
try try
{ {
this.Execute(info); this.Execute(info);
...@@ -83,7 +86,7 @@ namespace VIZ.Framework.Connection ...@@ -83,7 +86,7 @@ namespace VIZ.Framework.Connection
msg.LocalPort = package.LocalPort; msg.LocalPort = package.LocalPort;
msg.RemoteIP = package.RemoteIP; msg.RemoteIP = package.RemoteIP;
msg.RemotePort = package.RemotePort; msg.RemotePort = package.RemotePort;
msg.Buffer = info.Buffer; msg.Buffer = this.buffer_cache;
try try
{ {
...@@ -93,6 +96,11 @@ namespace VIZ.Framework.Connection ...@@ -93,6 +96,11 @@ namespace VIZ.Framework.Connection
{ {
log.Error(ex); log.Error(ex);
} }
// 清理缓存
this.buffer_cache = new byte[this.FixedBufferSize];
this.buffer_index = 0;
}
} }
/// <summary> /// <summary>
......
...@@ -68,12 +68,14 @@ ...@@ -68,12 +68,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ConnectionManager.cs" /> <Compile Include="ConnectionManager.cs" />
<Compile Include="Core\ConnMessageBase.cs" />
<Compile Include="Core\ConnInfoBase.cs" /> <Compile Include="Core\ConnInfoBase.cs" />
<Compile Include="Core\ConnMessageBase.cs" />
<Compile Include="Core\ConnPackageInfo.cs" /> <Compile Include="Core\ConnPackageInfo.cs" />
<Compile Include="Core\ConnPackageProviderAttribute.cs" /> <Compile Include="Core\ConnPackageProviderAttribute.cs" />
<Compile Include="Core\ConnSendMessage.cs" /> <Compile Include="Core\ConnSendMessage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Protocol\SerialPort\SerialPortEndpointManagerExpand.cs" />
<Compile Include="Protocol\SerialPort\SerialPortEndpointManager.cs" />
<Compile Include="Provider\CommandLine\ConnCommandLineInfo.cs" /> <Compile Include="Provider\CommandLine\ConnCommandLineInfo.cs" />
<Compile Include="Provider\CommandLine\ConnCommandLineMessage.cs" /> <Compile Include="Provider\CommandLine\ConnCommandLineMessage.cs" />
<Compile Include="Provider\CommandLine\ConnCommandLinePackageProvider.cs" /> <Compile Include="Provider\CommandLine\ConnCommandLinePackageProvider.cs" />
...@@ -84,13 +86,14 @@ ...@@ -84,13 +86,14 @@
<Compile Include="Provider\SingleJson\ConnSingleJsonInfo.cs" /> <Compile Include="Provider\SingleJson\ConnSingleJsonInfo.cs" />
<Compile Include="Provider\SingleJson\ConnSingleJsonMessage.cs" /> <Compile Include="Provider\SingleJson\ConnSingleJsonMessage.cs" />
<Compile Include="Provider\SingleJson\ConnSingleJsonPackageProvider.cs" /> <Compile Include="Provider\SingleJson\ConnSingleJsonPackageProvider.cs" />
<Compile Include="TCP\TcpConnection.cs" /> <Compile Include="Protocol\SerialPort\SerialPortConnection.cs" />
<Compile Include="TCP\TcpEndpointManager.cs" /> <Compile Include="Protocol\TCP\TcpConnection.cs" />
<Compile Include="TCP\TcpEndpointManagerExpand.cs" /> <Compile Include="Protocol\TCP\TcpEndpointManager.cs" />
<Compile Include="Protocol\TCP\TcpEndpointManagerExpand.cs" />
<Compile Include="Core\ConnThreadInfo.cs" /> <Compile Include="Core\ConnThreadInfo.cs" />
<Compile Include="UDP\UdpConnection.cs" /> <Compile Include="Protocol\UDP\UdpConnection.cs" />
<Compile Include="UDP\UdpEndpointManager.cs" /> <Compile Include="Protocol\UDP\UdpEndpointManager.cs" />
<Compile Include="UDP\UdpEndpointManagerExpand.cs" /> <Compile Include="Protocol\UDP\UdpEndpointManagerExpand.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment