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
4559c01e
Commit
4559c01e
authored
Jun 12, 2019
by
shaxuezheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modify Form1.cs
parent
df7736b6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
248 additions
and
208 deletions
+248
-208
Form1.cs
ModbusDemo/windows/Form1.cs
+248
-208
No files found.
ModbusDemo/windows/Form1.cs
View file @
4559c01e
...
@@ -26,11 +26,8 @@ namespace ModbusDemo
...
@@ -26,11 +26,8 @@ namespace ModbusDemo
{
{
public
partial
class
Form1
:
Form
public
partial
class
Form1
:
Form
{
{
bool
Debug_test
=
false
;
//打开测试功能
_414P
rtd
=
new
_414P
();
_478C
state
=
new
_478C
();
_418X
Analog
=
new
_418X
();
//Pond FunctionalService = new Pond();
//Pond FunctionalService = new Pond();
const
bool
LOCALDEBUG
=
false
;
private
bool
bStart
=
false
;
private
bool
bStart
=
false
;
FileStream
fsl
;
FileStream
fsl
;
AutoResetEvent
exitEvent
;
AutoResetEvent
exitEvent
;
...
@@ -93,7 +90,7 @@ namespace ModbusDemo
...
@@ -93,7 +90,7 @@ namespace ModbusDemo
string
mqttTopic
;
string
mqttTopic
;
string
mqttSubscribeTopic
;
string
mqttSubscribeTopic
;
string
mqttBackTopic
;
string
mqttBackTopic
;
string
mqttSn
=
"
20193261
"
;
string
mqttSn
=
"
66666666
"
;
string
mqttData
;
//数据
string
mqttData
;
//数据
string
mqttData_Back
;
//数据
string
mqttData_Back
;
//数据
private
IMqttClient
mqttClient
=
null
;
private
IMqttClient
mqttClient
=
null
;
...
@@ -111,13 +108,13 @@ namespace ModbusDemo
...
@@ -111,13 +108,13 @@ namespace ModbusDemo
private
void
SetMqtt
()
private
void
SetMqtt
()
{
{
mqttTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Data"
;
mqttTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Data"
;
if
(
Debug_test
==
true
)
if
(
LOCALDEBUG
==
true
)
{
{
mqttIp
=
"
172.16.1
.24"
;
mqttIp
=
"
47.101.50
.24"
;
mqttClientId
=
GetTimeStamp
()
+
"sxz"
;
mqttClientId
=
GetTimeStamp
()
+
"sxz"
;
mqttPort
=
1883
;
mqttPort
=
1883
;
mqttUsername
=
"
pasture
"
;
mqttUsername
=
"
root
"
;
mqttPwd
=
"
Pasture37774020
"
;
mqttPwd
=
"
public
"
;
}
}
else
else
{
{
...
@@ -127,7 +124,6 @@ namespace ModbusDemo
...
@@ -127,7 +124,6 @@ namespace ModbusDemo
mqttUsername
=
"pasture"
;
mqttUsername
=
"pasture"
;
mqttPwd
=
"Pasture37774020"
;
mqttPwd
=
"Pasture37774020"
;
}
}
}
}
private
void
SetTopic
()
private
void
SetTopic
()
{
{
...
@@ -137,7 +133,6 @@ namespace ModbusDemo
...
@@ -137,7 +133,6 @@ namespace ModbusDemo
}
}
void
m_bgw0_DoWork
(
object
sender
,
DoWorkEventArgs
e
)
void
m_bgw0_DoWork
(
object
sender
,
DoWorkEventArgs
e
)
{
{
while
(
m_Isbgw0_CanContinueRun
)
while
(
m_Isbgw0_CanContinueRun
)
{
{
int
_n
=
2
;
int
_n
=
2
;
...
@@ -149,7 +144,7 @@ namespace ModbusDemo
...
@@ -149,7 +144,7 @@ namespace ModbusDemo
Thread
.
Sleep
(
1000
);
Thread
.
Sleep
(
1000
);
}
}
}
}
#
region
时间戳
#region 时间戳
/// <summary>
/// <summary>
/// 获取时间戳
/// 获取时间戳
/// </summary>
/// </summary>
...
@@ -160,17 +155,17 @@ namespace ModbusDemo
...
@@ -160,17 +155,17 @@ namespace ModbusDemo
return
Convert
.
ToUInt64
(
ts
.
TotalSeconds
);
return
Convert
.
ToUInt64
(
ts
.
TotalSeconds
);
}
}
#
endregion
#endregion
#
region
方法
#region 方法
public
void
control
(
byte
SlaveAddress
,
ushort
startAddress
,
bool
value
)
public
void
control
(
byte
SlaveAddress
,
ushort
startAddress
,
bool
value
)
{
{
bool
OutputValue
;
bool
OutputValue
;
int
Caution
=
0
;
int
Caution
=
0
;
bool
[]
StateValue
=
{
};
bool
[]
StateValue
=
{
};
int
[]
WOnOff
=
{
0
};
int
[]
WOnOff
=
{
0
};
Modbus_ReadCoilsTask
(
out
StateValue
,
SlaveAddress
,
16
,
6
,
out
int
signW
);
//
Modbus_ReadCoilsTask(out StateValue, SlaveAddress, 16, 6, out int signW);
WOnOff
=
state
.
IO
(
StateValue
);
//
WOnOff = state.IO(StateValue);
if
(
WOnOff
[
0
]
==
1
)
if
(
WOnOff
[
0
]
==
1
)
{
{
if
(
startAddress
==
18
)
if
(
startAddress
==
18
)
...
@@ -179,8 +174,8 @@ namespace ModbusDemo
...
@@ -179,8 +174,8 @@ namespace ModbusDemo
}
}
else
else
{
{
Modbus_WriteSingleCoil
(
out
OutputValue
,
SlaveAddress
,
startAddress
,
value
,
out
Caution
);
//
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
Modbus_WriteSingleCoil
(
out
OutputValue
,
SlaveAddress
,
(
ushort
)(
startAddress
+
1
),
value
,
out
Caution
);
//
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
}
}
else
if
(
WOnOff
[
2
]==
1
)
else
if
(
WOnOff
[
2
]==
1
)
...
@@ -191,17 +186,16 @@ namespace ModbusDemo
...
@@ -191,17 +186,16 @@ namespace ModbusDemo
}
}
else
else
{
{
Modbus_WriteSingleCoil
(
out
OutputValue
,
SlaveAddress
,
startAddress
,
value
,
out
Caution
);
//
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
Modbus_WriteSingleCoil
(
out
OutputValue
,
SlaveAddress
,
(
ushort
)(
startAddress
+
1
),
value
,
out
Caution
);
//
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
}
}
else
else
{
{
Modbus_WriteSingleCoil
(
out
OutputValue
,
SlaveAddress
,
startAddress
,
value
,
out
Caution
);
//
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
Modbus_WriteSingleCoil
(
out
OutputValue
,
SlaveAddress
,
(
ushort
)(
startAddress
+
1
),
value
,
out
Caution
);
//
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
}
}
public
static
byte
[]
getByte
(
bool
[]
array
)
public
static
byte
[]
getByte
(
bool
[]
array
)
{
{
byte
[]
data
=
new
byte
[
array
.
Length
];
byte
[]
data
=
new
byte
[
array
.
Length
];
...
@@ -214,11 +208,9 @@ namespace ModbusDemo
...
@@ -214,11 +208,9 @@ namespace ModbusDemo
}
}
return
data
;
return
data
;
}
}
#endregion
#region mqtt服务
#
endregion
#
region
mqtt
服务
//创建一个委托,是为访问TextBox控件服务的。
//创建一个委托,是为访问TextBox控件服务的。
public
delegate
void
UpdateTxt
(
string
msg
);
public
delegate
void
UpdateTxt
(
string
msg
);
//定义一个委托变量
//定义一个委托变量
...
@@ -414,10 +406,13 @@ namespace ModbusDemo
...
@@ -414,10 +406,13 @@ namespace ModbusDemo
{
{
bool
on_off
=
true
;
bool
on_off
=
true
;
string
passageway
;
string
passageway
;
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
}
"
);
Console
.
WriteLine
(
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
));
stConvertString
=
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
);
Console
.
WriteLine
(
stConvertString
);
ModualControl
.
vGetaWay_Receive_Control
(
stConvertString
);
Ctrl
ctrl
=
JsonConvert
.
DeserializeObject
<
Ctrl
>(
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
));
Ctrl
ctrl
=
JsonConvert
.
DeserializeObject
<
Ctrl
>(
Encoding
.
UTF8
.
GetString
(
e
.
ApplicationMessage
.
Payload
));
Ctrl
ctrl_Back
=
new
Ctrl
();
Ctrl
ctrl_Back
=
new
Ctrl
();
Ctrl2
ctrl2
=
new
Ctrl2
();
Ctrl2
ctrl2
=
new
Ctrl2
();
...
@@ -452,7 +447,7 @@ namespace ModbusDemo
...
@@ -452,7 +447,7 @@ namespace ModbusDemo
ssses
[
0
].
op
.
Add
(
passageway
,
readyState
);
ssses
[
0
].
op
.
Add
(
passageway
,
readyState
);
ssses
[
0
].
op
.
Add
(
"ts"
,
GetTimeStamp
());
ssses
[
0
].
op
.
Add
(
"ts"
,
GetTimeStamp
());
ctrl2
.
ss
=
ssses
;
ctrl2
.
ss
=
ssses
;
}
}
if
(
ctrl
.
ss
[
0
].
op
.
d1
==
1
||
if
(
ctrl
.
ss
[
0
].
op
.
d1
==
1
||
ctrl
.
ss
[
0
].
op
.
d0
==
1
||
ctrl
.
ss
[
0
].
op
.
d0
==
1
||
ctrl
.
ss
[
0
].
op
.
d2
==
1
||
ctrl
.
ss
[
0
].
op
.
d2
==
1
||
...
@@ -483,13 +478,11 @@ namespace ModbusDemo
...
@@ -483,13 +478,11 @@ namespace ModbusDemo
objThread
.
Start
();
objThread
.
Start
();
}
}
if
(
State_enable
==
true
)
if
(
State_enable
==
true
)
{
{
control
(
ctrl
.
ss
[
0
].
getAddr
(),
address
,
on_off
);
//根据下发确定控制通道
control
(
ctrl
.
ss
[
0
].
getAddr
(),
address
,
on_off
);
//根据下发确定控制通道
mqttData_Back
=
JsonConvert
.
SerializeObject
(
ctrl2
);
mqttData_Back
=
JsonConvert
.
SerializeObject
(
ctrl2
);
Modbus_polling
();
//
Modbus_polling();
await
Publish
();
await
Publish
();
await
Back
();
await
Back
();
}
}
...
@@ -525,7 +518,7 @@ namespace ModbusDemo
...
@@ -525,7 +518,7 @@ namespace ModbusDemo
{
{
control
(
ID
,
address
,
false
);
//根据下发确定控制通道
control
(
ID
,
address
,
false
);
//根据下发确定控制通道
control
(
ID
,
(
ushort
)(
address
+
1
),
false
);
//根据下发确定控制通道
control
(
ID
,
(
ushort
)(
address
+
1
),
false
);
//根据下发确定控制通道
Modbus_polling
();
//
Modbus_polling();
await
Publish
();
await
Publish
();
time
=
0
;
time
=
0
;
n
=
0
;
n
=
0
;
...
@@ -534,13 +527,15 @@ namespace ModbusDemo
...
@@ -534,13 +527,15 @@ namespace ModbusDemo
}
}
this
.
BeginInvoke
(
updateTxt
,
"线程结束"
);
this
.
BeginInvoke
(
updateTxt
,
"线程结束"
);
}
}
#
endregion
#endregion
#
region
modbus
线程
#region modbus线程
private
static
bool
bSetDataIsRunning
=
false
;
private
Thread
ModbusPollThread
;
private
bool
bModbusPollThreadStart
=
false
;
private
void
GetData
()
private
void
GetData
()
{
{
timer
.
Interval
=
240
000
;
timer
.
Interval
=
9
000
;
timer
.
Enabled
=
true
;
timer
.
Enabled
=
true
;
timer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
timer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
timer
.
Start
();
timer
.
Start
();
...
@@ -553,7 +548,7 @@ namespace ModbusDemo
...
@@ -553,7 +548,7 @@ namespace ModbusDemo
private
void
Sendout
()
private
void
Sendout
()
{
{
Control
.
CheckForIllegalCrossThreadCalls
=
false
;
Control
.
CheckForIllegalCrossThreadCalls
=
false
;
MQTTTimer
.
Interval
=
480
000
;
MQTTTimer
.
Interval
=
7
000
;
MQTTTimer
.
Enabled
=
true
;
MQTTTimer
.
Enabled
=
true
;
MQTTTimer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
MQTTTimer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
MQTTTimer
.
Start
();
MQTTTimer
.
Start
();
...
@@ -562,7 +557,6 @@ namespace ModbusDemo
...
@@ -562,7 +557,6 @@ namespace ModbusDemo
try
try
{
{
await
Publish
();
await
Publish
();
totxt
.
Log
(
mqttData
+
Environment
.
NewLine
+
"\n"
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
...
@@ -584,24 +578,15 @@ namespace ModbusDemo
...
@@ -584,24 +578,15 @@ namespace ModbusDemo
{
{
await
Task
.
Run
(()
=>
await
Task
.
Run
(()
=>
{
{
totxt
.
Log
(
"开始执行"
);
Modbus_polling
();
ModualPoll
.
vAllNodePoll
();
mqttData
=
ModualPoll
.
stAll_Poll_Package
();
totxt
.
Log
(
"执行结束"
);
});
});
}
}
}
}
#
region
PoolTest
#
region
PoolTest
//pcPool cPool1 = new pcPool("East_transfer_pond");
#endregion
//double PoolTemper = 0;
#
endregion
//声明委托
//声明委托
public
static
ushort
usCommunFailTimes
=
0
;
private
delegate
void
ShowMessageDelegate
(
string
message
);
private
delegate
void
ShowMessageDelegate
(
string
message
);
private
void
ShowMessage
(
string
message
)
private
void
ShowMessage
(
string
message
)
{
{
...
@@ -862,41 +847,99 @@ namespace ModbusDemo
...
@@ -862,41 +847,99 @@ namespace ModbusDemo
}
}
private
async
void
Modbus_polling
()
private
async
void
Modbus_polling
()
{
{
//float EastTemperature = 0;
float
EastTemperature
=
0
;
//float Easternwaterlevel = 0;
float
Easternwaterlevel
=
0
;
//float WestWaterLevel = 0;
float
WestWaterLevel
=
0
;
//int sign = 0;
int
sign
=
0
;
//try
try
//{
{
// ushort[] temperature = { };
ushort
[]
temperature
=
{
};
// ushort[] type = { };
ushort
[]
type
=
{
};
// string[] vs = { };
string
[]
vs
=
{
};
// string[] s = { };
string
[]
s
=
{
};
// string[] s6 = { };
string
[]
s6
=
{
};
// ushort[] register = { };
ushort
[]
register
=
{
};
// ushort[] Inputtype = { };
ushort
[]
Inputtype
=
{
};
// ushort[] registerN = { };
ushort
[]
registerN
=
{
};
// ushort[] InputtypeN = { };
ushort
[]
InputtypeN
=
{
};
// bool[] EIO = { };
bool
[]
EIO
=
{
};
// bool[] SIO = { };
bool
[]
SIO
=
{
};
// bool[] WIO = { };
bool
[]
WIO
=
{
};
// int[] EOnOff = { 0 };
int
[]
EOnOff
=
{
0
};
// int[] SOnOff = { 0 };
int
[]
SOnOff
=
{
0
};
// int[] WOnOff = { 0 };
int
[]
WOnOff
=
{
0
};
// Modbus_ReadHoldingRegistersTask(out temperature, 27, 0, 4, out int signT);
Modbus_ReadHoldingRegistersTask
(
out
temperature
,
27
,
0
,
4
,
out
int
signT
);
// Modbus_ReadHoldingRegistersTask(out type, 27, 10, 4, out sign);
Modbus_ReadHoldingRegistersTask
(
out
type
,
27
,
10
,
4
,
out
sign
);
UInt64
time_1
=
GetTimeStamp
();
Modbus_ReadHoldingRegistersTask
(
out
register
,
21
,
0
,
8
,
out
int
signr
);
Modbus_ReadHoldingRegistersTask
(
out
Inputtype
,
21
,
10
,
8
,
out
int
signI
);
UInt64
time_2
=
GetTimeStamp
();
Modbus_ReadHoldingRegistersTask
(
out
registerN
,
53
,
0
,
8
,
out
int
signre
);
Modbus_ReadHoldingRegistersTask
(
out
InputtypeN
,
53
,
10
,
8
,
out
int
signIn
);
UInt64
time_3
=
GetTimeStamp
();
Modbus_ReadCoilsTask
(
out
EIO
,
40
,
16
,
6
,
out
int
signE
);
Modbus_ReadCoilsTask
(
out
SIO
,
19
,
16
,
6
,
out
int
signS
);
Modbus_ReadCoilsTask
(
out
WIO
,
41
,
16
,
6
,
out
int
signW
);
if
(
sign
==
-
1
)
{
}
//else
//{
vs
=
rtd
.
RtdValue
(
temperature
,
type
);
EastTemperature
=
float
.
Parse
(
vs
[
0
]);
s
=
Analog
.
Value
(
register
,
Inputtype
);
//(实时电流值 - 设定电流最小值) /(20Ma - 设定电流最小值) *设定测量最大值
//(实时电流值 - 4ma) /(20ma - 4ma) *设定测量最大值(也就是设定的满量程)
Easternwaterlevel
=
(
float
.
Parse
(
s
[
0
])
-
4
)
/
16
*
6
;
s6
=
Analog
.
Value
(
registerN
,
InputtypeN
);
WestWaterLevel
=
(
float
.
Parse
(
s6
[
0
])
-
4
)
/
16
*
6
;
label3
.
Text
=
EastTemperature
.
ToString
();
label4
.
Text
=
Easternwaterlevel
.
ToString
();
EOnOff
=
state
.
IO
(
EIO
);
////东中转池
SOnOff
=
state
.
IO
(
SIO
);
////西中转池
WOnOff
=
state
.
IO
(
WIO
);
////东南北水
//}
WTDR14P
x
=
new
WTDR14P
(
"27"
,
EastTemperature
,
0
,
0
,
0
,
time_1
);
WTDR18X
p
=
new
WTDR18X
(
"21"
,
Easternwaterlevel
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
time_2
);
WTDR18X
q
=
new
WTDR18X
(
"53"
,
WestWaterLevel
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
time_3
);
//东搅拌
WTDR66C
EastStir
=
new
WTDR66C
(
"40"
,
EOnOff
[
0
],
EOnOff
[
1
],
EOnOff
[
2
],
EOnOff
[
3
],
EOnOff
[
4
],
EOnOff
[
5
],
GetTimeStamp
());
//东南北水
WTDR66C
WestStir
=
new
WTDR66C
(
"41"
,
WOnOff
[
0
],
WOnOff
[
1
],
WOnOff
[
2
],
WOnOff
[
3
],
WOnOff
[
4
],
WOnOff
[
5
],
GetTimeStamp
());
//西搅拌
WTDR66C
SouthWater
=
new
WTDR66C
(
"19"
,
SOnOff
[
0
],
SOnOff
[
1
],
SOnOff
[
2
],
SOnOff
[
3
],
SOnOff
[
4
],
SOnOff
[
5
],
GetTimeStamp
());
mqttData
=
"["
+
JsonConvert
.
SerializeObject
(
x
)
+
","
+
JsonConvert
.
SerializeObject
(
p
)
+
","
+
JsonConvert
.
SerializeObject
(
q
)
+
","
+
JsonConvert
.
SerializeObject
(
EastStir
)
+
","
+
JsonConvert
.
SerializeObject
(
WestStir
)
+
","
+
JsonConvert
.
SerializeObject
(
SouthWater
)
+
"]"
;
}
// Modbus_ReadHoldingRegistersTask(out temperature, 1, 0, 4, out int signT);
// Modbus_ReadHoldingRegistersTask(out type, 1, 10, 4, out sign);
// UInt64 time_1 = GetTimeStamp();
// UInt64 time_1 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out register,
21
, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out register,
3
, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out Inputtype,
21
, 10, 8, out int signI);
// Modbus_ReadHoldingRegistersTask(out Inputtype,
3
, 10, 8, out int signI);
// UInt64 time_2 = GetTimeStamp();
// UInt64 time_2 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out registerN,
53
, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out registerN,
6
, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out InputtypeN,
53
, 10, 8, out int signIn);
// Modbus_ReadHoldingRegistersTask(out InputtypeN,
6
, 10, 8, out int signIn);
// UInt64 time_3 = GetTimeStamp();
// UInt64 time_3 = GetTimeStamp();
// Modbus_ReadCoilsTask(out EIO,
40
, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out EIO,
5
, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out SIO,
19
, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out SIO,
8
, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out WIO, 4
1
, 16, 6, out int signW);
// Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW);
// if (sign == -1)
// if (sign == -1)
// {
// {
...
@@ -907,9 +950,6 @@ namespace ModbusDemo
...
@@ -907,9 +950,6 @@ namespace ModbusDemo
// vs = rtd.RtdValue(temperature, type);
// vs = rtd.RtdValue(temperature, type);
// EastTemperature = float.Parse(vs[0]);
// EastTemperature = float.Parse(vs[0]);
// s = Analog.Value(register, Inputtype);
// s = Analog.Value(register, Inputtype);
// //(实时电流值 - 设定电流最小值) /(20Ma - 设定电流最小值) *设定测量最大值
// //(实时电流值 - 4ma) /(20ma - 4ma) *设定测量最大值(也就是设定的满量程)
// Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6;
// Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6;
// s6 = Analog.Value(registerN, InputtypeN);
// s6 = Analog.Value(registerN, InputtypeN);
// WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6;
// WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6;
...
@@ -920,16 +960,16 @@ namespace ModbusDemo
...
@@ -920,16 +960,16 @@ namespace ModbusDemo
// WOnOff = state.IO(WIO); ////东南北水
// WOnOff = state.IO(WIO); ////东南北水
// //}
// //}
// WTDR14P x = new WTDR14P("
27
", EastTemperature, 0, 0, 0, time_1);
// WTDR14P x = new WTDR14P("
1
", EastTemperature, 0, 0, 0, time_1);
// WTDR18X p = new WTDR18X("
21
", Easternwaterlevel, 0, 0, 0, 0, 0, 0, 0, time_2);
// WTDR18X p = new WTDR18X("
3
", Easternwaterlevel, 0, 0, 0, 0, 0, 0, 0, time_2);
// WTDR18X q = new WTDR18X("
53
", WestWaterLevel, 0, 0, 0, 0, 0, 0, 0, time_3);
// WTDR18X q = new WTDR18X("
6
", WestWaterLevel, 0, 0, 0, 0, 0, 0, 0, time_3);
// //东搅拌
// //东搅拌
// WTDR66C EastStir = new WTDR66C("
40
", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// WTDR66C EastStir = new WTDR66C("
5
", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// //东南北水
// //东南北水
// WTDR66C WestStir = new WTDR66C("4
1
", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp());
// WTDR66C WestStir = new WTDR66C("4", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp());
// //西搅拌
// //西搅拌
// WTDR66C SouthWater = new WTDR66C("
19
", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp());
// WTDR66C SouthWater = new WTDR66C("
8
", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp());
// mqttData = "[" +
// mqttData = "[" +
// JsonConvert.SerializeObject(x) + "," +
// JsonConvert.SerializeObject(x) + "," +
...
@@ -941,94 +981,94 @@ namespace ModbusDemo
...
@@ -941,94 +981,94 @@ namespace ModbusDemo
// + "]";
// + "]";
//}
//}
//
catch (Exception exception)
catch
(
Exception
exception
)
//
{
{
//Connection exception
//Connection exception
//No response from server.
//No response from server.
//The server maybe close the com port, or response timeout.
//The server maybe close the com port, or response timeout.
//
if (exception.Source.Equals("System"))
if
(
exception
.
Source
.
Equals
(
"System"
))
//
{
{
//
timer.Stop();
timer
.
Stop
();
//
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
//
totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
//
return;
return
;
//
}
}
//
//The server return error code.
//The server return error code.
//
//You can get the function code and exception code.
//You can get the function code and exception code.
//
if (exception.Source.Equals("nModbusPC"))
if
(
exception
.
Source
.
Equals
(
"nModbusPC"
))
//
{
{
//
string str = exception.Message;
string
str
=
exception
.
Message
;
//
int FunctionCode;
int
FunctionCode
;
//
string ExceptionCode;
string
ExceptionCode
;
//
str = str.Remove(0, str.IndexOf("\r\n") + 17);
str
=
str
.
Remove
(
0
,
str
.
IndexOf
(
"\r\n"
)
+
17
);
//
FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
FunctionCode
=
Convert
.
ToInt16
(
str
.
Remove
(
str
.
IndexOf
(
"\r\n"
)));
//
Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
Console
.
WriteLine
(
"Function Code: "
+
FunctionCode
.
ToString
(
"X"
));
//
totxt.Log("Function Code: " + FunctionCode.ToString("X"));
totxt
.
Log
(
"Function Code: "
+
FunctionCode
.
ToString
(
"X"
));
//
//MessageBox.Show("Function Code: " + FunctionCode.ToString("X"));
//MessageBox.Show("Function Code: " + FunctionCode.ToString("X"));
//
str = str.Remove(0, str.IndexOf("\r\n") + 17);
str
=
str
.
Remove
(
0
,
str
.
IndexOf
(
"\r\n"
)
+
17
);
//
ExceptionCode = str.Remove(str.IndexOf("-"));
ExceptionCode
=
str
.
Remove
(
str
.
IndexOf
(
"-"
));
//
switch (ExceptionCode.Trim())
switch
(
ExceptionCode
.
Trim
())
//
{
{
//
case "1":
case
"1"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Illegal function!"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Illegal function!"
);
//
break;
break
;
//
case "2":
case
"2"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Illegal data address!"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Illegal data address!"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
//
break;
break
;
//
case "3":
case
"3"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Illegal data value!"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Illegal data value!"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
//
break;
break
;
//
case "4":
case
"4"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Slave device failure!"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> Slave device failure!"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
//
break;
break
;
//
case "5":
case
"5"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> ACKNOWLEDGE!"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> ACKNOWLEDGE!"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
//
break;
break
;
//
case "6":
case
"6"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> SLAVE DEVICE BUSY !"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> SLAVE DEVICE BUSY !"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
//
break;
break
;
//
case "8":
case
"8"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> MEMORY PARITY ERROR !"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> MEMORY PARITY ERROR !"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
//
break;
break
;
//
case "A":
case
"A"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> GATEWAY PATH UNAVAILABLE !"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"----> GATEWAY PATH UNAVAILABLE !"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
//
break;
break
;
//
case "B":
case
"B"
:
//
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
Console
.
WriteLine
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"---->GATEWAY TARGET DEVICE FAILED TO RESPOND!"
);
//
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
totxt
.
Log
(
"Exception Code: "
+
ExceptionCode
.
Trim
()
+
"---->GATEWAY TARGET DEVICE FAILED TO RESPOND!"
);
//
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY TARGET DEVICE FAILED TO RESPOND!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY TARGET DEVICE FAILED TO RESPOND!");
//
break;
break
;
//
}
}
//
return;
return
;
//
}
}
//
}
}
//
finally
finally
//
{
{
//
}
}
return
;
return
;
}
}
#
endregion
#
endregion
#
region
json
字符串转对象
#region json字符串转对象
/// <summary>
/// <summary>
/// json字符串转对象
/// json字符串转对象
/// </summary>
/// </summary>
...
@@ -1093,10 +1133,9 @@ namespace ModbusDemo
...
@@ -1093,10 +1133,9 @@ namespace ModbusDemo
return
""
;
return
""
;
}
}
}
}
#
endregion
#endregion
#region 网络检测
#
region
网络检测
ManualResetEvent
network
=
new
ManualResetEvent
(
false
);
ManualResetEvent
network
=
new
ManualResetEvent
(
false
);
Thread
MonitorThread
;
Thread
MonitorThread
;
...
@@ -1225,9 +1264,9 @@ namespace ModbusDemo
...
@@ -1225,9 +1264,9 @@ namespace ModbusDemo
}
}
#
endregion
#endregion
#
region
更新线程
#region 更新线程
string
ImagefilePath
=
null
;
string
ImagefilePath
=
null
;
private
void
bgWorker_DoWork
(
object
sender
,
DoWorkEventArgs
e
)
private
void
bgWorker_DoWork
(
object
sender
,
DoWorkEventArgs
e
)
{
{
...
@@ -1562,8 +1601,7 @@ namespace ModbusDemo
...
@@ -1562,8 +1601,7 @@ namespace ModbusDemo
}
}
}
}
}
}
#
endregion
#endregion
private
void
txtReceiveMessage_TextChanged
(
object
sender
,
EventArgs
e
)
private
void
txtReceiveMessage_TextChanged
(
object
sender
,
EventArgs
e
)
{
{
...
@@ -1575,7 +1613,6 @@ namespace ModbusDemo
...
@@ -1575,7 +1613,6 @@ namespace ModbusDemo
private
void
button2_Click
(
object
sender
,
EventArgs
e
)
private
void
button2_Click
(
object
sender
,
EventArgs
e
)
{
{
//Modbus_polling();
ModualPoll
.
vAllNodePoll
();
ModualPoll
.
vAllNodePoll
();
mqttData
=
ModualPoll
.
stAll_Poll_Package
();
mqttData
=
ModualPoll
.
stAll_Poll_Package
();
}
}
...
@@ -1589,18 +1626,19 @@ namespace ModbusDemo
...
@@ -1589,18 +1626,19 @@ namespace ModbusDemo
private
MethodInvoker
simpleDelegate1
;
private
MethodInvoker
simpleDelegate1
;
private
MethodInvoker
MQTT_thread
;
private
MethodInvoker
MQTT_thread
;
[
Obsolete
]
private
void
btOpenCOM_Click
(
object
sender
,
EventArgs
e
)
private
void
btOpenCOM_Click
(
object
sender
,
EventArgs
e
)
{
{
//if (Debug_test
== true)
if
(
LOCALDEBUG
==
true
)
//
{
{
// comPort.PortName = "COM2
";
comPort
.
PortName
=
"COM14
"
;
//
comPort.BaudRate = 9600;
comPort
.
BaudRate
=
9600
;
//
comPort.Parity = Parity.None;
comPort
.
Parity
=
Parity
.
None
;
//
comPort.StopBits = StopBits.One;
comPort
.
StopBits
=
StopBits
.
One
;
//
comPort.DataBits = 8;
comPort
.
DataBits
=
8
;
//
}
}
//
else
else
//
{
{
if
(
cmbPort
.
Text
==
""
)
if
(
cmbPort
.
Text
==
""
)
{
{
MessageBox
.
Show
(
"串口打开错误"
);
MessageBox
.
Show
(
"串口打开错误"
);
...
@@ -1629,34 +1667,35 @@ namespace ModbusDemo
...
@@ -1629,34 +1667,35 @@ namespace ModbusDemo
{
{
comPort
.
StopBits
=
StopBits
.
One
;
comPort
.
StopBits
=
StopBits
.
One
;
}
}
//
}
}
try
try
{
{
comPort
.
Open
();
comPort
.
Open
();
isReconnect
=
true
;
isReconnect
=
true
;
SetMqtt
();
SetMqtt
();
simpleDelegate1
=
new
MethodInvoker
(
GetData
);
if
(
ModbusPollThread
==
null
)
simpleDelegate1
.
BeginInvoke
(
null
,
null
);
{
ModbusPollThread
=
new
Thread
(
vModbusPoll
);
ModbusPollThread
.
Start
();
}
else
ModbusPollThread
.
Resume
();
bModbusPollThreadStart
=
true
;
//simpleDelegate1 = new MethodInvoker(GetData);
//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
();
});
//master = ModbusSerialMaster.CreateRtu(comPort);
master
=
ModbusSerialMaster
.
CreateRtu
(
comPort
);
//master.Transport.Retries = 0; //重试次数
master
.
Transport
.
Retries
=
0
;
//重试次数
//master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms)
master
.
Transport
.
ReadTimeout
=
1000
;
//读取串口数据超时时间(ms)
//master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms)
master
.
Transport
.
WriteTimeout
=
1000
;
//写入串口数据超时时间(ms)
//master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms)
master
.
Transport
.
WaitToRetryMilliseconds
=
0
;
//重试间隔(ms)
ModbusComm
.
Modbus
=
ModbusSerialMaster
.
CreateRtu
(
comPort
);
ModbusComm
.
Modbus
.
Transport
.
Retries
=
0
;
ModbusComm
.
Modbus
.
Transport
.
ReadTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WriteTimeout
=
1000
;
ModbusComm
.
Modbus
.
Transport
.
WaitToRetryMilliseconds
=
0
;
modbus_Timer
.
Enabled
=
true
;
modbus_Timer
.
Enabled
=
true
;
btOpenCOM
.
Enabled
=
false
;
btOpenCOM
.
Enabled
=
false
;
btCloseCOM
.
Enabled
=
true
;
btCloseCOM
.
Enabled
=
true
;
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" =>Open "
+
comPort
.
PortName
+
" sucessfully!"
);
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" =>Open "
+
comPort
.
PortName
+
" sucessfully!"
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
...
@@ -1665,11 +1704,14 @@ namespace ModbusDemo
...
@@ -1665,11 +1704,14 @@ namespace ModbusDemo
return
;
return
;
}
}
}
}
[
Obsolete
]
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
();
//timer.Stop();
bModbusPollThreadStart
=
false
;
if
(
ModbusPollThread
.
IsAlive
)
ModbusPollThread
.
Suspend
();
isReconnect
=
false
;
isReconnect
=
false
;
modbus_Timer
.
Enabled
=
false
;
modbus_Timer
.
Enabled
=
false
;
btOpenCOM
.
Enabled
=
true
;
btOpenCOM
.
Enabled
=
true
;
...
@@ -1679,6 +1721,4 @@ namespace ModbusDemo
...
@@ -1679,6 +1721,4 @@ namespace ModbusDemo
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" =>Disconnect "
+
comPort
.
PortName
);
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" =>Disconnect "
+
comPort
.
PortName
);
}
}
}
}
}
}
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