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
beb1f654
Commit
beb1f654
authored
Jun 18, 2019
by
shaxuezheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update control logic
parent
3fd90cd4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
139 additions
and
57 deletions
+139
-57
ModbusComm.cs
ModbusDemo/MessageFormat/ModbusComm.cs
+6
-5
ModbusDemo.csproj
ModbusDemo/ModbusDemo.csproj
+2
-0
AutoControl.cs
ModbusDemo/application/AutoControl.cs
+39
-12
InIHelper.cs
ModbusDemo/application/InIHelper.cs
+1
-1
Form1.cs
ModbusDemo/windows/Form1.cs
+91
-39
No files found.
ModbusDemo/MessageFormat/ModbusComm.cs
View file @
beb1f654
...
...
@@ -28,11 +28,12 @@ namespace ModbusDemo.MessageFormat
MB_EMASTERSENT
,
MB_RTUERNOERR
}
public
static
ModbusMaster
Modbus
;
public
static
ModbusMaster
Modbus
;
public
static
ushort
usCommunFailTimes
=
0
;
static
object
oLock
=
new
object
();
public
static
eModbusErrorCode
Modbus_ReadHoldingRegistersTask
(
out
ushort
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
)
{
object
oLock
=
new
object
();
lock
(
oLock
)
{
eModbusErrorCode
ModbusErrorCode
;
...
...
@@ -117,7 +118,7 @@ namespace ModbusDemo.MessageFormat
}
public
static
eModbusErrorCode
Modbus_ReadCoilsTask
(
out
bool
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
)
{
object
oLock
=
new
object
();
//
object oLock = new object();
lock
(
oLock
)
{
eModbusErrorCode
ModbusErrorCode
;
...
...
@@ -202,7 +203,7 @@ namespace ModbusDemo.MessageFormat
}
public
static
eModbusErrorCode
Modbus_WriteSingleCoil
(
byte
slaveAddress
,
ushort
startAddress
,
bool
value
)
{
object
oLock
=
new
object
();
//
object oLock = new object();
lock
(
oLock
)
{
eModbusErrorCode
ModbusErrorCode
;
...
...
@@ -285,7 +286,7 @@ namespace ModbusDemo.MessageFormat
}
public
static
eModbusErrorCode
Modbus_WriteMultipleCoils
(
byte
slaveAddress
,
ushort
startAddress
,
bool
[]
data
)
{
object
oLock
=
new
object
();
//
object oLock = new object();
lock
(
oLock
)
{
eModbusErrorCode
ModbusErrorCode
;
...
...
ModbusDemo/ModbusDemo.csproj
View file @
beb1f654
...
...
@@ -173,6 +173,8 @@
<PropertyGroup>
<SsdtUnitTestVersion>
3.1
</SsdtUnitTestVersion>
</PropertyGroup>
<Import
Project=
"$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets"
Condition=
"$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' != ''"
/>
<Import
Project=
"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets"
Condition=
"$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' == ''"
/>
<PropertyGroup>
<SsdtUnitTestVersion>
3.1
</SsdtUnitTestVersion>
</PropertyGroup>
...
...
ModbusDemo/application/AutoControl.cs
View file @
beb1f654
...
...
@@ -6,6 +6,8 @@ using System.Text;
using
System.Threading.Tasks
;
using
ModbusDemo.windows
;
using
ModbusDemo.Modular
;
using
ModbusDemo.MessageFormat
;
namespace
ModbusDemo.application
{
public
class
AutoControl
...
...
@@ -23,36 +25,31 @@ namespace ModbusDemo.application
}
public
class
Control
:
Status
{
public
bool
Enablation
;
public
bool
Enablation
;
//任务使能
public
double
maxLevel
;
public
double
minLevel
;
public
double
DifferenceValue
;
public
double
realTimeLevel
;
public
bool
status
;
public
bool
status
;
public
string
Name
;
public
double
StatusTime
;
public
int
modularx
;
}
public
static
void
CriticalValueMonitoring
(
bool
Enabl
,
double
MaxLevel
,
double
MinLevel
,
double
ActualValue
,
string
Affiliation
,
out
Control
control
)
public
static
void
CriticalValueMonitoring
(
bool
Enabl
,
string
Affiliation
,
out
Control
control
)
{
System
.
Diagnostics
.
Stopwatch
stopwatch
=
new
System
.
Diagnostics
.
Stopwatch
();
Control
usControl
=
new
Control
{
Enablation
=
Enabl
,
realTimeLevel
=
ActualValue
,
Name
=
Affiliation
};
if
(
usControl
.
Enablation
==
true
)
{
if
(
MaxLevel
!=
0.0
&&
M
inLevel
!=
0.0
)
if
(
usControl
.
maxLevel
!=
0.0
&&
usControl
.
m
inLevel
!=
0.0
)
{
InIHelper
.
WriteConfig
(
"MaxLevel"
,
Affiliation
,
MaxLevel
.
ToString
());
InIHelper
.
WriteConfig
(
"MinLevel"
,
Affiliation
,
MinLevel
.
ToString
());
usControl
.
maxLevel
=
InIHelper
.
ReadConfig
<
float
>(
"MaxLevel"
,
Affiliation
);
usControl
.
minLevel
=
InIHelper
.
ReadConfig
<
float
>(
"MinLevel"
,
Affiliation
);
InIHelper
.
WriteConfig
(
"MaxLevel"
,
Affiliation
,
usControl
.
maxLevel
.
ToString
());
InIHelper
.
WriteConfig
(
"MinLevel"
,
Affiliation
,
usControl
.
minLevel
.
ToString
());
}
else
{
...
...
@@ -106,5 +103,35 @@ namespace ModbusDemo.application
Value
=
MeanValue
[
0
]
*
0.1
+
MeanValue
[
5
]
*
0.3
+
MeanValue
[
9
]
*
0.6
;
return
Value
;
}
public
static
byte
[]
EmodbusId
={
5
,
51
};
public
static
byte
[]
WmodbusId
=
{
37
,
8
};
public
static
bool
[]
state_ON
=
{
true
,
true
,
true
,
true
,
true
,
true
};
public
static
bool
[]
state_OFF
=
{
false
,
false
,
false
,
false
,
false
,
false
};
public
static
void
Automatic_control
(
bool
Eenabl
,
bool
Wenabl
)
{
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
//for (int j = 0; j < ModualPoll.byX66cNumber; j++)
//{
//}
if
(
Eenabl
==
true
)
{
ModbusComm
.
Modbus_WriteMultipleCoils
(
EmodbusId
[
i
],
0
,
state_ON
);
}
else
{
ModbusComm
.
Modbus_WriteMultipleCoils
(
EmodbusId
[
i
],
0
,
state_OFF
);
}
if
(
Wenabl
==
true
)
{
ModbusComm
.
Modbus_WriteMultipleCoils
(
WmodbusId
[
i
],
0
,
state_ON
);
}
else
{
ModbusComm
.
Modbus_WriteMultipleCoils
(
WmodbusId
[
i
],
0
,
state_OFF
);
}
}
}
}
}
ModbusDemo/application/InIHelper.cs
View file @
beb1f654
...
...
@@ -11,7 +11,7 @@ namespace ModbusDemo.application
{
public
class
InIHelper
{
p
rivate
static
string
FileName
=
Application
.
StartupPath
+
"\\AppConfig.ini"
;
p
ublic
static
string
FileName
=
Application
.
StartupPath
+
"\\AppConfig.ini"
;
/// <summary>
/// 读取配置文件
/// </summary>
...
...
ModbusDemo/windows/Form1.cs
View file @
beb1f654
...
...
@@ -31,6 +31,7 @@ namespace ModbusDemo
AutoResetEvent
exitEvent
;
private
int
waitTime
;
private
Thread
t_UpgradeConn
;
private
Thread
t_AutoControl
;
public
delegate
void
invokeDelegate
();
System
.
Timers
.
Timer
MQTTTimer
=
new
System
.
Timers
.
Timer
();
BackgroundWorker
m_bgw0
=
new
BackgroundWorker
();
...
...
@@ -69,11 +70,13 @@ namespace ModbusDemo
public
static
ModbusMaster
master
;
private
MethodInvoker
MQTT_thread
;
private
string
mqttIp
;
string
mqttClientId
;
int
mqttPort
;
string
mqttUsername
;
string
mqttPwd
;
byte
[]
LevelmodbusID
=
{
6
,
3
};
public
const
string
mqttSn
=
"66666666"
;
public
const
string
mqttSubscribeControlTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Ctrl"
;
public
const
string
mqttBackTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Back"
;
...
...
@@ -366,8 +369,93 @@ namespace ModbusDemo
#
endregion
#
region
modbus
线程
private
static
bool
bSetDataIsRunning
=
false
;
private
Thread
ModbusPollThread
;
private
void
T_AutoControl
()
{
while
(
true
)
{
AutoControl
.
Control
usEcontrol
=
new
AutoControl
.
Control
();
AutoControl
.
Control
usWcontrol
=
new
AutoControl
.
Control
();
if
(
ModualControl
.
cPoolLevelConfig
.
Count
!=
0
)
{
for
(
int
i
=
0
;
i
<
ModualControl
.
cPoolLevelConfig
.
Count
;
i
++)
{
for
(
int
j
=
0
;
j
<
ModualPoll
.
byX18xNumber
;
j
++)
{
if
(
ModualPoll
.
cx18x
.
byModbusID
[
j
]
==
LevelmodbusID
[
0
])
{
usEcontrol
.
realTimeLevel
=
ModualPoll
.
cx18x
.
sX18xDataStruct
[
j
].
dNowConvertData
[
0
];
usEcontrol
.
modularx
=
j
;
}
if
(
ModualPoll
.
cx18x
.
byModbusID
[
j
]
==
LevelmodbusID
[
1
])
{
usWcontrol
.
realTimeLevel
=
ModualPoll
.
cx18x
.
sX18xDataStruct
[
j
].
dNowConvertData
[
0
];
usWcontrol
.
modularx
=
j
;
}
}
if
(
ModualControl
.
cPoolLevelConfig
[
i
].
byModbusID
==
LevelmodbusID
[
0
])
{
usWcontrol
.
maxLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dUpLevel
;
usWcontrol
.
minLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dDownLevel
;
usWcontrol
.
Name
=
"西·中转池"
;
usWcontrol
.
Enablation
=
true
;
AutoControl
.
CriticalValueMonitoring
(
true
,
"西·中转池"
,
out
usWcontrol
);
InIHelper
.
WriteConfig
(
"Channel number"
,
"西液位通道"
,
usWcontrol
.
modularx
.
ToString
());
}
if
(
ModualControl
.
cPoolLevelConfig
[
i
].
byModbusID
==
LevelmodbusID
[
1
])
{
usEcontrol
.
maxLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dUpLevel
;
usEcontrol
.
minLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dDownLevel
;
usEcontrol
.
Name
=
"东·中转池"
;
usEcontrol
.
Enablation
=
true
;
AutoControl
.
CriticalValueMonitoring
(
true
,
"东·中转池"
,
out
usEcontrol
);
InIHelper
.
WriteConfig
(
"Channel number"
,
"东液位通道"
,
usEcontrol
.
modularx
.
ToString
());
}
}
AutoControl
.
Automatic_control
(
usWcontrol
.
status
,
usEcontrol
.
status
);
}
else
if
(
InIHelper
.
FileName
.
Length
!=
0
)
{
usEcontrol
.
modularx
=
InIHelper
.
ReadConfig
<
int
>(
"Channel number"
,
"东液位通道"
);
usWcontrol
.
modularx
=
InIHelper
.
ReadConfig
<
int
>(
"Channel number"
,
"西液位通道"
);
usWcontrol
.
maxLevel
=
InIHelper
.
ReadConfig
<
float
>(
"MaxLevel"
,
"西·中转池"
);
usWcontrol
.
minLevel
=
InIHelper
.
ReadConfig
<
float
>(
"MinLevel"
,
"西·中转池"
);
usEcontrol
.
maxLevel
=
InIHelper
.
ReadConfig
<
float
>(
"MaxLevel"
,
"东·中转池"
);
usEcontrol
.
minLevel
=
InIHelper
.
ReadConfig
<
float
>(
"MaxLevel"
,
"东·中转池"
);
usEcontrol
.
realTimeLevel
=
ModualPoll
.
cx18x
.
sX18xDataStruct
[
usEcontrol
.
modularx
].
dNowConvertData
[
0
];
usWcontrol
.
realTimeLevel
=
ModualPoll
.
cx18x
.
sX18xDataStruct
[
usWcontrol
.
modularx
].
dNowConvertData
[
0
];
AutoControl
.
CriticalValueMonitoring
(
true
,
"东·中转池"
,
out
usEcontrol
);
AutoControl
.
CriticalValueMonitoring
(
true
,
"西·中转池"
,
out
usWcontrol
);
AutoControl
.
Automatic_control
(
usWcontrol
.
status
,
usEcontrol
.
status
);
}
else
{
usWcontrol
.
status
=
false
;
usEcontrol
.
status
=
false
;
AutoControl
.
Automatic_control
(
usWcontrol
.
status
,
usEcontrol
.
status
);
totxt
.
Log
(
"Self - control has not started"
);
}
}
}
private
void
vModbusPoll
()
{
while
(
true
)
...
...
@@ -982,42 +1070,6 @@ namespace ModbusDemo
private
void
button2_Click
(
object
sender
,
EventArgs
e
)
{
AutoControl
.
Control
usEcontrol
=
new
AutoControl
.
Control
();
AutoControl
.
Control
usWcontrol
=
new
AutoControl
.
Control
();
if
(
ModualControl
.
cPoolLevelConfig
.
Count
!=
0
)
{
for
(
int
i
=
0
;
i
<
ModualControl
.
cPoolLevelConfig
.
Count
;
i
++)
{
if
(
ModualControl
.
cPoolLevelConfig
[
i
].
byModbusID
==
6
)
{
usWcontrol
.
maxLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dUpLevel
;
usWcontrol
.
minLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dDownLevel
;
usWcontrol
.
Name
=
"西·中转池"
;
usWcontrol
.
Enablation
=
true
;
AutoControl
.
CriticalValueMonitoring
(
true
,
ModualControl
.
cPoolLevelConfig
[
1
].
dUpLevel
,
ModualControl
.
cPoolLevelConfig
[
0
].
dDownLevel
,
ModualPoll
.
cx18x
.
sX18xDataStruct
[
1
].
dNowConvertData
[
1
],
"西·中转池"
,
out
usWcontrol
);
}
if
(
ModualControl
.
cPoolLevelConfig
[
i
].
byModbusID
==
3
)
{
usEcontrol
.
maxLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dUpLevel
;
usEcontrol
.
minLevel
=
ModualControl
.
cPoolLevelConfig
[
i
].
dDownLevel
;
usEcontrol
.
Name
=
"东·中转池"
;
usEcontrol
.
Enablation
=
true
;
AutoControl
.
CriticalValueMonitoring
(
true
,
ModualControl
.
cPoolLevelConfig
[
0
].
dUpLevel
,
ModualControl
.
cPoolLevelConfig
[
0
].
dDownLevel
,
ModualPoll
.
cx18x
.
sX18xDataStruct
[
0
].
dNowConvertData
[
0
],
"东·中转池"
,
out
usEcontrol
);
}
}
}
}
...
...
@@ -1076,14 +1128,14 @@ namespace ModbusDemo
MQTT_thread
=
new
MethodInvoker
(
Sendout
);
MQTT_thread
.
BeginInvoke
(
null
,
null
);
Task
.
Run
(
async
()
=>
{
await
ConnectMqttServerAsync
();
});
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
;
t_AutoControl
=
new
Thread
(
T_AutoControl
);
t_AutoControl
.
IsBackground
=
true
;
t_AutoControl
.
Start
();
modbus_Timer
.
Enabled
=
true
;
btOpenCOM
.
Enabled
=
false
;
btCloseCOM
.
Enabled
=
true
;
...
...
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