Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
7a8171aa
Commit
7a8171aa
authored
Jan 14, 2024
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改 通信测量合并到CommSpeedMeasuring
parent
4e5817fa
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
96 deletions
+140
-96
Dev7E.cs
GeneralGommunication/Dev7E.cs
+139
-72
IDev7E.cs
GeneralGommunication/IDev7E.cs
+1
-24
No files found.
GeneralGommunication/Dev7E.cs
View file @
7a8171aa
...
@@ -18,31 +18,12 @@ namespace GeneralGommunication
...
@@ -18,31 +18,12 @@ namespace GeneralGommunication
/// 正常模式,多个"问" 会储存在缓存区, 等上一条 "答" ,才能发下一条"问"。
/// 正常模式,多个"问" 会储存在缓存区, 等上一条 "答" ,才能发下一条"问"。
/// 打包模式, 多个"问" 没有先后关系,把它们打包, 不等 "答" 就同时发送"问"。 但 还是必须等 全部 "答" 都回复,才能执行一个动作。
/// 打包模式, 多个"问" 没有先后关系,把它们打包, 不等 "答" 就同时发送"问"。 但 还是必须等 全部 "答" 都回复,才能执行一个动作。
/// </summary>
/// </summary>
public
abstract
class
Dev7E
:
IDev7E
public
abstract
class
Dev7E
:
IDev7E
{
{
public
Logger
logger
=
NLog
.
LogManager
.
GetCurrentClassLogger
();
public
Logger
logger
=
NLog
.
LogManager
.
GetCurrentClassLogger
();
public
event
PropertyChangedEventHandler
PropertyChanged
;
public
event
PropertyChangedEventHandler
PropertyChanged
;
#
region
测量通讯速度
/// <summary>
/// 通讯速度 测量中
/// </summary>
public
bool
IsMeasuring
{
get
;
private
set
;
}
/// <summary>
/// 通讯速度 byte/s
/// </summary>
public
double
CommSpeed
{
get
;
private
set
;
}
/// <summary>
/// 通讯速度 单位 pack/s
/// </summary>
public
double
PackSpeed
{
get
;
private
set
;
}
#
endregion
/// <summary>
/// <summary>
/// 包出错次数
/// 包出错次数
/// </summary>
/// </summary>
...
@@ -53,15 +34,17 @@ namespace GeneralGommunication
...
@@ -53,15 +34,17 @@ namespace GeneralGommunication
/// </summary>
/// </summary>
public
bool
IsConnected
{
get
;
private
set
;
}
public
bool
IsConnected
{
get
;
private
set
;
}
/// <summary>
/// 通讯速度测量模块
/// </summary>
public
CommSpeedMeasuring
Csm
=>
csm
;
/// <summary>
/// <summary>
/// 有数据需要发送
/// 有数据需要发送
/// </summary>
/// </summary>
public
event
SendMsgEventHander
SendMsgEvent
;
public
event
SendMsgEventHander
SendMsgEvent
;
int
packCnt
=
0
;
int
recCnt
=
0
;
Stopwatch
stopwatch
=
new
Stopwatch
();
protected
CancellationTokenSource
cancellationTokenSource
;
/// <summary>
/// <summary>
/// 对于全部 有返回的函数调用,都使用Dispatcher,使线程同步
/// 对于全部 有返回的函数调用,都使用Dispatcher,使线程同步
...
@@ -89,6 +72,7 @@ namespace GeneralGommunication
...
@@ -89,6 +72,7 @@ namespace GeneralGommunication
/// </summary>
/// </summary>
int
retryCnt
=
0
;
int
retryCnt
=
0
;
CommSpeedMeasuring
csm
=
new
CommSpeedMeasuring
();
/// <summary>
/// <summary>
/// 指令前序 序号
/// 指令前序 序号
/// </summary>
/// </summary>
...
@@ -102,40 +86,7 @@ namespace GeneralGommunication
...
@@ -102,40 +86,7 @@ namespace GeneralGommunication
}
}
#
region
测量
#
region
测量
public
void
StartMeasure
()
{
if
(
IsMeasuring
)
return
;
IsMeasuring
=
true
;
cancellationTokenSource
=
new
CancellationTokenSource
();
//启动线程,测量速度
Task
.
Factory
.
StartNew
(
MeasureTask
,
cancellationTokenSource
.
Token
);
}
public
void
StopMeasure
()
{
cancellationTokenSource
.
Cancel
();
//停止线程
IsMeasuring
=
false
;
}
async
void
MeasureTask
()
{
stopwatch
.
Start
();
while
(!
cancellationTokenSource
.
IsCancellationRequested
)
{
if
(
stopwatch
.
Elapsed
>
TimeSpan
.
FromSeconds
(
1
))
{
//1秒均值
CommSpeed
=
recCnt
/
stopwatch
.
Elapsed
.
TotalSeconds
;
PackSpeed
=
packCnt
/
stopwatch
.
Elapsed
.
TotalSeconds
;
recCnt
=
0
;
packCnt
=
0
;
stopwatch
.
Restart
();
}
await
Task
.
Delay
(
100
);
}
stopwatch
.
Stop
();
}
#
endregion
#
endregion
...
@@ -182,7 +133,7 @@ namespace GeneralGommunication
...
@@ -182,7 +133,7 @@ namespace GeneralGommunication
public
void
RecMsg
(
byte
[]
recBuf
)
public
void
RecMsg
(
byte
[]
recBuf
)
{
{
IsConnected
=
true
;
IsConnected
=
true
;
recCnt
+=
recBuf
.
Count
(
);
csm
.
IncRec
(
recBuf
.
Count
()
);
for
(
int
i
=
0
;
i
<
recBuf
.
Count
();
i
++)
for
(
int
i
=
0
;
i
<
recBuf
.
Count
();
i
++)
{
{
...
@@ -195,7 +146,7 @@ namespace GeneralGommunication
...
@@ -195,7 +146,7 @@ namespace GeneralGommunication
var
pack
=
currPack
.
ToArray
();
var
pack
=
currPack
.
ToArray
();
ParsePack
(
pack
);
ParsePack
(
pack
);
currPack
.
Clear
();
currPack
.
Clear
();
packCnt
++
;
csm
.
IncPack
(
1
)
;
}
}
}
}
currPack
.
Add
(
recBuf
[
i
]);
currPack
.
Add
(
recBuf
[
i
]);
...
@@ -320,7 +271,7 @@ namespace GeneralGommunication
...
@@ -320,7 +271,7 @@ namespace GeneralGommunication
{
{
currTran
=
null
;
currTran
=
null
;
stopwatch_timeOut
.
Stop
();
stopwatch_timeOut
.
Stop
();
recCnt
=
0
;
csm
.
Reset
()
;
IsConnected
=
false
;
IsConnected
=
false
;
Transactions
.
Clear
();
Transactions
.
Clear
();
}
}
...
@@ -531,7 +482,8 @@ namespace GeneralGommunication
...
@@ -531,7 +482,8 @@ namespace GeneralGommunication
tran
=
null
;
tran
=
null
;
}
}
if
(
commReq
==
null
)
{
if
(
commReq
==
null
)
{
//不能解析
//不能解析
logger
.
Error
(
$"ACK multi reponse len =
{
pack
.
Count
()}
reponse:
{
bytes2hex
(
pack
)}
"
);
logger
.
Error
(
$"ACK multi reponse len =
{
pack
.
Count
()}
reponse:
{
bytes2hex
(
pack
)}
"
);
ErrCnt
++;
ErrCnt
++;
...
@@ -636,7 +588,7 @@ namespace GeneralGommunication
...
@@ -636,7 +588,7 @@ namespace GeneralGommunication
}
}
protected
void
AddTran
(
COMMREQ_Transaction
tran
)
protected
void
AddTran
(
COMMREQ_Transaction
tran
)
{
{
if
(
transactionMulti
!=
null
)
if
(
transactionMulti
!=
null
)
{
{
transactionMulti
.
transactions
.
Add
(
tran
);
transactionMulti
.
transactions
.
Add
(
tran
);
}
}
...
@@ -654,7 +606,8 @@ namespace GeneralGommunication
...
@@ -654,7 +606,8 @@ namespace GeneralGommunication
COMMREQ_TransactionMulti
transactionMulti
;
COMMREQ_TransactionMulti
transactionMulti
;
void
BuildMultiTransAsync
()
void
BuildMultiTransAsync
()
{
{
if
(
transactionMulti
!=
null
)
{
if
(
transactionMulti
!=
null
)
{
throw
new
Exception
(
"已经是 BuildMultiTrans, 不能再次执行, 必须调用 AddMultiTrans"
);
throw
new
Exception
(
"已经是 BuildMultiTrans, 不能再次执行, 必须调用 AddMultiTrans"
);
}
}
transactionMulti
=
new
COMMREQ_TransactionMulti
();
transactionMulti
=
new
COMMREQ_TransactionMulti
();
...
@@ -675,7 +628,8 @@ namespace GeneralGommunication
...
@@ -675,7 +628,8 @@ namespace GeneralGommunication
void
AddMultiTransAsync
(
CallBackHandler
asyncDelegate
,
object
asyncContext
)
void
AddMultiTransAsync
(
CallBackHandler
asyncDelegate
,
object
asyncContext
)
{
{
if
(
transactionMulti
.
transactions
.
Count
()
==
0
)
{
if
(
transactionMulti
.
transactions
.
Count
()
==
0
)
{
throw
new
Exception
(
"MultiTrans 为空"
);
throw
new
Exception
(
"MultiTrans 为空"
);
}
}
transactionMulti
.
asyncDelegate
=
asyncDelegate
;
transactionMulti
.
asyncDelegate
=
asyncDelegate
;
...
@@ -692,4 +646,117 @@ namespace GeneralGommunication
...
@@ -692,4 +646,117 @@ namespace GeneralGommunication
}
}
}
}
}
}
/// <summary>
/// 测量通讯速度模块
/// </summary>
public
class
CommSpeedMeasuring
:
INotifyPropertyChanged
{
public
event
PropertyChangedEventHandler
PropertyChanged
;
/// <summary>
/// 通讯速度 测量中
/// </summary>
public
bool
IsMeasuring
{
get
;
private
set
;
}
/// <summary>
/// 通讯速度 byte/s
/// </summary>
public
double
CommSpeed
{
get
;
private
set
;
}
/// <summary>
/// 通讯速度 单位 pack/s
/// </summary>
public
double
PackSpeed
{
get
;
private
set
;
}
int
packCnt
=
0
;
int
recCnt
=
0
;
Stopwatch
stopwatch
=
new
Stopwatch
();
protected
CancellationTokenSource
cancellationTokenSource
;
public
void
StartMeasure
()
{
if
(
IsMeasuring
)
return
;
IsMeasuring
=
true
;
Reset
();
cancellationTokenSource
=
new
CancellationTokenSource
();
//启动线程,测量速度
Task
.
Factory
.
StartNew
(
MeasureTask
,
cancellationTokenSource
.
Token
);
}
public
void
StopMeasure
()
{
cancellationTokenSource
.
Cancel
();
//停止线程
IsMeasuring
=
false
;
}
void
MeasureTask
()
{
stopwatch
.
Start
();
while
(!
cancellationTokenSource
.
IsCancellationRequested
)
{
if
(
stopwatch
.
Elapsed
>
TimeSpan
.
FromSeconds
(
1
))
{
int
rcnt
,
pcnt
;
double
totalsec
;
lock
(
this
)
{
totalsec
=
stopwatch
.
Elapsed
.
TotalSeconds
;
stopwatch
.
Restart
();
rcnt
=
recCnt
;
pcnt
=
packCnt
;
recCnt
=
0
;
packCnt
=
0
;
}
//1秒均值
CommSpeed
=
rcnt
/
totalsec
;
PackSpeed
=
pcnt
/
totalsec
;
}
Task
.
Delay
(
100
).
Wait
();
}
stopwatch
.
Stop
();
}
/// <summary>
/// 异常时调用
/// </summary>
public
void
Reset
()
{
lock
(
this
)
{
recCnt
=
0
;
packCnt
=
0
;
}
}
/// <summary>
/// 需要在 RecMsg(byte[] recBuf) 中调用
/// </summary>
/// <param name="cnt"></param>
public
void
IncRec
(
int
cnt
)
{
if
(!
IsMeasuring
)
return
;
lock
(
this
)
{
recCnt
+=
cnt
;
}
}
/// <summary>
/// 需要在 ParsePack(pack); 后调用
/// </summary>
/// <param name="cnt"></param>
public
void
IncPack
(
int
cnt
)
{
if
(!
IsMeasuring
)
return
;
lock
(
this
)
{
packCnt
+=
cnt
;
}
}
}
}
}
GeneralGommunication/IDev7E.cs
View file @
7a8171aa
...
@@ -10,30 +10,7 @@ namespace GeneralGommunication
...
@@ -10,30 +10,7 @@ namespace GeneralGommunication
public
interface
IDev7E
:
INotifyPropertyChanged
public
interface
IDev7E
:
INotifyPropertyChanged
{
{
#
region
测量通讯速度
#
region
测量通讯速度
/// <summary>
CommSpeedMeasuring
Csm
{
get
;
}
/// 通讯速度 测量中
/// </summary>
bool
IsMeasuring
{
get
;
}
/// <summary>
/// 通讯速度 byte/s
/// </summary>
double
CommSpeed
{
get
;
}
/// <summary>
/// 通讯速度 单位 pack/s
/// </summary>
double
PackSpeed
{
get
;
}
/// <summary>
/// 开始测量速度
/// </summary>
void
StartMeasure
();
/// <summary>
///结束测量速度
/// </summary>
void
StopMeasure
();
#
endregion
#
endregion
/// <summary>
/// <summary>
...
...
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