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
7728813d
Commit
7728813d
authored
Jun 10, 2019
by
leon.huang
Browse files
Options
Browse Files
Download
Plain Diff
temper commit
parents
b5aebba5
2b471f9a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
146 deletions
+102
-146
Totxt.cs
ModbusDemo/Common/Totxt.cs
+0
-11
ModbusComm.cs
ModbusDemo/MessageFormat/ModbusComm.cs
+4
-4
ModbusDemo.csproj
ModbusDemo/ModbusDemo.csproj
+5
-5
Form1.Designer.cs
ModbusDemo/windows/Form1.Designer.cs
+63
-89
Form1.cs
ModbusDemo/windows/Form1.cs
+30
-37
No files found.
ModbusDemo/Common/Totxt.cs
View file @
7728813d
...
@@ -36,17 +36,6 @@ namespace ModbusDemo.windows
...
@@ -36,17 +36,6 @@ namespace ModbusDemo.windows
}
}
writer
.
WriteLine
(
DateTime
.
Now
+
": "
+
info
);
writer
.
WriteLine
(
DateTime
.
Now
+
": "
+
info
);
}
}
catch
(
Exception
e
)
{
if
(
writer
!=
null
)
{
writer
.
Close
();
writer
.
Dispose
();
fileStream
.
Close
();
fileStream
.
Dispose
();
}
throw
;
}
finally
finally
{
{
if
(
writer
!=
null
)
if
(
writer
!=
null
)
...
...
ModbusDemo/MessageFormat/ModbusComm.cs
View file @
7728813d
...
@@ -46,7 +46,7 @@ namespace ModbusDemo.MessageFormat
...
@@ -46,7 +46,7 @@ namespace ModbusDemo.MessageFormat
if
(
exception
.
Source
.
Equals
(
"System"
))
if
(
exception
.
Source
.
Equals
(
"System"
))
{
{
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
+
"Modbus_ReadHoldingRegistersTask"
);
OutputValue
=
new
ushort
[
numberOfPoints
];
OutputValue
=
new
ushort
[
numberOfPoints
];
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
usCommunFailTimes
++;
usCommunFailTimes
++;
...
@@ -127,7 +127,7 @@ namespace ModbusDemo.MessageFormat
...
@@ -127,7 +127,7 @@ namespace ModbusDemo.MessageFormat
if
(
exception
.
Source
.
Equals
(
"System"
))
if
(
exception
.
Source
.
Equals
(
"System"
))
{
{
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
+
"Modbus_ReadCoilsTask"
);
OutputValue
=
new
bool
[
numberOfPoints
];
OutputValue
=
new
bool
[
numberOfPoints
];
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
usCommunFailTimes
++;
usCommunFailTimes
++;
...
@@ -208,7 +208,7 @@ namespace ModbusDemo.MessageFormat
...
@@ -208,7 +208,7 @@ namespace ModbusDemo.MessageFormat
if
(
exception
.
Source
.
Equals
(
"System"
))
if
(
exception
.
Source
.
Equals
(
"System"
))
{
{
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
+
"Modbus_WriteSingleCoil"
);
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
}
}
//The server return error code.
//The server return error code.
...
@@ -286,7 +286,7 @@ namespace ModbusDemo.MessageFormat
...
@@ -286,7 +286,7 @@ namespace ModbusDemo.MessageFormat
if
(
exception
.
Source
.
Equals
(
"System"
))
if
(
exception
.
Source
.
Equals
(
"System"
))
{
{
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
+
"Modbus_WriteMultipleCoils"
);
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
}
}
//The server return error code.
//The server return error code.
...
...
ModbusDemo/ModbusDemo.csproj
View file @
7728813d
...
@@ -167,11 +167,11 @@
...
@@ -167,11 +167,11 @@
<ItemGroup
/>
<ItemGroup
/>
<Import
Project=
"$(MSBuildToolsPath)\Microsoft.CSharp.targets"
/>
<Import
Project=
"$(MSBuildToolsPath)\Microsoft.CSharp.targets"
/>
<PropertyGroup>
<PropertyGroup>
<
SsdtUnitTestVersion>
3.1
</SsdtUnitTestVersion
>
<
!-- <SsdtUnitTestVersion>3.1</SsdtUnitTestVersion> --
>
<
/PropertyGroup
>
<
!-- </PropertyGroup> --
>
<
Import
Project=
"$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets"
Condition=
"$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' != ''"
/
>
<
!-- <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)' == ''"
/
>
<
!-- <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets" Condition="$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' == ''" /> --
>
<
PropertyGroup
>
<
!-- <PropertyGroup> --
>
<SsdtUnitTestVersion>
3.1
</SsdtUnitTestVersion>
<SsdtUnitTestVersion>
3.1
</SsdtUnitTestVersion>
</PropertyGroup>
</PropertyGroup>
<Import
Project=
"$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets"
Condition=
"$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' != ''"
/>
<Import
Project=
"$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.targets"
Condition=
"$(VisualStudioVersion) != '15.0' And '$(SQLDBExtensionsRefPath)' != ''"
/>
...
...
ModbusDemo/windows/Form1.Designer.cs
View file @
7728813d
This diff is collapsed.
Click to expand it.
ModbusDemo/windows/Form1.cs
View file @
7728813d
...
@@ -22,7 +22,6 @@ using ModbusDemo.application;
...
@@ -22,7 +22,6 @@ using ModbusDemo.application;
using
System.Collections
;
using
System.Collections
;
using
ModbusDemo.Modular
;
using
ModbusDemo.Modular
;
using
ModbusDemo.MessageFormat
;
using
ModbusDemo.MessageFormat
;
namespace
ModbusDemo
namespace
ModbusDemo
{
{
public
partial
class
Form1
:
Form
public
partial
class
Form1
:
Form
...
@@ -114,11 +113,11 @@ namespace ModbusDemo
...
@@ -114,11 +113,11 @@ namespace ModbusDemo
mqttTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Data"
;
mqttTopic
=
"Witium/WTDS78X/"
+
mqttSn
+
"/Data"
;
if
(
Debug_test
==
true
)
if
(
Debug_test
==
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
{
{
...
@@ -490,7 +489,7 @@ namespace ModbusDemo
...
@@ -490,7 +489,7 @@ namespace ModbusDemo
{
{
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
();
}
}
...
@@ -526,7 +525,7 @@ namespace ModbusDemo
...
@@ -526,7 +525,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
;
...
@@ -541,7 +540,7 @@ namespace ModbusDemo
...
@@ -541,7 +540,7 @@ namespace ModbusDemo
private
void
GetData
()
private
void
GetData
()
{
{
timer
.
Interval
=
9
000
;
timer
.
Interval
=
120
000
;
timer
.
Enabled
=
true
;
timer
.
Enabled
=
true
;
timer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
timer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
timer
.
Start
();
timer
.
Start
();
...
@@ -554,7 +553,7 @@ namespace ModbusDemo
...
@@ -554,7 +553,7 @@ namespace ModbusDemo
private
void
Sendout
()
private
void
Sendout
()
{
{
Control
.
CheckForIllegalCrossThreadCalls
=
false
;
Control
.
CheckForIllegalCrossThreadCalls
=
false
;
MQTTTimer
.
Interval
=
70
00
;
MQTTTimer
.
Interval
=
1205
00
;
MQTTTimer
.
Enabled
=
true
;
MQTTTimer
.
Enabled
=
true
;
MQTTTimer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
MQTTTimer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
MQTTTimer
.
Start
();
MQTTTimer
.
Start
();
...
@@ -562,8 +561,8 @@ namespace ModbusDemo
...
@@ -562,8 +561,8 @@ namespace ModbusDemo
{
{
try
try
{
{
label5
.
Text
=
ModbusComm
.
usCommunFailTimes
.
ToString
();
await
Publish
();
await
Publish
();
totxt
.
Log
(
mqttData
+
Environment
.
NewLine
+
"\n"
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
...
@@ -585,10 +584,10 @@ namespace ModbusDemo
...
@@ -585,10 +584,10 @@ namespace ModbusDemo
{
{
await
Task
.
Run
(()
=>
await
Task
.
Run
(()
=>
{
{
//Modbus_polling(
);
totxt
.
Log
(
"开始执行"
);
ModualPoll
.
vAllNodePoll
();
ModualPoll
.
vAllNodePoll
();
mqttData
=
ModualPoll
.
stAll_Poll_Package
();
mqttData
=
ModualPoll
.
stAll_Poll_Package
();
ModualPoll
.
vCheckAlarm_And_Send
(
mqttClient
);
totxt
.
Log
(
"执行结束"
);
});
});
}
}
...
@@ -614,12 +613,12 @@ namespace ModbusDemo
...
@@ -614,12 +613,12 @@ namespace ModbusDemo
else
else
{
{
//txtbox.Text = message;
//txtbox.Text = message;
totxt
.
Log
(
message
);
//
totxt.Log(message);
}
}
}
}
public
void
Modbus_ReadHoldingRegistersTask
(
out
ushort
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
int
Caution
)
public
void
Modbus_ReadHoldingRegistersTask
(
out
ushort
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
int
Caution
)
{
{
label5
.
Text
=
usCommunFailTimes
.
ToString
();
label5
.
Text
=
usCommunFailTimes
.
ToString
();
try
try
{
{
OutputValue
=
master
.
ReadHoldingRegisters
(
slaveAddress
,
startAddress
,
numberOfPoints
);
OutputValue
=
master
.
ReadHoldingRegisters
(
slaveAddress
,
startAddress
,
numberOfPoints
);
...
@@ -716,7 +715,7 @@ namespace ModbusDemo
...
@@ -716,7 +715,7 @@ namespace ModbusDemo
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
);
OutputValue
=
new
bool
[
numberOfPoints
];
OutputValue
=
new
bool
[
numberOfPoints
];
Caution
=
-
1
;
Caution
=
-
1
;
usCommunFailTimes
++;
usCommunFailTimes
++;
}
}
...
@@ -861,6 +860,7 @@ namespace ModbusDemo
...
@@ -861,6 +860,7 @@ namespace ModbusDemo
Caution
=
-
1
;
Caution
=
-
1
;
}
}
}
}
private
async
void
Modbus_polling
()
private
async
void
Modbus_polling
()
{
{
//float EastTemperature = 0;
//float EastTemperature = 0;
...
@@ -1026,6 +1026,7 @@ namespace ModbusDemo
...
@@ -1026,6 +1026,7 @@ namespace ModbusDemo
return
;
return
;
}
}
#
endregion
#
endregion
#
region
json
字符串转对象
#
region
json
字符串转对象
...
@@ -1574,9 +1575,8 @@ namespace ModbusDemo
...
@@ -1574,9 +1575,8 @@ 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();
}
}
private
async
void
SubscribeBut_Click
(
object
sender
,
EventArgs
e
)
private
async
void
SubscribeBut_Click
(
object
sender
,
EventArgs
e
)
...
@@ -1590,16 +1590,16 @@ namespace ModbusDemo
...
@@ -1590,16 +1590,16 @@ namespace ModbusDemo
private
void
btOpenCOM_Click
(
object
sender
,
EventArgs
e
)
private
void
btOpenCOM_Click
(
object
sender
,
EventArgs
e
)
{
{
//
if (Debug_test == true)
if
(
Debug_test
==
true
)
//
{
{
//
comPort.PortName = "COM2";
comPort
.
PortName
=
"COM2"
;
//
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
(
"串口打开错误"
);
...
@@ -1628,7 +1628,7 @@ namespace ModbusDemo
...
@@ -1628,7 +1628,7 @@ namespace ModbusDemo
{
{
comPort
.
StopBits
=
StopBits
.
One
;
comPort
.
StopBits
=
StopBits
.
One
;
}
}
//
}
}
try
try
{
{
comPort
.
Open
();
comPort
.
Open
();
...
@@ -1638,20 +1638,13 @@ namespace ModbusDemo
...
@@ -1638,20 +1638,13 @@ namespace ModbusDemo
simpleDelegate1
.
BeginInvoke
(
null
,
null
);
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
();
});
master
=
ModbusSerialMaster
.
CreateRtu
(
comPort
);
master
.
Transport
.
Retries
=
0
;
//重试次数
master
.
Transport
.
ReadTimeout
=
1000
;
//读取串口数据超时时间(ms)
master
.
Transport
.
WriteTimeout
=
1000
;
//写入串口数据超时时间(ms)
master
.
Transport
.
WaitToRetryMilliseconds
=
0
;
//重试间隔(ms)
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
;
modbus_Timer
.
Enabled
=
true
;
modbus_Timer
.
Enabled
=
true
;
btOpenCOM
.
Enabled
=
false
;
btOpenCOM
.
Enabled
=
false
;
btCloseCOM
.
Enabled
=
true
;
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