Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VIZ.Framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王永辉
VIZ.Framework
Commits
74205d8b
Commit
74205d8b
authored
Jul 18, 2022
by
liulongfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1. 添加串口通讯
parent
d3cd7a2e
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
222 additions
and
59 deletions
+222
-59
VIZ.Framework.Connection/Core/ConnMessageBase.cs
+5
-0
VIZ.Framework.Connection/Core/ConnPackageInfo.cs
+5
-0
VIZ.Framework.Connection/Core/ConnPackageProviderAttribute.cs
+6
-1
VIZ.Framework.Connection/Protocol/SerialPort/SerialPortConnection.cs
+30
-0
VIZ.Framework.Connection/Protocol/SerialPort/SerialPortEndpointManager.cs
+106
-0
VIZ.Framework.Connection/Protocol/SerialPort/SerialPortEndpointManagerExpand.cs
+15
-0
VIZ.Framework.Connection/Protocol/TCP/TcpConnection.cs
+0
-0
VIZ.Framework.Connection/Protocol/TCP/TcpEndpointManager.cs
+0
-14
VIZ.Framework.Connection/Protocol/TCP/TcpEndpointManagerExpand.cs
+0
-0
VIZ.Framework.Connection/Protocol/UDP/UdpConnection.cs
+0
-0
VIZ.Framework.Connection/Protocol/UDP/UdpEndpointManager.cs
+0
-0
VIZ.Framework.Connection/Protocol/UDP/UdpEndpointManagerExpand.cs
+0
-0
VIZ.Framework.Connection/Provider/FixedBuffer/ConnFixedBufferPackageProvider.cs
+45
-37
VIZ.Framework.Connection/VIZ.Framework.Connection.csproj
+10
-7
No files found.
VIZ.Framework.Connection/Core/ConnMessageBase.cs
View file @
74205d8b
...
@@ -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
;
}
}
}
}
}
VIZ.Framework.Connection/Core/ConnPackageInfo.cs
View file @
74205d8b
...
@@ -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
;
}
...
...
VIZ.Framework.Connection/Core/ConnPackageProviderAttribute.cs
View file @
74205d8b
...
@@ -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>
...
...
VIZ.Framework.Connection/Protocol/SerialPort/SerialPortConnection.cs
0 → 100644
View file @
74205d8b
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
()
{
}
}
}
VIZ.Framework.Connection/Protocol/SerialPort/SerialPortEndpointManager.cs
0 → 100644
View file @
74205d8b
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
);
}
}
}
}
}
VIZ.Framework.Connection/Protocol/SerialPort/SerialPortEndpointManagerExpand.cs
0 → 100644
View file @
74205d8b
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
{
}
}
VIZ.Framework.Connection/TCP/TcpConnection.cs
→
VIZ.Framework.Connection/
Protocol/
TCP/TcpConnection.cs
View file @
74205d8b
File moved
VIZ.Framework.Connection/TCP/TcpEndpointManager.cs
→
VIZ.Framework.Connection/
Protocol/
TCP/TcpEndpointManager.cs
View file @
74205d8b
...
@@ -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>
...
...
VIZ.Framework.Connection/TCP/TcpEndpointManagerExpand.cs
→
VIZ.Framework.Connection/
Protocol/
TCP/TcpEndpointManagerExpand.cs
View file @
74205d8b
File moved
VIZ.Framework.Connection/UDP/UdpConnection.cs
→
VIZ.Framework.Connection/
Protocol/
UDP/UdpConnection.cs
View file @
74205d8b
File moved
VIZ.Framework.Connection/UDP/UdpEndpointManager.cs
→
VIZ.Framework.Connection/
Protocol/
UDP/UdpEndpointManager.cs
View file @
74205d8b
File moved
VIZ.Framework.Connection/UDP/UdpEndpointManagerExpand.cs
→
VIZ.Framework.Connection/
Protocol/
UDP/UdpEndpointManagerExpand.cs
View file @
74205d8b
File moved
VIZ.Framework.Connection/Provider/FixedBuffer/ConnFixedBufferPackageProvider.cs
View file @
74205d8b
...
@@ -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,47 +51,55 @@ namespace VIZ.Framework.Connection
...
@@ -51,47 +51,55 @@ 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
);
this
.
buffer_index
+=
copy
;
if
(
this
.
buffer_index
<
this
.
FixedBufferSize
)
return
;
// 处理器处理消息
while
(
package_index
<
package
.
DataSize
)
ConnFixedBufferInfo
info
=
new
ConnFixedBufferInfo
();
{
info
.
LocalIP
=
package
.
LocalIP
;
int
copy
=
Math
.
Min
(
buffer_cache
.
Length
-
buffer_index
,
package
.
DataSize
-
package_index
);
info
.
LocalPort
=
package
.
LocalPort
;
Array
.
Copy
(
package
.
Data
,
package_index
,
this
.
buffer_cache
,
buffer_index
,
copy
);
info
.
RemoteIP
=
package
.
RemoteIP
;
this
.
buffer_index
+=
copy
;
info
.
RemotePort
=
package
.
RemotePort
;
package_index
+=
copy
;
info
.
Buffer
=
this
.
buffer_cache
;
this
.
buffer_index
=
0
;
if
(
this
.
buffer_index
<
this
.
FixedBufferSize
)
this
.
buffer_cache
=
new
byte
[
this
.
FixedBufferSize
]
;
continue
;
try
// 处理器处理消息
{
ConnFixedBufferInfo
info
=
new
ConnFixedBufferInfo
();
this
.
Execute
(
info
);
info
.
LocalIP
=
package
.
LocalIP
;
}
info
.
LocalPort
=
package
.
LocalPort
;
catch
(
Exception
ex
)
info
.
RemoteIP
=
package
.
RemoteIP
;
{
info
.
RemotePort
=
package
.
RemotePort
;
log
.
Error
(
ex
);
info
.
Buffer
=
this
.
buffer_cache
;
}
// 发送消息
try
ConnFixedBufferMessage
msg
=
new
ConnFixedBufferMessage
();
{
msg
.
LocalIP
=
package
.
LocalIP
;
this
.
Execute
(
info
);
msg
.
LocalPort
=
package
.
LocalPort
;
}
msg
.
RemoteIP
=
package
.
RemoteIP
;
catch
(
Exception
ex
)
msg
.
RemotePort
=
package
.
RemotePort
;
{
msg
.
Buffer
=
info
.
Buffer
;
log
.
Error
(
ex
);
}
try
// 发送消息
{
ConnFixedBufferMessage
msg
=
new
ConnFixedBufferMessage
();
ApplicationDomain
.
MessageManager
.
Send
(
msg
);
msg
.
LocalIP
=
package
.
LocalIP
;
}
msg
.
LocalPort
=
package
.
LocalPort
;
catch
(
Exception
ex
)
msg
.
RemoteIP
=
package
.
RemoteIP
;
{
msg
.
RemotePort
=
package
.
RemotePort
;
log
.
Error
(
ex
);
msg
.
Buffer
=
this
.
buffer_cache
;
try
{
ApplicationDomain
.
MessageManager
.
Send
(
msg
);
}
catch
(
Exception
ex
)
{
log
.
Error
(
ex
);
}
// 清理缓存
this
.
buffer_cache
=
new
byte
[
this
.
FixedBufferSize
];
this
.
buffer_index
=
0
;
}
}
}
}
...
...
VIZ.Framework.Connection/VIZ.Framework.Connection.csproj
View file @
74205d8b
...
@@ -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" />
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment