Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
PastureGateway
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
steven.sha
PastureGateway
Commits
5c95addb
Commit
5c95addb
authored
Jun 20, 2019
by
shaxuezheng
Browse files
Options
Browse Files
Download
Plain Diff
merge origin
parents
96689972
468680e9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
45 deletions
+80
-45
ModualAlarm.cs
ModbusDemo/Common/ModualAlarm.cs
+10
-7
Form1.cs
ModbusDemo/windows/Form1.cs
+70
-38
No files found.
ModbusDemo/Common/ModualAlarm.cs
View file @
5c95addb
...
@@ -49,13 +49,16 @@ namespace ModbusDemo.Common
...
@@ -49,13 +49,16 @@ namespace ModbusDemo.Common
}
}
public
static
async
Task
vPublishAlarmPackageJson
(
IMqttClient
mqttClient
)
public
static
async
Task
vPublishAlarmPackageJson
(
IMqttClient
mqttClient
)
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
if
(
mqttClient
.
IsConnected
==
true
)
.
WithTopic
(
Form1
.
sAlarmTopic
)
{
.
WithPayload
(
sAlarmJson
)
var
message
=
new
MqttApplicationMessageBuilder
()
.
WithAtMostOnceQoS
()
.
WithTopic
(
Form1
.
sAlarmTopic
)
.
WithRetainFlag
(
false
)
.
WithPayload
(
sAlarmJson
)
.
Build
();
.
WithAtMostOnceQoS
()
await
mqttClient
.
PublishAsync
(
message
);
.
WithRetainFlag
(
false
)
.
Build
();
await
mqttClient
.
PublishAsync
(
message
);
}
}
}
public
static
void
vSet_Modual_Alarm
(
byte
byModbusID
,
ushort
usAlarmCodde
,
bool
bSetAlarm
,
byte
byCommFailTimes
,
params
string
[]
strChannalList
)
public
static
void
vSet_Modual_Alarm
(
byte
byModbusID
,
ushort
usAlarmCodde
,
bool
bSetAlarm
,
byte
byCommFailTimes
,
params
string
[]
strChannalList
)
{
{
...
...
ModbusDemo/windows/Form1.cs
View file @
5c95addb
...
@@ -114,7 +114,6 @@ namespace ModbusDemo
...
@@ -114,7 +114,6 @@ namespace ModbusDemo
{
{
while
(
m_Isbgw0_CanContinueRun
)
while
(
m_Isbgw0_CanContinueRun
)
{
{
int
_n
=
2
;
this
.
Invoke
(
new
Action
(()
=>
this
.
Invoke
(
new
Action
(()
=>
{
{
}));
}));
...
@@ -144,37 +143,46 @@ namespace ModbusDemo
...
@@ -144,37 +143,46 @@ namespace ModbusDemo
public
List
<
string
>
ListHandle
=
new
List
<
string
>();
public
List
<
string
>
ListHandle
=
new
List
<
string
>();
private
async
Task
Publish
()
private
async
Task
Publish
()
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
if
(
mqttClient
.
IsConnected
==
true
)
.
WithTopic
(
mqttTopic
)
.
WithPayload
(
mqttData
)
.
WithAtMostOnceQoS
()
.
WithRetainFlag
(
false
)
.
Build
();
if
(
mqttData
!=
null
)
{
{
await
mqttClient
.
PublishAsync
(
message
);
var
message
=
new
MqttApplicationMessageBuilder
()
.
WithTopic
(
mqttTopic
)
.
WithPayload
(
mqttData
)
.
WithAtMostOnceQoS
()
.
WithRetainFlag
(
false
)
.
Build
();
if
(
mqttData
!=
null
)
{
await
mqttClient
.
PublishAsync
(
message
);
}
}
}
}
}
private
void
vControlResponseBack
()
private
void
vControlResponseBack
()
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
if
(
mqttClient
.
IsConnected
==
true
)
.
WithTopic
(
mqttBackTopic
)
{
.
WithPayload
(
mqttData_Back
)
var
message
=
new
MqttApplicationMessageBuilder
()
.
WithAtLeastOnceQoS
()
.
WithTopic
(
mqttBackTopic
)
.
WithRetainFlag
(
false
)
.
WithPayload
(
mqttData_Back
)
.
Build
();
.
WithAtLeastOnceQoS
()
mqttClient
.
PublishAsync
(
message
);
.
WithRetainFlag
(
false
)
.
Build
();
mqttClient
.
PublishAsync
(
message
);
}
}
}
private
async
Task
vCfgResponseBack
(
string
data
)
private
async
Task
vCfgResponseBack
(
string
data
)
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
if
(
mqttClient
.
IsConnected
==
true
)
.
WithTopic
(
mqttCfgResTopic
)
{
.
WithPayload
(
data
)
var
message
=
new
MqttApplicationMessageBuilder
()
.
WithAtMostOnceQoS
()
.
WithTopic
(
mqttCfgResTopic
)
.
WithRetainFlag
(
false
)
.
WithPayload
(
data
)
.
Build
();
.
WithAtMostOnceQoS
()
await
mqttClient
.
PublishAsync
(
message
);
.
WithRetainFlag
(
false
)
.
Build
();
await
mqttClient
.
PublishAsync
(
message
);
}
}
}
private
async
Task
Subscribe
()
private
async
Task
Subscribe
()
{
{
...
@@ -354,7 +362,6 @@ namespace ModbusDemo
...
@@ -354,7 +362,6 @@ namespace ModbusDemo
}
}
})));
})));
}
}
private
void
vMqtt_Receive_Handle
()
private
void
vMqtt_Receive_Handle
()
{
{
while
(
true
)
while
(
true
)
...
@@ -373,8 +380,8 @@ namespace ModbusDemo
...
@@ -373,8 +380,8 @@ namespace ModbusDemo
#
region
modbus
线程
#
region
modbus
线程
private
static
bool
bSetDataIsRunning
=
false
;
private
static
bool
bSetDataIsRunning
=
false
;
private
Thread
ModbusPollThread
;
private
Thread
ModbusPollThread
;
private
void
vModbusPoll
()
private
void
vModbusPoll
()
{
{
while
(
true
)
while
(
true
)
...
@@ -389,7 +396,7 @@ namespace ModbusDemo
...
@@ -389,7 +396,7 @@ namespace ModbusDemo
private
void
Sendout
()
private
void
Sendout
()
{
{
Control
.
CheckForIllegalCrossThreadCalls
=
false
;
Control
.
CheckForIllegalCrossThreadCalls
=
false
;
MQTTTimer
.
Interval
=
20000
;
//120500
MQTTTimer
.
Interval
=
20000
l
;
//120500
MQTTTimer
.
Enabled
=
true
;
MQTTTimer
.
Enabled
=
true
;
MQTTTimer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
MQTTTimer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
MQTTTimer
.
Start
();
MQTTTimer
.
Start
();
...
@@ -989,6 +996,8 @@ namespace ModbusDemo
...
@@ -989,6 +996,8 @@ namespace ModbusDemo
{
{
}
}
private
void
SubscribeBut_Click
(
object
sender
,
EventArgs
e
)
private
void
SubscribeBut_Click
(
object
sender
,
EventArgs
e
)
{
{
...
@@ -1044,12 +1053,25 @@ namespace ModbusDemo
...
@@ -1044,12 +1053,25 @@ namespace ModbusDemo
SetMqtt
();
SetMqtt
();
MQTT_thread
=
new
MethodInvoker
(
Sendout
);
MQTT_thread
=
new
MethodInvoker
(
Sendout
);
MQTT_thread
.
BeginInvoke
(
null
,
null
);
MQTT_thread
.
BeginInvoke
(
null
,
null
);
await
Task
.
Run
(
async
()
=>
{
await
ConnectMqttServerAsync
();
});
await
Task
.
Run
(
async
()
=>
{
await
ConnectMqttServerAsync
();
});
ModbusComm
.
Modbus
=
ModbusSerialMaster
.
CreateRtu
(
comPort
);
ModbusComm
.
Modbus
=
ModbusSerialMaster
.
CreateRtu
(
comPort
);
ModbusComm
.
Modbus
.
Transport
.
Retries
=
0
;
ModbusComm
.
Modbus
.
Transport
.
Retries
=
0
;
ModbusComm
.
Modbus
.
Transport
.
ReadTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
ReadTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WriteTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WriteTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WaitToRetryMilliseconds
=
0
;
ModbusComm
.
Modbus
.
Transport
.
WaitToRetryMilliseconds
=
0
;
<<<<<<<
HEAD
//t_AutoControl = new Thread(T_AutoControl);
//t_AutoControl.IsBackground = true;
//t_AutoControl.Start();
=======
//t_AutoControl = new Thread(T_AutoControl)
//{
// IsBackground = true
//};
//t_AutoControl.Start();
modbus_Timer
.
Enabled
=
true
;
modbus_Timer
.
Enabled
=
true
;
btOpenCOM
.
Enabled
=
false
;
btOpenCOM
.
Enabled
=
false
;
...
@@ -1108,25 +1130,35 @@ namespace ModbusDemo
...
@@ -1108,25 +1130,35 @@ namespace ModbusDemo
ModbusComm
.
Modbus
.
Transport
.
WriteTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WriteTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WaitToRetryMilliseconds
=
0
;
ModbusComm
.
Modbus
.
Transport
.
WaitToRetryMilliseconds
=
0
;
t_AutoControl
=
new
Thread
(
AtuoControl_run
);
//t_AutoControl = new Thread(T_AutoControl)
t_AutoControl
.
IsBackground
=
true
;
//{
t_AutoControl
.
Start
();
// IsBackground = true
//};
//t_AutoControl.Start();
modbus_Timer
.
Enabled
=
true
;
modbus_Timer
.
Enabled
=
true
;
btOpenCOM
.
Enabled
=
false
;
btOpenCOM
.
Enabled
=
false
;
btCloseCOM
.
Enabled
=
true
;
btCloseCOM
.
Enabled
=
true
;
if
(
bCretaThead
==
false
)
if
(
bCretaThead
==
false
)
{
{
bCretaThead
=
true
;
if
(
bCretaThead
==
false
)
ModbusPollThread
=
new
Thread
(
vModbusPoll
);
{
ModbusPollThread
.
Start
();
bCretaThead
=
true
;
ModbusPollThread
=
new
Thread
(
vModbusPoll
);
ModbusPollThread
.
Start
();
ModbusPollThread
.
Priority
=
ThreadPriority
.
Normal
;
ModbusControlThread
=
new
Thread
(
ModualControl
.
vControl_Poll
);
ModbusControlThread
.
Start
();
ModbusControlThread
=
new
Thread
(
ModualControl
.
vControl_Poll
);
}
ModbusControlThread
.
Start
();
else
ModbusControlThread
.
Priority
=
ThreadPriority
.
BelowNormal
;
{
ModbusPollThread
.
Resume
();
MqttReceiveThread
=
new
Thread
(
vMqtt_Receive_Handle
);
MqttReceiveThread
.
Start
();
MqttReceiveThread
.
Priority
=
ThreadPriority
.
AboveNormal
;
Task
task1
=
Task
.
Factory
.
StartNew
(
async
()
=>
{
await
ModualControl
.
vControl_Res_Poll
();});
}
}
}
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" =>Open "
+
comPort
.
PortName
+
" sucessfully!"
);
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" =>Open "
+
comPort
.
PortName
+
" sucessfully!"
);
await
Subscribe
();
await
Subscribe
();
...
...
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