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
1dfb305f
Commit
1dfb305f
authored
May 31, 2019
by
leon.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add modbus poll all modual
parent
0b293b14
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
425 additions
and
157 deletions
+425
-157
Modbuslib.cs
ModbusDemo/Common/Modbuslib.cs
+0
-2
ModbusComm.cs
ModbusDemo/MessageFormat/ModbusComm.cs
+95
-80
ModbusDemo.csproj
ModbusDemo/ModbusDemo.csproj
+2
-0
ModualPoll.cs
ModbusDemo/Modular/ModualPoll.cs
+90
-0
modual.cs
ModbusDemo/Modular/modual.cs
+87
-0
Pond.cs
ModbusDemo/application/Pond.cs
+16
-16
PoolModbusSet.cs
ModbusDemo/application/PoolModbusSet.cs
+18
-28
Form1.Designer.cs
ModbusDemo/windows/Form1.Designer.cs
+26
-2
Form1.cs
ModbusDemo/windows/Form1.cs
+91
-29
No files found.
ModbusDemo/Common/Modbuslib.cs
View file @
1dfb305f
...
...
@@ -233,7 +233,6 @@ namespace ModbusDemo.windows
public
String
GetVersion
(
Byte
slaveid
)
{
int
ret
;
String
version
=
"0.0"
;
String
ip
=
"127.0.0.1"
;
UInt16
[]
buf
=
new
ushort
[
2
];
...
...
@@ -247,6 +246,5 @@ namespace ModbusDemo.windows
}
return
version
;
}
}
}
ModbusDemo/MessageFormat/ModbusComm.cs
View file @
1dfb305f
...
...
@@ -13,14 +13,29 @@ namespace ModbusDemo.MessageFormat
{
public
static
class
ModbusComm
{
public
enum
eModbusErrorCode
:
byte
{
MB_ENOERR
,
/*!< no error. */
MB_ENOREG
,
/*!< illegal register address. */
MB_EINVAL
,
/*!< illegal argument. */
MB_EPORTERR
,
/*!< porting layer error. */
MB_ENORES
,
/*!< insufficient resources. */
MB_EIO
,
/*!< I/O error. */
MB_EILLSTATE
,
/*!< protocol stack in illegal state. */
MB_ETIMEDOUT
,
/*!< timeout error occurred. */
MB_EMASTERNOERR
,
/*!< master mode no error. */
MB_EMASTERERR
,
/*!< master mode error. */
MB_EMASTERSENT
,
MB_RTUERNOERR
}
public
static
ModbusMaster
Modbus
;
public
static
void
Modbus_ReadHoldingRegistersTask
(
out
ushort
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
bool
bCommStatus
)
public
static
ushort
usCommunFailTimes
=
0
;
public
static
void
Modbus_ReadHoldingRegistersTask
(
out
ushort
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
eModbusErrorCode
ModbusErrorCode
)
{
try
{
OutputValue
=
Modbus
.
ReadHoldingRegisters
(
slaveAddress
,
startAddress
,
numberOfPoints
);
bCommStatus
=
true
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_EMASTERNOERR
;
}
catch
(
Exception
exception
)
{
...
...
@@ -32,7 +47,8 @@ namespace ModbusDemo.MessageFormat
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
OutputValue
=
new
ushort
[
numberOfPoints
];
bCommStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
usCommunFailTimes
++;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -90,15 +106,15 @@ namespace ModbusDemo.MessageFormat
}
}
OutputValue
=
new
ushort
[
numberOfPoints
];
bCommStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_EMASTERERR
;
}
}
public
static
void
Modbus_ReadCoilsTask
(
out
bool
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
bool
bComStatus
)
public
static
void
Modbus_ReadCoilsTask
(
out
bool
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
eModbusErrorCode
ModbusErrorCode
)
{
try
{
OutputValue
=
Modbus
.
ReadCoils
(
slaveAddress
,
startAddress
,
numberOfPoints
);
bComStatus
=
true
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_EMASTERNOERR
;
}
catch
(
Exception
exception
)
{
...
...
@@ -110,7 +126,8 @@ namespace ModbusDemo.MessageFormat
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
OutputValue
=
new
bool
[
numberOfPoints
];
bComStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
usCommunFailTimes
++;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -168,15 +185,15 @@ namespace ModbusDemo.MessageFormat
}
}
OutputValue
=
new
bool
[
numberOfPoints
];
bComStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_EMASTERERR
;
}
}
public
static
void
Modbus_WriteSingleCoil
(
byte
slaveAddress
,
ushort
startAddress
,
bool
[]
value
,
out
bool
bComStatus
)
public
static
void
Modbus_WriteSingleCoil
(
byte
slaveAddress
,
ushort
startAddress
,
bool
[]
value
,
out
eModbusErrorCode
ModbusErrorCode
)
{
try
{
Modbus
.
WriteMultipleCoils
(
slaveAddress
,
startAddress
,
value
);
bComStatus
=
true
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ENOERR
;
}
catch
(
Exception
exception
)
{
...
...
@@ -187,7 +204,7 @@ namespace ModbusDemo.MessageFormat
{
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
bComStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -244,16 +261,15 @@ namespace ModbusDemo.MessageFormat
break
;
}
}
bComStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_EMASTERERR
;
}
}
public
static
void
Modbus_WriteMultipleCoils
(
byte
slaveAddress
,
ushort
startAddress
,
bool
[]
data
,
out
bool
bComStatus
)
{
public
static
void
Modbus_WriteMultipleCoils
(
byte
slaveAddress
,
ushort
startAddress
,
bool
[]
data
,
out
eModbusErrorCode
ModbusErrorCode
)
{
try
{
Modbus
.
WriteMultipleCoils
(
slaveAddress
,
startAddress
,
data
);
bComStatus
=
true
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ENOERR
;
}
catch
(
Exception
exception
)
{
...
...
@@ -264,7 +280,7 @@ namespace ModbusDemo.MessageFormat
{
Console
.
WriteLine
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
Form1
.
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
bComStatus
=
false
;
ModbusErrorCode
=
eModbusErrorCode
.
MB_ETIMEDOUT
;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -321,8 +337,7 @@ namespace ModbusDemo.MessageFormat
break
;
}
}
bComStatus
=
false
;
}
ModbusErrorCode
=
eModbusErrorCode
.
MB_EMASTERERR
;
}
}
//private void Init_Read_data()
...
...
ModbusDemo/ModbusDemo.csproj
View file @
1dfb305f
...
...
@@ -98,6 +98,8 @@
<Compile
Include=
"Modular\478C.cs"
/>
<Compile
Include=
"Modular\418X.cs"
/>
<Compile
Include=
"Common\Modbuslib.cs"
/>
<Compile
Include=
"Modular\ModualPoll.cs"
/>
<Compile
Include=
"Modular\modual.cs"
/>
<Compile
Include=
"windows\Form1.cs"
>
<SubType>
Form
</SubType>
</Compile>
...
...
ModbusDemo/Modular/ModualPoll.cs
0 → 100644
View file @
1dfb305f
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
ModbusDemo.MessageFormat
;
namespace
ModbusDemo.Modular
{
class
ModualPoll
{
const
byte
byX14pNumber
=
3
;
const
byte
byX14pReadDataStartAddress
=
0
;
const
byte
byX14pReadDataNumber
=
4
;
const
byte
byX14pReadTypeStartAddress
=
10
;
const
byte
byX14pReadTypeNumber
=
4
;
static
byte
[]
byX14pIdlist
=
{
17
,
28
,
29
};
modual
.
pcx14P
cx14P
=
new
modual
.
pcx14P
(
byX14pIdlist
);
_414P
c14formulasormual
=
new
_414P
();
const
byte
byX18xNumber
=
6
;
const
byte
byX18xReadDataStartAddress
=
0
;
const
byte
byX18xReadDataNumber
=
8
;
const
byte
byX18xReadTypeStartAddress
=
10
;
const
byte
byX18xReadTypeNumber
=
8
;
static
byte
[]
byX18xIdlist
=
{
12
,
13
,
21
,
25
,
45
,
53
};
static
modual
.
eX18xSensorChannelType
[,]
sX18xChannelType
=
new
modual
.
eX18xSensorChannelType
[
byX18xNumber
,
byX18xReadDataNumber
]{{
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
},
{
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
},
{
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
},
{
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
},
{
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
},
{
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
,
modual
.
eX18xSensorChannelType
.
Normal
}};
modual
.
pcx18X
cx18x
=
new
modual
.
pcx18X
(
byX18xIdlist
,
sX18xChannelType
);
_418X
c18xformulasormual
=
new
_418X
();
const
byte
byX78cNumber
=
6
;
const
byte
byX78cInputRegStartAddress
=
0
;
const
byte
byX78cInputRegNumber
=
8
;
const
byte
byX78cOutputRegStartAddress
=
16
;
const
byte
byX78cOutputRegNumber
=
8
;
static
byte
[]
byX78cIdlist
=
{
11
,
20
,
33
,
34
,
55
,
56
};
modual
.
pcx78C
cx78c
=
new
modual
.
pcx78C
(
byX78cIdlist
);
const
byte
byX66cNumber
=
32
;
const
byte
byX66cRelayRegStartAddress
=
16
;
const
byte
byX66cRelayRegNumber
=
6
;
static
byte
[]
byX66cIdlist
=
{
9
,
10
,
14
,
15
,
16
,
19
,
22
,
23
,
24
,
26
,
30
,
31
,
32
,
35
,
36
,
37
,
38
,
39
,
40
,
41
,
42
,
43
,
44
,
46
,
47
,
48
,
49
,
50
,
51
,
52
,
54
,
57
};
modual
.
pcx66C
cx66c
=
new
modual
.
pcx66C
(
byX66cIdlist
);
void
vAllNodePoll
()
{
for
(
byte
i
=
0
;
i
<
byX14pNumber
;
i
++)
{
string
[][]
sTempString
=
new
string
[
byX14pNumber
][];
ModbusComm
.
Modbus_ReadHoldingRegistersTask
(
out
cx14P
.
sX14pDataStruct
[
i
].
usX14pRegData
,
cx14P
.
byModbusID
[
i
],
byX14pReadDataStartAddress
,
byX14pReadDataNumber
,
out
cx14P
.
ModbusErrorCode
[
i
]);
ModbusComm
.
Modbus_ReadHoldingRegistersTask
(
out
cx14P
.
sX14pDataStruct
[
i
].
usX14pRegType
,
cx14P
.
byModbusID
[
i
],
byX14pReadTypeStartAddress
,
byX14pReadTypeNumber
,
out
cx14P
.
ModbusErrorCode
[
i
]);
sTempString
[
i
]
=
c14formulasormual
.
RtdValue
(
cx14P
.
sX14pDataStruct
[
i
].
usX14pRegData
,
cx14P
.
sX14pDataStruct
[
i
].
usX14pRegType
);
for
(
byte
j
=
0
;
j
<
byX14pReadTypeNumber
;
j
++)
{
cx14P
.
sX14pDataStruct
[
i
].
dX14pConvertData
[
j
]
=
double
.
Parse
(
sTempString
[
i
][
j
]);
}
}
for
(
byte
i
=
0
;
i
<
byX18xNumber
;
i
++)
{
string
[][]
sTempString
=
new
string
[
byX18xNumber
][];
ModbusComm
.
Modbus_ReadHoldingRegistersTask
(
out
cx18x
.
sX18xDataStruct
[
i
].
usRegData
,
cx18x
.
byModbusID
[
i
],
byX18xReadDataStartAddress
,
byX18xReadDataNumber
,
out
cx18x
.
ModbusErrorCode
[
i
]);
ModbusComm
.
Modbus_ReadHoldingRegistersTask
(
out
cx18x
.
sX18xDataStruct
[
i
].
usTypeData
,
cx18x
.
byModbusID
[
i
],
byX18xReadTypeStartAddress
,
byX18xReadTypeNumber
,
out
cx18x
.
ModbusErrorCode
[
i
]);
sTempString
[
i
]
=
c18xformulasormual
.
Value
(
cx14P
.
sX14pDataStruct
[
i
].
usX14pRegData
,
cx14P
.
sX14pDataStruct
[
i
].
usX14pRegType
);
for
(
byte
j
=
0
;
j
<
byX18xReadTypeNumber
;
j
++)
{
if
(
cx18x
.
sX18xDataStruct
[
i
].
eChannelType
[
j
]
==
modual
.
eX18xSensorChannelType
.
Normal
)
cx18x
.
sX18xDataStruct
[
i
].
dConvertData
[
j
]
=
(
double
.
Parse
(
sTempString
[
i
][
j
])-
4
)/
16
*
6
;
}
}
for
(
byte
i
=
0
;
i
<
byX78cNumber
;
i
++)
{
ModbusComm
.
Modbus_ReadCoilsTask
(
out
cx78c
.
sx78CRegDataStructStatus
[
i
].
bInputRegStatus
,
cx78c
.
byModbusID
[
i
],
byX78cInputRegStartAddress
,
byX78cInputRegNumber
,
out
cx78c
.
ModbusErrorCode
[
i
]);
ModbusComm
.
Modbus_ReadCoilsTask
(
out
cx78c
.
sx78CRegDataStructStatus
[
i
].
bOutPutRegStatus
,
cx78c
.
byModbusID
[
i
],
byX78cOutputRegStartAddress
,
byX78cOutputRegNumber
,
out
cx78c
.
ModbusErrorCode
[
i
]);
}
for
(
byte
i
=
0
;
i
<
byX66cNumber
;
i
++)
{
ModbusComm
.
Modbus_ReadCoilsTask
(
out
cx66c
.
sx78CDatastructStatus
[
i
].
bOutPutRegStatus
,
cx66c
.
byModbusID
[
i
],
byX66cRelayRegStartAddress
,
byX66cRelayRegNumber
,
out
cx66c
.
ModbusErrorCode
[
i
]);
}
}
}
}
ModbusDemo/Modular/modual.cs
0 → 100644
View file @
1dfb305f
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
ModbusDemo.MessageFormat
;
namespace
ModbusDemo.Modular
{
class
modual
{
public
struct
psx14p
{
public
ushort
[]
usX14pRegData
;
public
ushort
[]
usX14pRegType
;
public
double
[]
dX14pConvertData
;
}
public
class
pcx14P
{
public
byte
[]
byModbusID
;
public
psx14p
[]
sX14pDataStruct
;
public
ModbusComm
.
eModbusErrorCode
[]
ModbusErrorCode
;
public
pcx14P
(
byte
[]
InitIDlist
)
{
this
.
byModbusID
=
InitIDlist
;
}
}
public
enum
eX18xSensorChannelType
:
byte
{
Normal
=
0
,
Level
,
}
public
struct
psX18x
{
public
ushort
[]
usRegData
;
public
ushort
[]
usTypeData
;
public
eX18xSensorChannelType
[]
eChannelType
;
public
double
[]
dConvertData
;
}
public
class
pcx18X
{
public
byte
[]
byModbusID
;
public
psX18x
[]
sX18xDataStruct
;
public
ModbusComm
.
eModbusErrorCode
[]
ModbusErrorCode
;
public
pcx18X
(
byte
[]
InitIDlist
,
eX18xSensorChannelType
[,]
eChannnelType
)
{
this
.
byModbusID
=
InitIDlist
;
for
(
ushort
i
=
0
;
i
<
eChannnelType
.
GetLength
(
0
);
i
++)
{
for
(
ushort
j
=
0
;
j
<
eChannnelType
.
GetLength
(
1
);
j
++)
{
this
.
sX18xDataStruct
[
i
].
eChannelType
[
j
]
=
eChannnelType
[
i
,
j
];
}
}
}
}
public
struct
psx78C
{
public
bool
[]
bInputRegStatus
;
public
bool
[]
bOutPutRegStatus
;
}
public
class
pcx78C
{
public
byte
[]
byModbusID
;
public
psx78C
[]
sx78CRegDataStructStatus
;
public
ModbusComm
.
eModbusErrorCode
[]
ModbusErrorCode
;
public
pcx78C
(
byte
[]
InitIDlist
)
{
this
.
byModbusID
=
InitIDlist
;
}
}
public
struct
psx66C
{
public
bool
[]
bRegRelayStatus
;
}
public
class
pcx66C
{
public
byte
[]
byModbusID
;
public
psx78C
[]
sx78CDatastructStatus
;
public
ModbusComm
.
eModbusErrorCode
[]
ModbusErrorCode
;
public
pcx66C
(
byte
[]
InitIDlist
)
{
this
.
byModbusID
=
InitIDlist
;
}
}
}
}
ModbusDemo/application/Pond.cs
View file @
1dfb305f
...
...
@@ -77,26 +77,26 @@ namespace ModbusDemo.application
// }
//}
public
class
pcPump
{
public
double
dPumpCurrent
;
public
double
dPumpVoltage
;
public
bool
bPumpStatus
;
public
bool
bPumpHasOpen
;
public
void
vOpen_Pump
()
{
//
public class pcPump
//
{
//
public double dPumpCurrent;
//
public double dPumpVoltage;
//
public bool bPumpStatus;
//
public bool bPumpHasOpen;
//
public void vOpen_Pump()
//
{
}
public
void
vClose_Pump
()
{
//
}
//
public void vClose_Pump()
//
{
}
public
void
vGet_Pump_Status
()
{
//
}
//
public void vGet_Pump_Status()
//
{
}
//
}
}
//
}
public
class
pcTemperatureData
...
...
ModbusDemo/application/PoolModbusSet.cs
View file @
1dfb305f
...
...
@@ -18,7 +18,6 @@ namespace ModbusDemo.application
this
.
usTypeRegAddress
=
usSetRegAddress
;
}
}
public
struct
psLevelData
{
public
byte
byModbusID
;
...
...
@@ -31,7 +30,6 @@ namespace ModbusDemo.application
this
.
usTypeAddressList
=
usDataTypeRegAddress
;
}
}
public
struct
psBumpOperation
{
public
byte
byModbusID
;
...
...
@@ -44,7 +42,6 @@ namespace ModbusDemo.application
this
.
bOpenStatus
=
bInitOPen
;
}
}
public
struct
psPumbData
{
public
byte
byModbusID
;
...
...
@@ -55,7 +52,6 @@ namespace ModbusDemo.application
this
.
usRegAddList
=
usInitRegAddressInit
;
}
}
public
struct
psAgitatorOperation
{
public
byte
byModbusID
;
...
...
@@ -68,7 +64,6 @@ namespace ModbusDemo.application
this
.
bOpenStatus
=
bInitOPen
;
}
}
public
struct
psAgitatorData
{
public
byte
byModbusID
;
...
...
@@ -164,7 +159,7 @@ namespace ModbusDemo.application
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
"Bump Open Operation Error"
);
Form1
.
totxt
.
Log
(
exception
+
"Bump Open Operation Error"
);
throw
;
}
}
...
...
@@ -182,7 +177,7 @@ namespace ModbusDemo.application
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
"Bump Close Operation Error"
);
Form1
.
totxt
.
Log
(
exception
+
"Bump Close Operation Error"
);
throw
;
}
}
...
...
@@ -195,12 +190,11 @@ namespace ModbusDemo.application
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
"Bump Read Status Error"
);
Form1
.
totxt
.
Log
(
exception
+
"Bump Read Status Error"
);
throw
;
}
}
}
public
class
pcAgitator
{
private
double
[]
dAgitatorCurrent
=
new
double
[
pcPoolInit
.
PoolAgitatorCurrentNumber
]
{
pcPoolInit
.
cAgitatorCurrent
[
0
].
vRead_Agitator_Data
()
};
...
...
@@ -208,8 +202,7 @@ namespace ModbusDemo.application
public
bool
[]
bAgitatorStatus
=
new
bool
[
pcPoolInit
.
PoolBumpOperatNumber
];
public
bool
[]
bAgitatorHasOpen
=
new
bool
[
pcPoolInit
.
PoolBumpOperatNumber
];
public
void
vOpen_Bump
(
ushort
usAgitatorNumber
)
public
void
vOpen_Agitator
(
ushort
usAgitatorNumber
)
{
try
{
...
...
@@ -220,11 +213,11 @@ namespace ModbusDemo.application
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
"Agitator Open Operation Error"
);
Form1
.
totxt
.
Log
(
exception
+
"Agitator Open Operation Error"
);
throw
;
}
}
public
void
vClose_
Bump
(
ushort
usAgitatorNumber
)
public
void
vClose_
Agitator
(
ushort
usAgitatorNumber
)
{
try
{
...
...
@@ -238,12 +231,12 @@ namespace ModbusDemo.application
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
"Agitator Close Operation Error"
);
Form1
.
totxt
.
Log
(
exception
+
"Agitator Close Operation Error"
);
throw
;
}
}
public
bool
[]
vGet_
Pumb
_Status
(
ushort
usAgitatorNumber
)
public
bool
[]
vGet_
Agitator
_Status
(
ushort
usAgitatorNumber
)
{
try
{
...
...
@@ -251,13 +244,11 @@ namespace ModbusDemo.application
}
catch
(
Exception
exception
)
{
Form1
.
totxt
.
Log
(
"Agitator Read Status Error"
);
Form1
.
totxt
.
Log
(
exception
+
"Agitator Read Status Error"
);
throw
;
}
}
}
public
class
pcPool
{
public
string
ePondName
;
...
...
@@ -268,14 +259,13 @@ namespace ModbusDemo.application
public
double
dMaxTemper
;
public
double
dMinTemper
;
public
pc
Pump
cP
ump
;
public
pc
Bump
cB
ump
;
public
pcAgitator
cAgitator
;
public
double
dPondCurrentLevel
{
get
=>
pcPoolInit
.
Get_Current_Level
();
}
public
double
dPondCurentTemper
{
get
=>
pcPoolInit
.
Get_Current_temper
();
}
public
pcPool
(
string
eInitPondName
)
{
this
.
ePondName
=
eInitPondName
;
}
}
}
ModbusDemo/windows/Form1.Designer.cs
View file @
1dfb305f
...
...
@@ -59,6 +59,8 @@
this
.
label4
=
new
System
.
Windows
.
Forms
.
Label
();
this
.
button2
=
new
System
.
Windows
.
Forms
.
Button
();
this
.
SubscribeBut
=
new
System
.
Windows
.
Forms
.
Button
();
this
.
label5
=
new
System
.
Windows
.
Forms
.
Label
();
this
.
label6
=
new
System
.
Windows
.
Forms
.
Label
();
this
.
updateops
.
SuspendLayout
();
this
.
SuspendLayout
();
//
...
...
@@ -327,7 +329,7 @@
//
this
.
label2
.
AutoSize
=
true
;
this
.
label2
.
Font
=
new
System
.
Drawing
.
Font
(
"宋体"
,
12F
,
System
.
Drawing
.
FontStyle
.
Regular
,
System
.
Drawing
.
GraphicsUnit
.
Point
,
((
byte
)(
134
)));
this
.
label2
.
Location
=
new
System
.
Drawing
.
Point
(
44
,
4
92
);
this
.
label2
.
Location
=
new
System
.
Drawing
.
Point
(
44
,
4
87
);
this
.
label2
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
4
,
0
,
4
,
0
);
this
.
label2
.
Name
=
"label2"
;
this
.
label2
.
Size
=
new
System
.
Drawing
.
Size
(
69
,
20
);
...
...
@@ -347,7 +349,7 @@
// label4
//
this
.
label4
.
AutoSize
=
true
;
this
.
label4
.
Location
=
new
System
.
Drawing
.
Point
(
108
,
49
8
);
this
.
label4
.
Location
=
new
System
.
Drawing
.
Point
(
108
,
49
2
);
this
.
label4
.
Margin
=
new
System
.
Windows
.
Forms
.
Padding
(
4
,
0
,
4
,
0
);
this
.
label4
.
Name
=
"label4"
;
this
.
label4
.
Size
=
new
System
.
Drawing
.
Size
(
55
,
15
);
...
...
@@ -376,11 +378,31 @@
this
.
SubscribeBut
.
UseVisualStyleBackColor
=
true
;
this
.
SubscribeBut
.
Click
+=
new
System
.
EventHandler
(
this
.
SubscribeBut_Click
);
//
// label5
//
this
.
label5
.
AutoSize
=
true
;
this
.
label5
.
Location
=
new
System
.
Drawing
.
Point
(
111
,
528
);
this
.
label5
.
Name
=
"label5"
;
this
.
label5
.
Size
=
new
System
.
Drawing
.
Size
(
55
,
15
);
this
.
label5
.
TabIndex
=
99
;
this
.
label5
.
Text
=
"label5"
;
//
// label6
//
this
.
label6
.
AutoSize
=
true
;
this
.
label6
.
Location
=
new
System
.
Drawing
.
Point
(
23
,
528
);
this
.
label6
.
Name
=
"label6"
;
this
.
label6
.
Size
=
new
System
.
Drawing
.
Size
(
82
,
15
);
this
.
label6
.
TabIndex
=
100
;
this
.
label6
.
Text
=
"超时次数:"
;
//
// Form1
//
this
.
AutoScaleDimensions
=
new
System
.
Drawing
.
SizeF
(
8F
,
15F
);
this
.
AutoScaleMode
=
System
.
Windows
.
Forms
.
AutoScaleMode
.
Font
;
this
.
ClientSize
=
new
System
.
Drawing
.
Size
(
1184
,
672
);
this
.
Controls
.
Add
(
this
.
label6
);
this
.
Controls
.
Add
(
this
.
label5
);
this
.
Controls
.
Add
(
this
.
SubscribeBut
);
this
.
Controls
.
Add
(
this
.
button2
);
this
.
Controls
.
Add
(
this
.
label4
);
...
...
@@ -445,5 +467,7 @@
private
System
.
Windows
.
Forms
.
Label
label4
;
private
System
.
Windows
.
Forms
.
Button
button2
;
private
System
.
Windows
.
Forms
.
Button
SubscribeBut
;
private
System
.
Windows
.
Forms
.
Label
label5
;
private
System
.
Windows
.
Forms
.
Label
label6
;
}
}
\ No newline at end of file
ModbusDemo/windows/Form1.cs
View file @
1dfb305f
...
...
@@ -532,7 +532,7 @@ namespace ModbusDemo
#
region
modbus
线程
private
void
GetData
()
{
timer
.
Interval
=
5
000
;
timer
.
Interval
=
9
000
;
timer
.
Enabled
=
true
;
timer
.
AutoReset
=
true
;
//设置是执行一次(false)还是一直执行(true);
timer
.
Start
();
...
...
@@ -578,8 +578,16 @@ namespace ModbusDemo
Modbus_polling
();
}
}
#
region
PoolTest
//pcPool cPool1 = new pcPool("East_transfer_pond");
//double PoolTemper = 0;
#
endregion
//声明委托
public
static
ushort
usCommunFailTimes
=
0
;
private
delegate
void
ShowMessageDelegate
(
string
message
);
private
void
ShowMessage
(
string
message
)
{
...
...
@@ -596,6 +604,7 @@ namespace ModbusDemo
}
public
void
Modbus_ReadHoldingRegistersTask
(
out
ushort
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
int
Caution
)
{
label5
.
Text
=
usCommunFailTimes
.
ToString
();
try
{
OutputValue
=
master
.
ReadHoldingRegisters
(
slaveAddress
,
startAddress
,
numberOfPoints
);
...
...
@@ -613,6 +622,7 @@ namespace ModbusDemo
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
OutputValue
=
new
ushort
[
numberOfPoints
];
Caution
=
-
1
;
usCommunFailTimes
++;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -675,6 +685,7 @@ namespace ModbusDemo
}
public
void
Modbus_ReadCoilsTask
(
out
bool
[]
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
ushort
numberOfPoints
,
out
int
Caution
)
{
label5
.
Text
=
usCommunFailTimes
.
ToString
();
try
{
OutputValue
=
master
.
ReadCoils
(
slaveAddress
,
startAddress
,
numberOfPoints
);
...
...
@@ -692,6 +703,7 @@ namespace ModbusDemo
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
OutputValue
=
new
bool
[
numberOfPoints
];
Caution
=
-
1
;
usCommunFailTimes
++;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -754,6 +766,7 @@ namespace ModbusDemo
}
public
void
Modbus_WriteSingleCoil
(
out
bool
OutputValue
,
byte
slaveAddress
,
ushort
startAddress
,
bool
value
,
out
int
Caution
)
{
label5
.
Text
=
usCommunFailTimes
.
ToString
();
try
{
master
.
WriteSingleCoil
(
slaveAddress
,
startAddress
,
value
);
...
...
@@ -772,6 +785,7 @@ namespace ModbusDemo
totxt
.
Log
(
DateTime
.
Now
.
ToString
()
+
" "
+
exception
.
Message
);
OutputValue
=
false
;
Caution
=
-
1
;
usCommunFailTimes
++;
}
//The server return error code.
//You can get the function code and exception code.
...
...
@@ -855,36 +869,33 @@ namespace ModbusDemo
int
[]
EOnOff
=
{
0
};
int
[]
SOnOff
=
{
0
};
int
[]
WOnOff
=
{
0
};
Modbus_ReadHoldingRegistersTask
(
out
temperature
,
1
,
0
,
4
,
out
int
signT
);
Modbus_ReadHoldingRegistersTask
(
out
type
,
1
,
10
,
4
,
out
sign
);
Modbus_ReadHoldingRegistersTask
(
out
temperature
,
27
,
0
,
4
,
out
int
signT
);
Modbus_ReadHoldingRegistersTask
(
out
type
,
27
,
10
,
4
,
out
sign
);
UInt64
time_1
=
GetTimeStamp
();
Modbus_ReadHoldingRegistersTask
(
out
register
,
3
,
0
,
8
,
out
int
signr
);
Modbus_ReadHoldingRegistersTask
(
out
Inputtype
,
3
,
10
,
8
,
out
int
signI
);
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
,
6
,
0
,
8
,
out
int
signre
);
Modbus_ReadHoldingRegistersTask
(
out
InputtypeN
,
6
,
10
,
8
,
out
int
signIn
);
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
,
5
,
16
,
6
,
out
int
signE
);
Modbus_ReadCoilsTask
(
out
EIO
,
40
,
16
,
6
,
out
int
signE
);
Modbus_ReadCoilsTask
(
out
SIO
,
8
,
16
,
6
,
out
int
signS
);
Modbus_ReadCoilsTask
(
out
SIO
,
19
,
16
,
6
,
out
int
signS
);
Modbus_ReadCoilsTask
(
out
WIO
,
4
,
16
,
6
,
out
int
signW
);
Modbus_ReadCoilsTask
(
out
WIO
,
4
1
,
16
,
6
,
out
int
signW
);
if
(
sign
==
-
1
)
{
//if (sign == -1 || signT ==-1|| signW ==-1 || signr == -1 || signI == -1 || signre == -1 || signIn == -1 || signE == -1 || signS == -1)
//{
// vs = null;
// type = null;
// EastTemperature = 0;
// Easternwaterlevel = 0;
// WestWaterLevel = 0;
//}
}
//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
;
...
...
@@ -895,18 +906,16 @@ namespace ModbusDemo
WOnOff
=
state
.
IO
(
WIO
);
////东南北水
//}
WTDR14P
x
=
new
WTDR14P
(
"1"
,
EastTemperature
,
0
,
0
,
0
,
time_1
);
WTDR18X
p
=
new
WTDR18X
(
"3"
,
Easternwaterlevel
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
time_2
);
WTDR18X
q
=
new
WTDR18X
(
"6"
,
WestWaterLevel
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
time_3
);
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
(
"
5
"
,
EOnOff
[
0
],
EOnOff
[
1
],
EOnOff
[
2
],
EOnOff
[
3
],
EOnOff
[
4
],
EOnOff
[
5
],
GetTimeStamp
());
WTDR66C
EastStir
=
new
WTDR66C
(
"
40
"
,
EOnOff
[
0
],
EOnOff
[
1
],
EOnOff
[
2
],
EOnOff
[
3
],
EOnOff
[
4
],
EOnOff
[
5
],
GetTimeStamp
());
//东南北水
WTDR66C
WestStir
=
new
WTDR66C
(
"4"
,
WOnOff
[
0
],
WOnOff
[
1
],
WOnOff
[
2
],
WOnOff
[
3
],
WOnOff
[
4
],
WOnOff
[
5
],
GetTimeStamp
());
WTDR66C
WestStir
=
new
WTDR66C
(
"4
1
"
,
WOnOff
[
0
],
WOnOff
[
1
],
WOnOff
[
2
],
WOnOff
[
3
],
WOnOff
[
4
],
WOnOff
[
5
],
GetTimeStamp
());
//西搅拌
WTDR66C
SouthWater
=
new
WTDR66C
(
"
8
"
,
SOnOff
[
0
],
SOnOff
[
1
],
SOnOff
[
2
],
SOnOff
[
3
],
SOnOff
[
4
],
SOnOff
[
5
],
GetTimeStamp
());
WTDR66C
SouthWater
=
new
WTDR66C
(
"
19
"
,
SOnOff
[
0
],
SOnOff
[
1
],
SOnOff
[
2
],
SOnOff
[
3
],
SOnOff
[
4
],
SOnOff
[
5
],
GetTimeStamp
());
mqttData
=
"["
+
JsonConvert
.
SerializeObject
(
x
)
+
","
+
...
...
@@ -916,10 +925,63 @@ namespace ModbusDemo
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();
// Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI);
// UInt64 time_2 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn);
// UInt64 time_3 = GetTimeStamp();
// Modbus_ReadCoilsTask(out EIO, 5, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out SIO, 8, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW);
// if (sign == -1)
// {
// }
// //else
// //{
// vs = rtd.RtdValue(temperature, type);
// EastTemperature = float.Parse(vs[0]);
// s = Analog.Value(register, Inputtype);
// 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("1", EastTemperature, 0, 0, 0, time_1);
// WTDR18X p = new WTDR18X("3", Easternwaterlevel, 0, 0, 0, 0, 0, 0, 0, time_2);
// WTDR18X q = new WTDR18X("6", WestWaterLevel, 0, 0, 0, 0, 0, 0, 0, time_3);
// //东搅拌
// WTDR66C EastStir = new WTDR66C("5", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// //东南北水
// WTDR66C WestStir = new WTDR66C("4", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp());
// //西搅拌
// WTDR66C SouthWater = new WTDR66C("8", 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)
// + "]";
//}
catch
(
Exception
exception
)
{
//Connection exception
...
...
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