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
4e5817fa
Commit
4e5817fa
authored
Nov 22, 2022
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加 7E协议支持批量包发送,不等回复已经发送
parent
59014699
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
447 additions
and
99 deletions
+447
-99
Dev7E.cs
GeneralGommunication/Dev7E.cs
+319
-41
GComm_SerialPort.cs
GeneralGommunication/GComm_SerialPort.cs
+1
-1
GComm_TcpClient.cs
GeneralGommunication/GComm_TcpClient.cs
+70
-33
IDev7E.cs
GeneralGommunication/IDev7E.cs
+57
-24
No files found.
GeneralGommunication/Dev7E.cs
View file @
4e5817fa
This diff is collapsed.
Click to expand it.
GeneralGommunication/GComm_SerialPort.cs
View file @
4e5817fa
...
...
@@ -190,7 +190,7 @@ namespace GeneralGommunication
if
(!
IsRunning
)
return
;
IsRunning
=
false
;
IsConnected
=
false
;
cancellation
.
Cancel
();
sp
.
Close
();
}
...
...
GeneralGommunication/GComm_TcpClient.cs
View file @
4e5817fa
...
...
@@ -48,9 +48,9 @@ namespace GeneralGommunication
public
event
PropertyChangedEventHandler
PropertyChanged
;
Socket
sock
;
CancellationTokenSource
c
ancellation
;
CancellationTokenSource
c
ancellation_waitf
orSend
;
CancellationTokenSource
c
ts_readTask
;
CancellationTokenSource
c
ts_waitF
orSend
;
CancellationTokenSource
cts_sendTask
;
public
GComm_TcpClient
()
{
...
...
@@ -64,9 +64,9 @@ namespace GeneralGommunication
if
(
IsRunning
)
return
;
IsRunning
=
true
;
c
ancellation
=
new
CancellationTokenSource
();
c
ts_readTask
=
new
CancellationTokenSource
();
Task
.
Factory
.
StartNew
(
OnTask
,
c
ancellation
.
Token
);
Task
.
Factory
.
StartNew
(
OnTask
,
c
ts_readTask
.
Token
);
}
/// <summary>
...
...
@@ -77,8 +77,8 @@ namespace GeneralGommunication
if
(!
IsRunning
)
return
;
IsRunning
=
false
;
c
ancellation
.
Cancel
();
IsConnected
=
false
;
c
ts_readTask
.
Cancel
();
if
(
sock
!=
null
&&
sock
.
Connected
)
{
sock
.
Close
();
...
...
@@ -88,7 +88,7 @@ namespace GeneralGommunication
void
OnTask
()
{
while
(!
c
ancellation
.
IsCancellationRequested
)
while
(!
c
ts_readTask
.
IsCancellationRequested
)
{
ConnectTask
();
...
...
@@ -96,17 +96,27 @@ namespace GeneralGommunication
{
sendBuf
.
Clear
();
//启动发送task
Task
.
Factory
.
StartNew
(
SendTask
,
cancellation
.
Token
);
cts_sendTask
=
new
CancellationTokenSource
();
var
sendtask
=
Task
.
Factory
.
StartNew
(
SendTask
,
cts_sendTask
.
Token
);
//进入接收task
ReceiveTask
();
//退出了,肯定连接断开了
//需要等待 SendTask 也退出了
cts_sendTask
.
Cancel
();
sendtask
.
Wait
();
if
(
sock
!=
null
)
{
sock
.
Close
();
}
}
//休息一会儿,在重连
try
{
Task
.
Delay
(
2000
,
c
ancellation
.
Token
).
Wait
();
Task
.
Delay
(
2000
,
c
ts_readTask
.
Token
).
Wait
();
}
catch
(
Exception
e
)
{
...
...
@@ -126,14 +136,12 @@ namespace GeneralGommunication
sock
=
new
Socket
(
AddressFamily
.
InterNetwork
,
SocketType
.
Stream
,
ProtocolType
.
Tcp
);
sock
.
Blocking
=
true
;
//1秒内,需要收到信息
sock
.
ReceiveTimeout
=
1000
;
//1秒内,必须发送完
sock
.
SendTimeout
=
1000
;
while
(!
c
ancellation
.
IsCancellationRequested
)
while
(!
c
ts_readTask
.
IsCancellationRequested
)
{
try
{
...
...
@@ -150,7 +158,7 @@ namespace GeneralGommunication
}
try
{
Task
.
Delay
(
2000
,
c
ancellation
.
Token
).
Wait
();
Task
.
Delay
(
2000
,
c
ts_readTask
.
Token
).
Wait
();
}
catch
(
Exception
e
)
{
...
...
@@ -166,7 +174,7 @@ namespace GeneralGommunication
void
ReceiveTask
()
{
byte
[]
buf
=
new
byte
[
0x10000
];
while
(!
c
ancellation
.
IsCancellationRequested
)
while
(!
c
ts_readTask
.
IsCancellationRequested
)
{
int
len
;
try
...
...
@@ -196,10 +204,7 @@ namespace GeneralGommunication
DataReceived
?.
Invoke
(
this
,
buf
.
Take
(
len
).
ToArray
());
}
if
(
sock
!=
null
)
{
sock
.
Close
();
}
IsConnected
=
false
;
}
...
...
@@ -208,14 +213,24 @@ namespace GeneralGommunication
/// </summary>
void
SendTask
()
{
while
(!
c
ancellation
.
IsCancellationRequested
)
while
(!
c
ts_sendTask
.
IsCancellationRequested
)
{
while
(
sendBuf
.
Count
>
0
)
CancellationTokenSource
cts3
;
while
(
true
)
{
byte
[]
buffer
;
lock
(
sendBuf
)
{
if
(
sendBuf
.
Count
<=
0
)
break
;
buffer
=
sendBuf
.
ToArray
();
}
int
slen
;
try
{
slen
=
sock
.
Send
(
sendBuf
.
ToArray
()
);
slen
=
sock
.
Send
(
buffer
);
}
catch
(
Exception
e
)
{
...
...
@@ -223,26 +238,42 @@ namespace GeneralGommunication
IsConnected
=
false
;
break
;
}
if
(
slen
>
0
)
sendBuf
.
RemoveRange
(
0
,
slen
);
else
if
(
slen
<=
0
)
if
(
slen
<=
0
)
{
//连接断开了
IsConnected
=
false
;
break
;
}
else
{
lock
(
sendBuf
)
{
sendBuf
.
RemoveRange
(
0
,
slen
);
}
}
}
//重新等 下次被呼醒
cancellation_waitforSend
=
new
CancellationTokenSource
();
lock
(
sendBuf
)
{
// 呼醒 发送task
if
(
cts_waitForSend
==
null
)
{
cts_waitForSend
=
new
CancellationTokenSource
();
}
//cts_waitForSend 与 cts_sendTask 合体, 任意一个都会呼醒 delay
cts3
=
CancellationTokenSource
.
CreateLinkedTokenSource
(
cts_waitForSend
.
Token
,
cts_sendTask
.
Token
);
}
try
{
Task
.
Delay
(-
1
,
c
ancellation_waitforSend
.
Token
).
Wait
();
Task
.
Delay
(-
1
,
c
ts3
.
Token
).
Wait
();
}
catch
(
Exception
e
)
{
//被打断, 有数据需要发送
}
cts_waitForSend
=
null
;
}
}
/// <summary>
...
...
@@ -259,13 +290,19 @@ namespace GeneralGommunication
if
(!
IsConnected
)
return
;
lock
(
sendBuf
)
{
//放入,发送缓存区
sendBuf
.
AddRange
(
buf
);
//放入,发送缓存区
sendBuf
.
AddRange
(
buf
);
//呼醒 发送task
cancellation_waitforSend
.
Cancel
();
//呼醒 发送task
if
(
cts_waitForSend
==
null
)
{
cts_waitForSend
=
new
CancellationTokenSource
();
}
cts_waitForSend
.
Cancel
();
}
}
/// <summary>
...
...
GeneralGommunication/IDev7E.cs
View file @
4e5817fa
...
...
@@ -9,7 +9,10 @@ namespace GeneralGommunication
{
public
interface
IDev7E
:
INotifyPropertyChanged
{
#
region
测量速度
#
region
测量通讯速度
/// <summary>
/// 通讯速度 测量中
/// </summary>
bool
IsMeasuring
{
get
;
}
/// <summary>
...
...
@@ -18,7 +21,7 @@ namespace GeneralGommunication
double
CommSpeed
{
get
;
}
/// <summary>
///
传输
速度 单位 pack/s
///
通讯
速度 单位 pack/s
/// </summary>
double
PackSpeed
{
get
;
}
...
...
@@ -100,26 +103,31 @@ namespace GeneralGommunication
/// </summary>
public
class
COMMREQ
{
private
byte
[]
prefix
;
/// <summary>
/// 指令 前缀
/// </summary>
public
byte
[]
Prefix
;
public
string
PrefixString
public
byte
[]
Prefix
{
get
{
return
prefix
;
}
set
{
prefix
=
value
;
updatePrefixString
();
}
}
public
string
PrefixString
{
get
;
private
set
;
}
void
updatePrefixString
()
{
get
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
Prefix
.
Count
();
i
++)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
Prefix
.
Count
();
i
++)
{
if
(
Prefix
[
i
]
>=
33
||
Prefix
[
i
]
<=
126
)
stringBuilder
.
Append
((
char
)
Prefix
[
i
]);
else
stringBuilder
.
Append
(
$" 0x
{
Prefix
[
i
]:
X2
}
"
);
}
return
stringBuilder
.
ToString
();
if
(
Prefix
[
i
]
>=
33
&&
Prefix
[
i
]
<=
126
)
stringBuilder
.
Append
((
char
)
Prefix
[
i
]);
else
stringBuilder
.
Append
(
$" 0x
{
Prefix
[
i
]:
X2
}
"
);
}
PrefixString
=
stringBuilder
.
ToString
();
}
/// <summary>
/// 回复数据 长度, 不含前序
/// </summary>
...
...
@@ -133,9 +141,9 @@ namespace GeneralGommunication
public
delegate
object
ParseFuncPackHandler
(
byte
[]
pack
,
int
dataIdx
);
public
ParseFuncPackHandler
ParseFuncPack
;
public
delegate
bool
IsMatchHandler
(
COMMREQ
commReq
,
byte
[]
pack
,
int
dataIdx
,
object
context
,
ref
object
retData
);
public
IsMatchHandler
IsMatch
;
public
object
IsMatchContext
;
public
static
byte
[]
ToPrefix
(
params
object
[]
commands
)
{
List
<
byte
>
bytes
=
new
List
<
byte
>();
...
...
@@ -176,13 +184,9 @@ namespace GeneralGommunication
}
public
class
COMMREQ_Transaction
{
/// <summary>
/// 请求
/// </summary>
public
COMMREQ
commReq
;
public
abstract
class
COMMREQ_TransactionBase
{
/// <summary>
/// 回复 callback
/// </summary>
...
...
@@ -192,6 +196,25 @@ namespace GeneralGommunication
/// 上下文
/// </summary>
public
object
asyncContext
;
}
/// <summary>
/// 交易集合包
/// </summary>
public
class
COMMREQ_TransactionMulti
:
COMMREQ_TransactionBase
{
/// <summary>
/// 交易集合
/// </summary>
public
List
<
COMMREQ_Transaction
>
transactions
=
new
List
<
COMMREQ_Transaction
>();
}
public
class
COMMREQ_Transaction
:
COMMREQ_TransactionBase
{
/// <summary>
/// 请求
/// </summary>
public
COMMREQ
commReq
;
/// <summary>
/// 数据
...
...
@@ -202,5 +225,15 @@ namespace GeneralGommunication
/// 数据的object 表达,只用于调试而已
/// </summary>
public
object
datasObj
;
/// <summary>
/// 数据已经回复
/// </summary>
public
bool
hasRet
;
/// <summary>
/// 回复的数据,只用于调试而已
/// </summary>
public
object
retData
;
}
}
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