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
6d9c15c0
Commit
6d9c15c0
authored
Jun 14, 2019
by
leon.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Send down control and relate alarm
parent
ae9d62a8
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
437 additions
and
329 deletions
+437
-329
ModualAlarm.cs
ModbusDemo/Common/ModualAlarm.cs
+9
-7
ModbusComm.cs
ModbusDemo/MessageFormat/ModbusComm.cs
+2
-2
ModualControl.cs
ModbusDemo/MessageFormat/ModualControl.cs
+398
-6
ModualPoll.cs
ModbusDemo/Modular/ModualPoll.cs
+3
-2
Form1.cs
ModbusDemo/windows/Form1.cs
+25
-312
No files found.
ModbusDemo/Common/ModualAlarm.cs
View file @
6d9c15c0
...
@@ -12,17 +12,19 @@ namespace ModbusDemo.Common
...
@@ -12,17 +12,19 @@ namespace ModbusDemo.Common
public
static
class
ModualAlarm
public
static
class
ModualAlarm
{
{
public
const
ushort
usChannelNumber
=
8
;
public
const
ushort
usChannelNumber
=
8
;
private
const
string
sAlarmTopic
=
"Witium/WTDS78X/66666666/Alarm"
;
public
static
string
sAlarmJson
;
public
static
string
sAlarmJson
;
public
static
string
sAlarmJsonPackage
;
public
static
string
sAlarmJsonPackage
;
//private IMqttClient mqttClient = null;
//private IMqttClient mqttClient = null;
public
enum
eAlarmCode
:
ushort
public
enum
eAlarmCode
:
ushort
{
{
ChannelNoError
=
0
,
ChannelNoError
=
0
,
ChannelCommunError
=
1
,
ChannelCommunError
=
1
,
ChannelCollectDataError
=
2
,
ChannelCollectDataError
=
2
,
ChannelDataRespError
=
4
,
ChannelDataRespError
=
4
,
ChannelDataChangeError
=
8
ChannelDataChangeError
=
8
,
ChannelLevelError
=
16
,
ChannelControlError
=
32
,
}
}
public
static
void
vSetAlarmList
(
ushort
usAddress
,
ushort
[]
usChannel
,
ushort
[]
usChannelAlarmcode
)
public
static
void
vSetAlarmList
(
ushort
usAddress
,
ushort
[]
usChannel
,
ushort
[]
usChannelAlarmcode
)
{
{
...
@@ -46,7 +48,7 @@ namespace ModbusDemo.Common
...
@@ -46,7 +48,7 @@ namespace ModbusDemo.Common
public
static
async
Task
vPublishAlarmPackageJson
(
IMqttClient
mqttClient
)
public
static
async
Task
vPublishAlarmPackageJson
(
IMqttClient
mqttClient
)
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
var
message
=
new
MqttApplicationMessageBuilder
()
.
WithTopic
(
sAlarmTopic
)
.
WithTopic
(
Form1
.
sAlarmTopic
)
.
WithPayload
(
sAlarmJson
)
.
WithPayload
(
sAlarmJson
)
.
WithAtMostOnceQoS
()
.
WithAtMostOnceQoS
()
.
WithRetainFlag
(
false
)
.
WithRetainFlag
(
false
)
...
...
ModbusDemo/MessageFormat/ModbusComm.cs
View file @
6d9c15c0
...
@@ -200,7 +200,7 @@ namespace ModbusDemo.MessageFormat
...
@@ -200,7 +200,7 @@ namespace ModbusDemo.MessageFormat
return
ModbusErrorCode
;
return
ModbusErrorCode
;
}
}
}
}
public
static
eModbusErrorCode
Modbus_WriteSingleCoil
(
byte
slaveAddress
,
ushort
startAddress
,
bool
[]
value
)
public
static
eModbusErrorCode
Modbus_WriteSingleCoil
(
byte
slaveAddress
,
ushort
startAddress
,
bool
value
)
{
{
object
oLock
=
new
object
();
object
oLock
=
new
object
();
lock
(
oLock
)
lock
(
oLock
)
...
@@ -208,7 +208,7 @@ namespace ModbusDemo.MessageFormat
...
@@ -208,7 +208,7 @@ namespace ModbusDemo.MessageFormat
eModbusErrorCode
ModbusErrorCode
;
eModbusErrorCode
ModbusErrorCode
;
try
try
{
{
Modbus
.
Write
MultipleCoils
(
slaveAddress
,
startAddress
,
value
);
Modbus
.
Write
SingleCoil
(
slaveAddress
,
startAddress
,
value
);
ModbusErrorCode
=
eModbusErrorCode
.
MB_ENOERR
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ENOERR
;
}
}
catch
(
Exception
exception
)
catch
(
Exception
exception
)
...
...
ModbusDemo/MessageFormat/ModualControl.cs
View file @
6d9c15c0
...
@@ -6,14 +6,19 @@ using System.Threading.Tasks;
...
@@ -6,14 +6,19 @@ using System.Threading.Tasks;
using
MQTTnet
;
using
MQTTnet
;
using
MQTTnet.Client
;
using
MQTTnet.Client
;
using
Newtonsoft.Json
;
using
Newtonsoft.Json
;
using
Newtonsoft.Json.Linq
;
using
ModbusDemo.windows
;
using
ModbusDemo
;
using
ModbusDemo.Modular
;
using
ModbusDemo.Common
;
namespace
ModbusDemo.MessageFormat
namespace
ModbusDemo.MessageFormat
{
{
public
class
ModualControl
public
class
ModualControl
{
{
public
class
Op
public
class
Op
{
{
public
UInt64
duration
{
get
;
set
;
}
public
ushort
duration
{
get
;
set
;
}
public
UInt64
startTime
{
get
;
set
;}
public
UInt64
startTime
{
get
;
set
;
}
public
int
d0
{
get
;
set
;
}
public
int
d0
{
get
;
set
;
}
public
int
d1
{
get
;
set
;
}
public
int
d1
{
get
;
set
;
}
public
int
d2
{
get
;
set
;
}
public
int
d2
{
get
;
set
;
}
...
@@ -23,6 +28,14 @@ namespace ModbusDemo.MessageFormat
...
@@ -23,6 +28,14 @@ namespace ModbusDemo.MessageFormat
public
int
d6
{
get
;
set
;
}
public
int
d6
{
get
;
set
;
}
public
int
d7
{
get
;
set
;
}
public
int
d7
{
get
;
set
;
}
public
UInt64
ts
{
get
;
set
;
}
public
UInt64
ts
{
get
;
set
;
}
public
bool
bD0HasConfig
{
get
;
set
;
}
public
bool
bD1HasConfig
{
get
;
set
;
}
public
bool
bD2HasConfig
{
get
;
set
;
}
public
bool
bD3HasConfig
{
get
;
set
;
}
public
bool
bD4HasConfig
{
get
;
set
;
}
public
bool
bD5HasConfig
{
get
;
set
;
}
public
bool
bD6HasConfig
{
get
;
set
;
}
public
bool
bD7HasConfig
{
get
;
set
;
}
}
}
public
class
SsItem
public
class
SsItem
...
@@ -36,10 +49,389 @@ namespace ModbusDemo.MessageFormat
...
@@ -36,10 +49,389 @@ namespace ModbusDemo.MessageFormat
public
string
taskId
{
get
;
set
;
}
public
string
taskId
{
get
;
set
;
}
public
List
<
SsItem
>
ss
{
get
;
set
;
}
public
List
<
SsItem
>
ss
{
get
;
set
;
}
}
}
public
static
List
<
MqttRoot
>
liModuualControl
=
new
List
<
MqttRoot
>();
public
class
ModualCntrol
public
static
void
vGetaWay_Receive_Control
(
string
sDataString
)
{
{
public
bool
bStartControl
;
MqttRoot
cControl
=
JsonConvert
.
DeserializeObject
<
MqttRoot
>(
sDataString
);
public
byte
sbModbusID
;
public
byte
sbModualIndex
;
public
bool
[]
bControlData
;
public
ushort
usDurationTime
;
public
UInt64
ulStartTime
;
public
bool
[]
bControlBit
=
new
bool
[
8
];
}
public
static
List
<
ModualCntrol
>
liModuualControl
=
new
List
<
ModualCntrol
>();
public
static
void
vGetaWay_Receive_Control
(
string
sDataString
,
out
string
stResponseData
)
{
byte
byModualIndex
=
0
;
MqttRoot
cControl
=
new
MqttRoot
();
var
jsonObj
=
new
JObject
();
try
{
cControl
=
JsonConvert
.
DeserializeObject
<
MqttRoot
>(
sDataString
);
jsonObj
=
JObject
.
Parse
(
sDataString
);
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
exception
+
"Control Mqtt Parse Fail"
);
}
for
(
int
i
=
0
;
i
<
cControl
.
ss
.
Count
;
i
++)
{
bool
[]
btReadTempData
=
new
bool
[
6
];
ModbusComm
.
eModbusErrorCode
ModbusErrorCode
=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
;
ModualCntrol
cModualCntrol
=
new
ModualCntrol
{
sbModbusID
=
byte
.
Parse
(
cControl
.
ss
[
i
].
addr
),
usDurationTime
=
cControl
.
ss
[
i
].
op
.
duration
,
ulStartTime
=
cControl
.
ss
[
i
].
op
.
startTime
};
ModbusErrorCode
|=
ModbusComm
.
Modbus_ReadCoilsTask
(
out
cModualCntrol
.
bControlData
,
cModualCntrol
.
sbModbusID
,
16
,
6
);
for
(
byte
j
=
0
;
j
<
ModualPoll
.
byX66cNumber
;
j
++)
{
if
(
cModualCntrol
.
sbModbusID
==
ModualPoll
.
byX66cIdlist
[
j
])
{
byModualIndex
=
j
;
cModualCntrol
.
sbModualIndex
=
j
;
break
;
}
else
{
byModualIndex
=
0
;
cModualCntrol
.
sbModualIndex
=
j
;
}
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
]
!=
null
)
{
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d0"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD0HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d0
);
cModualCntrol
.
bControlBit
[
0
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d1"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD1HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d1
);
cModualCntrol
.
bControlBit
[
1
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d2"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD2HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d2
);
cModualCntrol
.
bControlBit
[
2
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d3"
]
!=
null
)
{
cControl
.
ss
[
0
].
op
.
bD3HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d3
);
cModualCntrol
.
bControlBit
[
3
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d4"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD4HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d4
);
cModualCntrol
.
bControlBit
[
4
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d5"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD5HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d5
);
cModualCntrol
.
bControlBit
[
5
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d6"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD6HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d6
);
cModualCntrol
.
bControlBit
[
6
]
=
true
;
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"d7"
]
!=
null
)
{
cControl
.
ss
[
i
].
op
.
bD7HasConfig
=
true
;
cModualCntrol
.
bControlData
[
i
]
=
Convert
.
ToBoolean
(
cControl
.
ss
[
i
].
op
.
d7
);
cModualCntrol
.
bControlBit
[
7
]
=
true
;
}
if
(
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
cModualCntrol
.
ulStartTime
<=
Form1
.
GetTimeStamp
())
{
cModualCntrol
.
bStartControl
=
true
;
ModbusErrorCode
|=
ModbusComm
.
Modbus_WriteMultipleCoils
(
cModualCntrol
.
sbModbusID
,
16
,
cModualCntrol
.
bControlData
);
ModbusErrorCode
|=
ModbusComm
.
Modbus_ReadCoilsTask
(
out
btReadTempData
,
cModualCntrol
.
sbModbusID
,
16
,
6
);
}
if
(
jsonObj
[
"ss"
][
i
][
"op"
][
"ts"
]
!=
null
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"ts"
]
=
Form1
.
GetTimeStamp
();
}
if
(
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
{
liModuualControl
.
Add
(
cModualCntrol
);
}
else
if
(
cModualCntrol
.
bStartControl
==
false
)
{
liModuualControl
.
Add
(
cModualCntrol
);
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD0HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d0"
]
=
1
;
if
(
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
0
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
0
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD0HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d0"
]
=
0
;
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
0
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
0
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
if
(!
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
)
&&
byModualIndex
!=
0
&&
cModualCntrol
.
bStartControl
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
0
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD1HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d1"
]
=
1
;
if
(
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
1
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
1
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD1HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d1"
]
=
0
;
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
1
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
1
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
if
(!
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
)
&&
byModualIndex
!=
0
&&
cModualCntrol
.
bStartControl
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
1
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD2HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d2"
]
=
1
;
if
(
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
2
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
2
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD2HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d2"
]
=
0
;
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
2
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
2
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
if
(!
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
)
&&
byModualIndex
!=
0
&&
cModualCntrol
.
bStartControl
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
2
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD3HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d3"
]
=
1
;
if
(
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
3
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
3
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD3HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d3"
]
=
0
;
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
3
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
3
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
if
(!
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
)
&&
byModualIndex
!=
0
&&
cModualCntrol
.
bStartControl
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
3
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD4HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d4"
]
=
1
;
if
(
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
4
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
4
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD4HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d4"
]
=
0
;
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
4
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
4
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
if
(!
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
)
&&
byModualIndex
!=
0
&&
cModualCntrol
.
bStartControl
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
4
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD5HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d5"
]
=
1
;
if
(
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
5
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
cModualCntrol
.
bStartControl
==
true
&&
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
5
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD5HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d5"
]
=
0
;
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
&&
byModualIndex
!=
0
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
if
(
Enumerable
.
SequenceEqual
(
cModualCntrol
.
bControlData
,
btReadTempData
)
&&
byModualIndex
!=
0
&&
cModualCntrol
.
bStartControl
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
byModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
if
(
cControl
.
ss
[
i
].
op
.
bD6HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d6"
]
=
1
;
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD6HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d6"
]
=
0
;
}
if
(
cControl
.
ss
[
i
].
op
.
bD7HasConfig
==
true
&&
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d7"
]
=
1
;
}
else
if
(
cControl
.
ss
[
i
].
op
.
bD7HasConfig
==
true
)
{
jsonObj
[
"ss"
][
i
][
"op"
][
"d7"
]
=
0
;
}
}
}
stResponseData
=
jsonObj
.
ToString
();
}
public
static
void
vControl_Poll
()
{
while
(
true
)
{
for
(
sbyte
i
=
0
;
i
<
liModuualControl
.
Count
;
i
++)
{
if
(
liModuualControl
[
i
].
bStartControl
==
false
)
{
if
(
liModuualControl
[
i
].
ulStartTime
<=
Form1
.
GetTimeStamp
())
{
ModbusComm
.
eModbusErrorCode
ModbusErrorCode
=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
;
liModuualControl
[
i
].
bStartControl
=
true
;
bool
[]
bControlData
=
new
bool
[
6
];
bool
[]
btReadTempData
;
ModbusErrorCode
|=
ModbusComm
.
Modbus_ReadCoilsTask
(
out
bControlData
,
liModuualControl
[
i
].
sbModbusID
,
16
,
6
);
for
(
sbyte
j
=
0
;
j
<
6
;
j
++)
{
if
(
liModuualControl
[
i
].
bControlBit
[
j
]
==
true
)
{
bControlData
[
j
]
=
liModuualControl
[
i
].
bControlData
[
j
];
}
else
{
liModuualControl
[
i
].
bControlData
[
j
]
=
bControlData
[
j
];
}
ModbusErrorCode
|=
ModbusComm
.
Modbus_WriteMultipleCoils
(
liModuualControl
[
i
].
sbModbusID
,
16
,
bControlData
);
ModbusErrorCode
|=
ModbusComm
.
Modbus_ReadCoilsTask
(
out
btReadTempData
,
liModuualControl
[
i
].
sbModbusID
,
16
,
6
);
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
for
(
sbyte
k
=
0
;
k
<
6
;
k
++)
{
if
(
liModuualControl
[
i
].
bControlBit
[
k
]
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
}
else
if
(
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
for
(
sbyte
k
=
0
;
k
<
6
;
k
++)
{
if
(
liModuualControl
[
i
].
bControlBit
[
k
]
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
Enumerable
.
SequenceEqual
(
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
}
}
}
}
else
{
if
((
liModuualControl
[
i
].
usDurationTime
+
liModuualControl
[
i
].
ulStartTime
)
<=
Form1
.
GetTimeStamp
())
{
ModbusComm
.
eModbusErrorCode
ModbusErrorCode
=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
;
bool
[]
bControlData
=
new
bool
[
6
];
bool
[]
btReadTempData
;
ModbusErrorCode
|=
ModbusComm
.
Modbus_ReadCoilsTask
(
out
bControlData
,
liModuualControl
[
i
].
sbModbusID
,
16
,
6
);
for
(
sbyte
j
=
0
;
j
<
6
;
j
++)
{
if
(
liModuualControl
[
i
].
bControlBit
[
j
]
==
true
)
{
bControlData
[
j
]
=
!
liModuualControl
[
i
].
bControlData
[
j
];
}
}
ModbusErrorCode
|=
ModbusComm
.
Modbus_WriteMultipleCoils
(
liModuualControl
[
i
].
sbModbusID
,
16
,
bControlData
);
ModbusErrorCode
|=
ModbusComm
.
Modbus_ReadCoilsTask
(
out
btReadTempData
,
liModuualControl
[
i
].
sbModbusID
,
16
,
6
);
if
(
ModbusErrorCode
!=
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
for
(
sbyte
k
=
0
;
k
<
6
;
k
++)
{
if
(
liModuualControl
[
i
].
bControlBit
[
k
]
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
|=
(
ushort
)
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
}
else
if
(
ModbusErrorCode
==
ModbusComm
.
eModbusErrorCode
.
MB_EMASTERNOERR
)
{
for
(
sbyte
k
=
0
;
k
<
6
;
k
++)
{
if
(
liModuualControl
[
i
].
bControlBit
[
k
]
==
true
)
{
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelCommunError
;
if
(
Enumerable
.
SequenceEqual
(
bControlData
,
btReadTempData
))
ModualPoll
.
cx66c
.
sX66cAlarmStruct
[
liModuualControl
[
i
].
sbModualIndex
].
usNowAlarmCode
[
5
]
&=
(
ushort
)~
ModualAlarm
.
eAlarmCode
.
ChannelControlError
;
}
}
}
liModuualControl
.
Remove
(
liModuualControl
[
i
]);
break
;
}
}
}
}
}
}
}
}
}
}
ModbusDemo/Modular/ModualPoll.cs
View file @
6d9c15c0
...
@@ -10,6 +10,7 @@ using MQTTnet;
...
@@ -10,6 +10,7 @@ using MQTTnet;
using
MQTTnet.Client
;
using
MQTTnet.Client
;
using
Newtonsoft.Json
;
using
Newtonsoft.Json
;
using
ModbusDemo
;
using
ModbusDemo
;
namespace
ModbusDemo.Modular
namespace
ModbusDemo.Modular
{
{
class
ModualPoll
class
ModualPoll
...
@@ -70,8 +71,8 @@ namespace ModbusDemo.Modular
...
@@ -70,8 +71,8 @@ namespace ModbusDemo.Modular
public
static
modual
.
pcx78C
cx78c
=
new
modual
.
pcx78C
(
byX78cIdlist
);
public
static
modual
.
pcx78C
cx78c
=
new
modual
.
pcx78C
(
byX78cIdlist
);
#if DEBUG_TEST
#if DEBUG_TEST
public
const
byte
byX66cNumber
=
2
;
public
const
byte
byX66cNumber
=
3
;
public
static
byte
[]
byX66cIdlist
=
{
16
,
31
};
public
static
byte
[]
byX66cIdlist
=
{
16
,
31
,
35
};
#else
#else
public
const
byte
byX66cNumber
=
32
;
public
const
byte
byX66cNumber
=
32
;
public
static
byte
[]
byX66cIdlist
=
{
4
,
5
,
7
,
8
,
9
,
10
,
14
,
15
,
19
,
22
,
23
,
24
,
26
,
30
,
32
,
36
,
37
,
38
,
40
,
41
,
42
,
43
,
44
,
46
,
47
,
48
,
49
,
50
,
51
,
52
,
54
,
57
};
public
static
byte
[]
byX66cIdlist
=
{
4
,
5
,
7
,
8
,
9
,
10
,
14
,
15
,
19
,
22
,
23
,
24
,
26
,
30
,
32
,
36
,
37
,
38
,
40
,
41
,
42
,
43
,
44
,
46
,
47
,
48
,
49
,
50
,
51
,
52
,
54
,
57
};
...
...
ModbusDemo/windows/Form1.cs
View file @
6d9c15c0
...
@@ -27,9 +27,8 @@ namespace ModbusDemo
...
@@ -27,9 +27,8 @@ namespace ModbusDemo
public
partial
class
Form1
:
Form
public
partial
class
Form1
:
Form
{
{
//Pond FunctionalService = new Pond();
//Pond FunctionalService = new Pond();
const
bool
LOCALDEBUG
=
fals
e
;
const
bool
LOCALDEBUG
=
tru
e
;
private
bool
bStart
=
false
;
private
bool
bStart
=
false
;
FileStream
fsl
;
AutoResetEvent
exitEvent
;
AutoResetEvent
exitEvent
;
private
int
waitTime
;
private
int
waitTime
;
private
Thread
t_UpgradeConn
;
private
Thread
t_UpgradeConn
;
...
@@ -39,7 +38,7 @@ namespace ModbusDemo
...
@@ -39,7 +38,7 @@ namespace ModbusDemo
BackgroundWorker
m_bgw0
=
new
BackgroundWorker
();
BackgroundWorker
m_bgw0
=
new
BackgroundWorker
();
bool
m_Isbgw0_CanContinueRun
=
true
;
bool
m_Isbgw0_CanContinueRun
=
true
;
bool
State_enable
=
true
;
bool
State_enable
=
true
;
bool
bCretaThead
=
false
;
public
static
ModbusDemo
.
windows
.
Totxt
totxt
=
new
windows
.
Totxt
(
AppDomain
.
CurrentDomain
.
BaseDirectory
+
@"/log/Log.txt"
);
public
static
ModbusDemo
.
windows
.
Totxt
totxt
=
new
windows
.
Totxt
(
AppDomain
.
CurrentDomain
.
BaseDirectory
+
@"/log/Log.txt"
);
public
Form1
()
public
Form1
()
...
@@ -69,45 +68,28 @@ namespace ModbusDemo
...
@@ -69,45 +68,28 @@ namespace ModbusDemo
System
.
Environment
.
Exit
(
0
);
System
.
Environment
.
Exit
(
0
);
}
}
#
region
参数
/// <summary>
/// 串口变量
/// </summary>
SerialPort
serialPort
;
string
portName
;
int
baudRate
;
int
dataBits
;
Parity
parity
;
StopBits
stopBits
;
private
SerialPort
comPort
=
new
SerialPort
();
private
SerialPort
comPort
=
new
SerialPort
();
public
static
ModbusMaster
master
;
public
static
ModbusMaster
master
;
//////////////////////////////////////////////////////////////
private
MethodInvoker
MQTT_thread
;
string
mqttIp
;
string
mqttIp
;
string
mqttClientId
;
string
mqttClientId
;
int
mqttPort
;
int
mqttPort
;
string
mqttUsername
;
string
mqttUsername
;
string
mqttPwd
;
string
mqttPwd
;
string
mqttTopic
;
public
const
string
mqttSn
=
"66666666"
;
string
mqttSubscribeTopic
;
public
const
string
mqttSubscribeControlTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Ctrl"
;
string
mqttBackTopic
;
public
const
string
mqttBackTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Back"
;
string
mqttSn
=
"66666666"
;
public
const
string
mqttTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Data"
;
public
const
string
sAlarmTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Alarm"
;
string
mqttData
;
//数据
string
mqttData
;
//数据
string
mqttData_Back
;
//数据
string
mqttData_Back
;
//数据
private
IMqttClient
mqttClient
=
null
;
private
IMqttClient
mqttClient
=
null
;
private
bool
isReconnect
=
true
;
private
bool
isReconnect
=
true
;
ushort
address
;
byte
ID
;
int
readyState
;
bool
[]
DI
=
new
bool
[
10
];
bool
[]
DO
=
new
bool
[
10
];
float
[]
AI
=
new
float
[
10
];
float
[]
AO
=
new
float
[
10
];
#
endregion
private
void
SetMqtt
()
private
void
SetMqtt
()
{
{
mqttTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Data"
;
if
(
LOCALDEBUG
==
true
)
if
(
LOCALDEBUG
==
true
)
{
{
mqttIp
=
"47.101.50.24"
;
mqttIp
=
"47.101.50.24"
;
...
@@ -125,12 +107,6 @@ namespace ModbusDemo
...
@@ -125,12 +107,6 @@ namespace ModbusDemo
mqttPwd
=
"Pasture37774020"
;
mqttPwd
=
"Pasture37774020"
;
}
}
}
}
private
void
SetTopic
()
{
mqttSubscribeTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Ctrl"
;
mqttBackTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Back"
;
}
void
m_bgw0_DoWork
(
object
sender
,
DoWorkEventArgs
e
)
void
m_bgw0_DoWork
(
object
sender
,
DoWorkEventArgs
e
)
{
{
while
(
m_Isbgw0_CanContinueRun
)
while
(
m_Isbgw0_CanContinueRun
)
...
@@ -156,66 +132,12 @@ namespace ModbusDemo
...
@@ -156,66 +132,12 @@ namespace ModbusDemo
}
}
#endregion
#endregion
#region 方法
public
void
control
(
byte
SlaveAddress
,
ushort
startAddress
,
bool
value
)
{
bool
OutputValue
;
int
Caution
=
0
;
bool
[]
StateValue
=
{
};
int
[]
WOnOff
=
{
0
};
//Modbus_ReadCoilsTask(out StateValue, SlaveAddress, 16, 6, out int signW);
//WOnOff = state.IO(StateValue);
if
(
WOnOff
[
0
]
==
1
)
{
if
(
startAddress
==
18
)
{
return
;
}
else
{
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
else
if
(
WOnOff
[
2
]==
1
)
{
if
(
startAddress
==
16
)
{
return
;
}
else
{
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
else
{
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
public
static
byte
[]
getByte
(
bool
[]
array
)
{
byte
[]
data
=
new
byte
[
array
.
Length
];
for
(
int
i
=
0
;
i
<
array
.
Length
;
i
++)
{
if
(
array
[
i
])
data
[
i
]
=
1
;
else
data
[
i
]
=
0
;
}
return
data
;
}
#endregion
#region mqtt服务
#region mqtt服务
//创建一个委托,是为访问TextBox控件服务的。
//创建一个委托,是为访问TextBox控件服务的。
public
delegate
void
UpdateTxt
(
string
msg
);
public
delegate
void
UpdateTxt
(
string
msg
);
//定义一个委托变量
//定义一个委托变量
public
UpdateTxt
updateTxt
;
public
UpdateTxt
updateTxt
;
private
Thread
ModbusControlThread
;
private
async
Task
Publish
()
private
async
Task
Publish
()
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
var
message
=
new
MqttApplicationMessageBuilder
()
...
@@ -229,7 +151,7 @@ namespace ModbusDemo
...
@@ -229,7 +151,7 @@ namespace ModbusDemo
await
mqttClient
.
PublishAsync
(
message
);
await
mqttClient
.
PublishAsync
(
message
);
}
}
}
}
private
async
Task
Back
()
private
async
Task
vControlResponse
Back
()
{
{
var
message
=
new
MqttApplicationMessageBuilder
()
var
message
=
new
MqttApplicationMessageBuilder
()
.
WithTopic
(
mqttBackTopic
)
.
WithTopic
(
mqttBackTopic
)
...
@@ -250,7 +172,7 @@ namespace ModbusDemo
...
@@ -250,7 +172,7 @@ namespace ModbusDemo
// Subscribe to a topic
// Subscribe to a topic
await
mqttClient
.
SubscribeAsync
(
new
TopicFilterBuilder
()
await
mqttClient
.
SubscribeAsync
(
new
TopicFilterBuilder
()
.
WithTopic
(
mqttSubscribeTopic
)
.
WithTopic
(
mqttSubscribe
Control
Topic
)
.
WithAtMostOnceQoS
()
.
WithAtMostOnceQoS
()
.
Build
()
.
Build
()
);
);
...
@@ -259,13 +181,12 @@ namespace ModbusDemo
...
@@ -259,13 +181,12 @@ namespace ModbusDemo
{
{
Console
.
WriteLine
(
"已订阅[{topic}]主题{Environment.NewLine}"
);
Console
.
WriteLine
(
"已订阅[{topic}]主题{Environment.NewLine}"
);
txtReceiveMessage
.
AppendText
(
$"已订阅[
{
mqttSubscribeTopic
}
]主题
{
Environment
.
NewLine
}
"
);
txtReceiveMessage
.
AppendText
(
$"已订阅[
{
mqttSubscribe
Control
Topic
}
]主题
{
Environment
.
NewLine
}
"
);
//totxt.Log($"已订阅[{mqttSubscribeTopic}]主题{Environment.NewLine}");
//totxt.Log($"已订阅[{mqttSubscribeTopic}]主题{Environment.NewLine}");
//txtReceiveMessage.AppendText($"已订阅[{Data}]主题{Environment.NewLine}");
//txtReceiveMessage.AppendText($"已订阅[{Data}]主题{Environment.NewLine}");
})));
})));
}
}
private
async
Task
ConnectMqttServerAsync
()
private
async
Task
ConnectMqttServerAsync
()
{
{
// Create a new MQTT client.
// Create a new MQTT client.
...
@@ -274,7 +195,6 @@ namespace ModbusDemo
...
@@ -274,7 +195,6 @@ namespace ModbusDemo
{
{
var
factory
=
new
MqttFactory
();
var
factory
=
new
MqttFactory
();
mqttClient
=
factory
.
CreateMqttClient
();
mqttClient
=
factory
.
CreateMqttClient
();
mqttClient
.
ApplicationMessageReceived
+=
MqttClient_ApplicationMessageReceived
;
mqttClient
.
ApplicationMessageReceived
+=
MqttClient_ApplicationMessageReceived
;
mqttClient
.
Connected
+=
MqttClient_Connected
;
mqttClient
.
Connected
+=
MqttClient_Connected
;
mqttClient
.
Disconnected
+=
MqttClient_Disconnected
;
mqttClient
.
Disconnected
+=
MqttClient_Disconnected
;
...
@@ -316,7 +236,6 @@ namespace ModbusDemo
...
@@ -316,7 +236,6 @@ namespace ModbusDemo
}
}
}
}
private
void
MqttClient_Connected
(
object
sender
,
EventArgs
e
)
private
void
MqttClient_Connected
(
object
sender
,
EventArgs
e
)
{
{
Invoke
((
new
Action
(()
=>
Invoke
((
new
Action
(()
=>
...
@@ -327,7 +246,6 @@ namespace ModbusDemo
...
@@ -327,7 +246,6 @@ namespace ModbusDemo
//Console.WriteLine("已连接到MQTT服务器!" + Environment.NewLine);
//Console.WriteLine("已连接到MQTT服务器!" + Environment.NewLine);
})));
})));
}
}
private
void
MqttClient_Disconnected
(
object
sender
,
EventArgs
e
)
private
void
MqttClient_Disconnected
(
object
sender
,
EventArgs
e
)
{
{
Invoke
((
new
Action
(()
=>
Invoke
((
new
Action
(()
=>
...
@@ -387,7 +305,6 @@ namespace ModbusDemo
...
@@ -387,7 +305,6 @@ namespace ModbusDemo
})));
})));
}
}
}
}
private
void
MqttClient_ApplicationMessageReceived
(
object
sender
,
MqttApplicationMessageReceivedEventArgs
e
)
private
void
MqttClient_ApplicationMessageReceived
(
object
sender
,
MqttApplicationMessageReceivedEventArgs
e
)
{
{
Invoke
((
new
Action
(()
=>
Invoke
((
new
Action
(()
=>
...
@@ -404,91 +321,17 @@ namespace ModbusDemo
...
@@ -404,91 +321,17 @@ namespace ModbusDemo
})));
})));
Invoke
((
new
Action
(
async
()
=>
Invoke
((
new
Action
(
async
()
=>
{
{
bool
on_off
=
true
;
string
passageway
;
string
stConvertString
;
string
stConvertString
;
//消息的内容
//消息的内容
txtReceiveMessage
.
AppendText
(
$">> Payload =
{
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
)}{
Environment
.
NewLine
}
"
);
txtReceiveMessage
.
AppendText
(
$">> Payload =
{
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
)}{
Environment
.
NewLine
}
"
);
totxt
.
Log
(
$">> Payload =
{
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
)}{
Environment
.
NewLine
}
"
);
totxt
.
Log
(
$">> Payload =
{
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
)}{
Environment
.
NewLine
}
"
);
stConvertString
=
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
);
stConvertString
=
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
);
Console
.
WriteLine
(
stConvertString
);
Console
.
WriteLine
(
stConvertString
);
ModualControl
.
vGetaWay_Receive_Control
(
stConvertString
);
if
(
e
.
ApplicationMessage
.
Topic
==
mqttSubscribeControlTopic
)
Ctrl
ctrl
=
JsonConvert
.
DeserializeObject
<
Ctrl
>(
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
));
Ctrl
ctrl_Back
=
new
Ctrl
();
Ctrl2
ctrl2
=
new
Ctrl2
();
List
<
Sss
>
ssses
=
new
List
<
Sss
>();
ssses
.
Add
(
new
Sss
());
List
<
Ss
>
ctrl_Back_ss
=
new
List
<
Ss
>();
ctrl_Back_ss
.
Add
(
new
Ss
());
string
txt
=
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
);
for
(
int
i
=
0
;
i
<
6
;
i
++)
{
if
(
txt
.
Contains
(
"d"
+
i
))
{
address
=
(
ushort
)(
16
+
i
);
ctrl2
.
taskId
=
ctrl
.
taskId
.
ToString
();
passageway
=
"d"
+
i
;
if
(
passageway
==
"d0"
)
{
readyState
=
ctrl
.
ss
[
0
].
op
.
d0
;
}
if
(
passageway
==
"d2"
)
{
readyState
=
ctrl
.
ss
[
0
].
op
.
d2
;
}
if
(
passageway
==
"d4"
)
{
readyState
=
ctrl
.
ss
[
0
].
op
.
d4
;
}
ssses
[
0
].
addr
=
ctrl
.
ss
[
0
].
getAddr
();
ssses
[
0
].
op
.
Add
(
"duration"
,
ctrl
.
ss
[
0
].
op
.
getDuration
());
ssses
[
0
].
op
.
Add
(
"startTime"
,
ctrl
.
ss
[
0
].
op
.
getStartTime
());
ssses
[
0
].
op
.
Add
(
passageway
,
readyState
);
ssses
[
0
].
op
.
Add
(
"ts"
,
GetTimeStamp
());
ctrl2
.
ss
=
ssses
;
}
if
(
ctrl
.
ss
[
0
].
op
.
d1
==
1
||
ctrl
.
ss
[
0
].
op
.
d0
==
1
||
ctrl
.
ss
[
0
].
op
.
d2
==
1
||
ctrl
.
ss
[
0
].
op
.
d3
==
1
||
ctrl
.
ss
[
0
].
op
.
d4
==
1
||
ctrl
.
ss
[
0
].
op
.
d5
==
1
)
{
on_off
=
true
;
}
else
on_off
=
false
;
}
ID
=
ctrl
.
ss
[
0
].
getAddr
();
ctrl_Back
.
taskId
=
ctrl
.
taskId
.
ToString
();
ctrl_Back_ss
[
0
]
=
ctrl
.
ss
[
0
];
totxt
.
Log
(
"我在469_ctrl_Back_ss[0] = ctrl.ss[0];"
);
//ctrl_Back.ss[0].op.setStartTime(long.Parse(GetTimeStamp()));
if
(
ctrl
.
ss
[
0
].
op
.
getStartTime
()
!=
0
)
{
{
Thread
objThread
=
new
Thread
(
new
ThreadStart
(
async
delegate
ModualControl
.
vGetaWay_Receive_Control
(
stConvertString
,
out
mqttData_Back
);
{
await
vControlResponseBack
();
await
ThreadMethodTxtAsync
(
Convert
.
ToInt32
(
ctrl
.
ss
[
0
].
op
.
getDuration
()));
}));
objThread
.
Start
();
}
}
if
(
State_enable
==
true
)
{
control
(
ctrl
.
ss
[
0
].
getAddr
(),
address
,
on_off
);
//根据下发确定控制通道
mqttData_Back
=
JsonConvert
.
SerializeObject
(
ctrl2
);
//Modbus_polling();
await
Publish
();
await
Back
();
}
//订阅内容部分jSON 解析转对象 根据modbusID或者设备类型处理设备消息
//解析控制部分
})));
})));
Invoke
((
new
Action
(()
=>
Invoke
((
new
Action
(()
=>
{
{
...
@@ -501,31 +344,6 @@ namespace ModbusDemo
...
@@ -501,31 +344,6 @@ namespace ModbusDemo
//totxt.Log($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}");
//totxt.Log($">> Retain = {e.ApplicationMessage.Retain}{Environment.NewLine}");
})));
})));
mqttData
=
null
;
mqttData
=
null
;
}
public
async
Task
ThreadMethodTxtAsync
(
int
n
)
{
this
.
BeginInvoke
(
updateTxt
,
"线程开始执行,执行"
+
n
+
"次,每一秒执行一次"
);
int
time
=
n
*
60
;
for
(
int
i
=
0
;
i
<
time
;
i
++)
{
State_enable
=
false
;
this
.
BeginInvoke
(
updateTxt
,
i
.
ToString
());
//一秒 执行一次
Thread
.
Sleep
(
1000
);
if
(
i
>
time
-
2
)
{
control
(
ID
,
address
,
false
);
//根据下发确定控制通道
control
(
ID
,
(
ushort
)(
address
+
1
),
false
);
//根据下发确定控制通道
//Modbus_polling();
await
Publish
();
time
=
0
;
n
=
0
;
State_enable
=
true
;
}
}
this
.
BeginInvoke
(
updateTxt
,
"线程结束"
);
}
}
#endregion
#endregion
...
@@ -533,18 +351,6 @@ namespace ModbusDemo
...
@@ -533,18 +351,6 @@ namespace ModbusDemo
private
static
bool
bSetDataIsRunning
=
false
;
private
static
bool
bSetDataIsRunning
=
false
;
private
Thread
ModbusPollThread
;
private
Thread
ModbusPollThread
;
private
bool
bModbusPollThreadStart
=
false
;
private
bool
bModbusPollThreadStart
=
false
;
private
void
GetData
()
{
//timer.Interval = 12000;
//timer.Enabled = true;
//timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
//timer.Start();
//timer.Elapsed += (o, a) =>
//{
// SetData();
//};
}
private
void
vModbusPoll
()
private
void
vModbusPoll
()
{
{
while
(
bModbusPollThreadStart
)
while
(
bModbusPollThreadStart
)
...
@@ -603,92 +409,6 @@ namespace ModbusDemo
...
@@ -603,92 +409,6 @@ namespace ModbusDemo
bSetDataIsRunning
=
false
;
bSetDataIsRunning
=
false
;
}
}
}
}
}
#region PoolTest
#endregion
//声明委托
private
delegate
void
ShowMessageDelegate
(
string
message
);
private
void
ShowMessage
(
string
message
)
{
if
(
this
.
InvokeRequired
)
{
ShowMessageDelegate
showMessageDelegate
=
ShowMessage
;
this
.
Invoke
(
showMessageDelegate
,
new
object
[]
{
message
});
}
else
{
//txtbox.Text = message;
//totxt.Log(message);
}
}
#endregion
#region json字符串转对象
/// <summary>
/// json字符串转对象
/// </summary>
/// <typeparam name="RData">接收的对象</typeparam>
/// <param name="rd">json字符串</param>
/// <returns></returns>
public
RData
DataConversion
<
RData
>(
string
rd
)
{
try
{
return
JsonConvert
.
DeserializeObject
<
RData
>(
rd
);
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
ex
.
Message
);
return
default
(
RData
);
}
}
public
UData
DataConverion
<
UData
>(
string
ud
)
{
try
{
return
JsonConvert
.
DeserializeObject
<
UData
>(
ud
);
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
ex
.
Message
);
return
default
(
UData
);
}
}
/// <summary>
/// 对象转json字符串
/// </summary>
/// <param name="ud">上传实体</param>
/// <returns></returns>
public
string
DataConversion
(
UData
ud
)
{
try
{
return
JsonConvert
.
SerializeObject
(
ud
);
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
ex
.
Message
);
return
""
;
}
}
/// <summary>
///
/// </summary>
/// <param name="rD"></param>
/// <returns></returns>
public
string
DataConversion
(
RData
rD
)
{
try
{
return
JsonConvert
.
SerializeObject
(
rD
);
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
ex
.
Message
);
return
""
;
}
}
}
#endregion
#endregion
...
@@ -706,12 +426,10 @@ namespace ModbusDemo
...
@@ -706,12 +426,10 @@ namespace ModbusDemo
MonitorThread
.
IsBackground
=
true
;
MonitorThread
.
IsBackground
=
true
;
MonitorThread
.
Start
();
MonitorThread
.
Start
();
}
}
public
void
MonitorThreadStop
()
public
void
MonitorThreadStop
()
{
{
network
.
Set
();
network
.
Set
();
}
}
private
void
MonitorNetWork
()
private
void
MonitorNetWork
()
{
{
while
(!
network
.
WaitOne
(
500
))
while
(!
network
.
WaitOne
(
500
))
...
@@ -819,8 +537,6 @@ namespace ModbusDemo
...
@@ -819,8 +537,6 @@ namespace ModbusDemo
// isconn = true;
// isconn = true;
return
isconn
;
return
isconn
;
}
}
#endregion
#endregion
#region 更新线程
#region 更新线程
...
@@ -931,7 +647,6 @@ namespace ModbusDemo
...
@@ -931,7 +647,6 @@ namespace ModbusDemo
const
int
Singlepackage
=
100
;
const
int
Singlepackage
=
100
;
const
int
PerBlockPacket
=
Singlepackage
;
const
int
PerBlockPacket
=
Singlepackage
;
const
int
MB_ENOERR
=
0
;
int
iFileSizeNow
=
0
;
int
iFileSizeNow
=
0
;
int
iFileSize
=
0
;
int
iFileSize
=
0
;
int
iMaxBufSize
=
1024
*
100
;
int
iMaxBufSize
=
1024
*
100
;
...
@@ -944,7 +659,6 @@ namespace ModbusDemo
...
@@ -944,7 +659,6 @@ namespace ModbusDemo
ushort
[]
usTrans
=
new
ushort
[
Singlepackage
];
ushort
[]
usTrans
=
new
ushort
[
Singlepackage
];
UInt16
[]
u16unlock
=
{
0x5749
,
0x5449
,
0x554D
};
UInt16
[]
u16unlock
=
{
0x5749
,
0x5449
,
0x554D
};
int
ret
;
int
errCnt
=
0
;
int
errCnt
=
0
;
FileStream
fsl
=
File
.
Open
(
ImagefilePath
,
FileMode
.
Open
,
FileAccess
.
ReadWrite
);
FileStream
fsl
=
File
.
Open
(
ImagefilePath
,
FileMode
.
Open
,
FileAccess
.
ReadWrite
);
...
@@ -1176,19 +890,15 @@ namespace ModbusDemo
...
@@ -1176,19 +890,15 @@ namespace ModbusDemo
private
async
void
SubscribeBut_Click
(
object
sender
,
EventArgs
e
)
private
async
void
SubscribeBut_Click
(
object
sender
,
EventArgs
e
)
{
{
SetTopic
();
await
Subscribe
();
await
Subscribe
();
}
}
private
MethodInvoker
simpleDelegate1
;
private
MethodInvoker
MQTT_thread
;
[
Obsolete
]
[
Obsolete
]
private
void
btOpenCOM_Click
(
object
sender
,
EventArgs
e
)
private
void
btOpenCOM_Click
(
object
sender
,
EventArgs
e
)
{
{
if
(
LOCALDEBUG
==
true
)
if
(
LOCALDEBUG
==
true
)
{
{
comPort
.
PortName
=
"COM
14
"
;
comPort
.
PortName
=
"COM
6
"
;
comPort
.
BaudRate
=
9600
;
comPort
.
BaudRate
=
9600
;
comPort
.
Parity
=
Parity
.
None
;
comPort
.
Parity
=
Parity
.
None
;
comPort
.
StopBits
=
StopBits
.
One
;
comPort
.
StopBits
=
StopBits
.
One
;
...
@@ -1230,16 +940,20 @@ namespace ModbusDemo
...
@@ -1230,16 +940,20 @@ namespace ModbusDemo
comPort
.
Open
();
comPort
.
Open
();
isReconnect
=
true
;
isReconnect
=
true
;
SetMqtt
();
SetMqtt
();
if
(
ModbusPollThread
==
null
)
if
(
bCretaThead
==
false
)
{
{
bCretaThead
=
true
;
ModbusPollThread
=
new
Thread
(
vModbusPoll
);
ModbusPollThread
=
new
Thread
(
vModbusPoll
);
ModbusPollThread
.
Start
();
ModbusPollThread
.
Start
();
ModbusControlThread
=
new
Thread
(
ModualControl
.
vControl_Poll
);
ModbusControlThread
.
Start
();
}
}
else
else
{
ModbusPollThread
.
Resume
();
ModbusPollThread
.
Resume
();
bModbusPollThreadStart
=
true
;
}
//simpleDelegate1 = new MethodInvoker(GetData);
bModbusPollThreadStart
=
true
;
//simpleDelegate1.BeginInvoke(null, null);
MQTT_thread
=
new
MethodInvoker
(
Sendout
);
MQTT_thread
=
new
MethodInvoker
(
Sendout
);
MQTT_thread
.
BeginInvoke
(
null
,
null
);
MQTT_thread
.
BeginInvoke
(
null
,
null
);
Task
.
Run
(
async
()
=>
{
await
ConnectMqttServerAsync
();
});
Task
.
Run
(
async
()
=>
{
await
ConnectMqttServerAsync
();
});
...
@@ -1267,7 +981,6 @@ namespace ModbusDemo
...
@@ -1267,7 +981,6 @@ namespace ModbusDemo
private
void
btCloseCOM_Click
(
object
sender
,
EventArgs
e
)
private
void
btCloseCOM_Click
(
object
sender
,
EventArgs
e
)
{
{
//Close comport first,then stop and dispose slave.
//Close comport first,then stop and dispose slave.
//timer.Stop();
bModbusPollThreadStart
=
false
;
bModbusPollThreadStart
=
false
;
if
(
ModbusPollThread
.
IsAlive
)
if
(
ModbusPollThread
.
IsAlive
)
ModbusPollThread
.
Suspend
();
ModbusPollThread
.
Suspend
();
...
...
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