Commit 432ed26a authored by leon.huang's avatar leon.huang

Modify Modbus and Pool abstruct

parent ee7b5c46
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<Name>Database1</Name>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>4.1</ProjectVersion>
<ProjectGuid>{214ece0a-ae66-4aad-a2ba-5c06fa5d5d97}</ProjectGuid>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider</DSP>
<OutputType>Database</OutputType>
<RootPath>
</RootPath>
<RootNamespace>Database1</RootNamespace>
<AssemblyName>Database1</AssemblyName>
<ModelCollation>1033, CI</ModelCollation>
<DefaultFileStructure>BySchemaAndSchemaType</DefaultFileStructure>
<DeployToDatabase>True</DeployToDatabase>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetLanguage>CS</TargetLanguage>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SqlServerVerification>False</SqlServerVerification>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseSet>True</TargetDatabaseSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
</ItemGroup>
</Project>
\ No newline at end of file
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio Version 16
VisualStudioVersion = 15.0.27703.2042 VisualStudioVersion = 16.0.28803.452
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusDemo", "ModbusDemo\ModbusDemo.csproj", "{7D83EE1F-F3D9-4592-A0ED-A0D92DCF91AB}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusDemo", "ModbusDemo\ModbusDemo.csproj", "{7D83EE1F-F3D9-4592-A0ED-A0D92DCF91AB}"
EndProject EndProject
......
...@@ -8,12 +8,11 @@ using System.Windows.Forms; ...@@ -8,12 +8,11 @@ using System.Windows.Forms;
namespace ModbusDemo.windows namespace ModbusDemo.windows
{ {
class Totxt public class Totxt
{ {
private string logFile; private string logFile;
private StreamWriter writer; private StreamWriter writer;
private FileStream fileStream = null; private FileStream fileStream = null;
public Totxt(string fileName) public Totxt(string fileName)
{ {
logFile = fileName; logFile = fileName;
...@@ -22,7 +21,6 @@ namespace ModbusDemo.windows ...@@ -22,7 +21,6 @@ namespace ModbusDemo.windows
public void Log(string info) public void Log(string info)
{ {
try try
{ {
System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile); System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile);
...@@ -59,7 +57,6 @@ namespace ModbusDemo.windows ...@@ -59,7 +57,6 @@ namespace ModbusDemo.windows
fileStream.Dispose(); fileStream.Dispose();
} }
} }
} }
public void CreateDirectory(string infoPath) public void CreateDirectory(string infoPath)
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modbus.Device;
using System.Windows.Forms;
using System.Timers;
using ModbusDemo.windows;
using ModbusDemo;
namespace ModbusDemo.MessageFormat
{
public static class ModbusComm
{
public static ModbusMaster Modbus;
public static void Modbus_ReadHoldingRegistersTask(out ushort[] OutputValue, byte slaveAddress, ushort startAddress, ushort numberOfPoints, out bool bCommStatus)
{
try
{
OutputValue = Modbus.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
bCommStatus = true;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
OutputValue = new ushort[numberOfPoints];
bCommStatus = false;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
str = str.Remove(0, str.IndexOf("\r\n") + 17);
FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
Form1.totxt.Log("Function Code: " + FunctionCode.ToString("X"));
str = str.Remove(0, str.IndexOf("\r\n") + 17);
ExceptionCode = str.Remove(str.IndexOf("-"));
switch (ExceptionCode.Trim())
{
case "1":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
OutputValue = new ushort[numberOfPoints];
bCommStatus = false;
}
}
public static void Modbus_ReadCoilsTask(out bool[] OutputValue, byte slaveAddress, ushort startAddress, ushort numberOfPoints, out bool bComStatus)
{
try
{
OutputValue = Modbus.ReadCoils(slaveAddress, startAddress, numberOfPoints);
bComStatus = true;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
OutputValue = new bool[numberOfPoints];
bComStatus = false;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
str = str.Remove(0, str.IndexOf("\r\n") + 17);
FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
Form1.totxt.Log("Function Code: " + FunctionCode.ToString("X"));
str = str.Remove(0, str.IndexOf("\r\n") + 17);
ExceptionCode = str.Remove(str.IndexOf("-"));
switch (ExceptionCode.Trim())
{
case "1":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
OutputValue = new bool[numberOfPoints];
bComStatus = false;
}
}
public static void Modbus_WriteSingleCoil(byte slaveAddress, ushort startAddress, bool[] value, out bool bComStatus)
{
try
{
Modbus.WriteMultipleCoils(slaveAddress, startAddress, value);
bComStatus = true;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
bComStatus = false;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
str = str.Remove(0, str.IndexOf("\r\n") + 17);
FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
Form1.totxt.Log("Function Code: " + FunctionCode.ToString("X"));
str = str.Remove(0, str.IndexOf("\r\n") + 17);
ExceptionCode = str.Remove(str.IndexOf("-"));
switch (ExceptionCode.Trim())
{
case "1":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
bComStatus = false;
}
}
public static void Modbus_WriteMultipleCoils(byte slaveAddress, ushort startAddress, bool[] data, out bool bComStatus)
{
{
try
{
Modbus.WriteMultipleCoils(slaveAddress, startAddress, data);
bComStatus = true;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
bComStatus = false;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
str = str.Remove(0, str.IndexOf("\r\n") + 17);
FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
Form1.totxt.Log("Function Code: " + FunctionCode.ToString("X"));
str = str.Remove(0, str.IndexOf("\r\n") + 17);
ExceptionCode = str.Remove(str.IndexOf("-"));
switch (ExceptionCode.Trim())
{
case "1":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
Form1.totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
bComStatus = false;
}
}
}
//private void Init_Read_data()
//{
//}
//private async void Modbus_polling()
//{
// float EastTemperature = 0;
// float Easternwaterlevel = 0;
// float WestWaterLevel = 0;
//ushort[] temperature = { };
//ushort[] type = { };
//string[] vs = { };
//string[] s = { };
//string[] s6 = { };
//ushort[] register = { };
//ushort[] Inputtype = { };
//ushort[] registerN = { };
//ushort[] InputtypeN = { };
//bool[] EIO = { };
//bool[] SIO = { };
//bool[] WIO = { };
//int[] EOnOff = { 0 };
//int[] SOnOff = { 0 };
//int[] WOnOff = { 0 };
//pcDeviceAlarm pcDeviceAlarmdata = new pcDeviceAlarm();
//Modbus_ReadHoldingRegistersTask(out temperature, 1, 0, 4, out int signT);
//Modbus_ReadHoldingRegistersTask(out type, 1, 10, 4, out int sign);
//UInt64 time_1 = Form1.GetTimeStamp();
//Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr);
//Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI);
//UInt64 time_2 = Form1.GetTimeStamp();
//Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre);
//Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn);
//UInt64 time_3 = Form1.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)
//{
// ushort bAddress = 1;
// ushort[] bChannel = new ushort[pcDeviceAlarm.usChannelNumber];
// pcDeviceAlarm.eAlarmCode[] eAlarmCode = new pcDeviceAlarm.eAlarmCode[pcDeviceAlarm.usChannelNumber];
// for (ushort i = 0; i < pcDeviceAlarm.usChannelNumber; i++)
// {
// eAlarmCode[i] = pcDeviceAlarm.eAlarmCode.CommunError;
// }
// pcDeviceAlarmdata.vSetAlarmList(bAddress, bChannel, eAlarmCode);
// await pcDeviceAlarmdata.vPublishAlarmPackageJson(mqttClient);
//}
//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);
//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], Form1.GetTimeStamp());
////东南北水
//WTDR66C WestStir = new WTDR66C("4", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], Form1.GetTimeStamp());
////西搅拌
//WTDR66C SouthWater = new WTDR66C("8", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], Form1.GetTimeStamp());
// mqttData = "[" +
// JsonConvert.SerializeObject(x) + "," +
// JsonConvert.SerializeObject(p) + "," +
// JsonConvert.SerializeObject(q) + "," +
// JsonConvert.SerializeObject(EastStir) + "," +
// JsonConvert.SerializeObject(WestStir) + "," +
// JsonConvert.SerializeObject(SouthWater)
// + "]";
//}
}
}
...@@ -4,6 +4,10 @@ using System.Linq; ...@@ -4,6 +4,10 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections; using System.Collections;
using MQTTnet;
using MQTTnet.Client;
using Newtonsoft.Json;
namespace ModbusDemo namespace ModbusDemo
{ {
class startAddress class startAddress
...@@ -24,8 +28,8 @@ namespace ModbusDemo ...@@ -24,8 +28,8 @@ namespace ModbusDemo
public int d5 { get; set; } public int d5 { get; set; }
public int d6 { get; set; } public int d6 { get; set; }
public int d7 { get; set; } public int d7 { get; set; }
public string ts { get; set; } public UInt64 ts { get; set; }
public WTDR18X(string addr, double d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7, string ts) public WTDR18X(string addr, double d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7, UInt64 ts)
{ {
this.addr = addr; this.addr = addr;
this.d0 = d0; this.d0 = d0;
...@@ -46,8 +50,8 @@ namespace ModbusDemo ...@@ -46,8 +50,8 @@ namespace ModbusDemo
public int d1 { get; set; } public int d1 { get; set; }
public int d2 { get; set; } public int d2 { get; set; }
public int d3 { get; set; } public int d3 { get; set; }
public string ts { get; set; } public UInt64 ts { get; set; }
public WTDR14P(string addr, double d0, int d1, int d2, int d3, string ts) public WTDR14P(string addr, double d0, int d1, int d2, int d3, UInt64 ts)
{ {
this.addr = addr; this.addr = addr;
this.d0 = d0; this.d0 = d0;
...@@ -66,9 +70,9 @@ namespace ModbusDemo ...@@ -66,9 +70,9 @@ namespace ModbusDemo
public int d3 { get; set; } public int d3 { get; set; }
public int d4 { get; set; } public int d4 { get; set; }
public int d5 { get; set; } public int d5 { get; set; }
public string ts { get; set; } public UInt64 ts { get; set; }
public WTDR66C(string addr, int d0, int d1, int d2, int d3, int d4, int d5, string ts) public WTDR66C(string addr, int d0, int d1, int d2, int d3, int d4, int d5, UInt64 ts)
{ {
this.addr = addr; this.addr = addr;
this.d0 = d0; this.d0 = d0;
...@@ -220,4 +224,47 @@ namespace ModbusDemo ...@@ -220,4 +224,47 @@ namespace ModbusDemo
return op; return op;
} }
} }
public class pcDeviceAlarm
{
public const ushort usChannelNumber = 8;
private const string sAlarmTopic = "Witium/WTDS78X/20193261/Alarm";
public Hashtable hMqtttAlarmMap = new Hashtable();
public bool bErrorHasSend = false;
private string sAlarmJson;
private string sAlarmJsonPackage;
//private IMqttClient mqttClient = null;
public enum eAlarmCode : sbyte
{
ChannelNoError = 0,
ChannelCommunError = 1,
ChannelCollectDataError = 2,
ChannelDataRespError = 4,
ChannelDataChangeError = 8
}
public void vSetAlarmList(ushort usAddress, ushort[] usChannel, eAlarmCode[] usChannelAlarmcode)
{
ushort i = 0;
hMqtttAlarmMap.Add("addr", usAddress.ToString());
string sAlarm = "alm";
foreach (ushort usdata in usChannelAlarmcode)
{
hMqtttAlarmMap.Add(sAlarm + i, usChannelAlarmcode[i]);
i++;
}
hMqtttAlarmMap.Add("ts", Form1.GetTimeStamp());
sAlarmJson = JsonConvert.SerializeObject(hMqtttAlarmMap);
}
public async Task vPublishAlarmPackageJson(IMqttClient mqttClient)
{
sAlarmJson = "[" + sAlarmJson + "]";
var message = new MqttApplicationMessageBuilder()
.WithTopic(sAlarmTopic)
.WithPayload(sAlarmJson)
.WithAtMostOnceQoS()
.WithRetainFlag(false)
.Build();
await mqttClient.PublishAsync(message);
}
}
} }
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SSDTUnitTestPath Condition="'$(SSDTUnitTestPath)' == ''">$(VsInstallRoot)\Common7\IDE\Extensions\Microsoft\SQLDB</SSDTUnitTestPath>
</PropertyGroup>
<PropertyGroup>
<SSDTPath Condition="'$(SSDTPath)' == ''">$(VsInstallRoot)\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\130</SSDTPath>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
...@@ -59,15 +68,15 @@ ...@@ -59,15 +68,15 @@
<Reference Include="log4net"> <Reference Include="log4net">
<HintPath>Lib\log4net.dll</HintPath> <HintPath>Lib\log4net.dll</HintPath>
</Reference> </Reference>
<Reference Include="MQTTnet, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MQTTnet, Version=2.8.5.0, Culture=neutral, PublicKeyToken=b69712f52770c0a7, processorArchitecture=MSIL">
<HintPath>..\packages\MQTTnet-CP.2.7.5\lib\net452\MQTTnet.dll</HintPath> <HintPath>..\packages\MQTTnet.2.8.5\lib\net452\MQTTnet.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="nmodbuspc"> <Reference Include="nmodbuspc, Version=1.12.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>Lib\nmodbuspc.dll</HintPath> <SpecificVersion>False</SpecificVersion>
<HintPath>bin\Debug\nmodbuspc.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
...@@ -81,7 +90,10 @@ ...@@ -81,7 +90,10 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="application\PoolModbusSet.cs" />
<Compile Include="application\ModbusToPool.cs" />
<Compile Include="application\Pond.cs" /> <Compile Include="application\Pond.cs" />
<Compile Include="MessageFormat\ModbusComm.cs" />
<Compile Include="Modular\414P.cs" /> <Compile Include="Modular\414P.cs" />
<Compile Include="Modular\478C.cs" /> <Compile Include="Modular\478C.cs" />
<Compile Include="Modular\418X.cs" /> <Compile Include="Modular\418X.cs" />
...@@ -148,7 +160,33 @@ ...@@ -148,7 +160,33 @@
<Content Include="picture\nut_128px_1101797_easyicon.net.ico" /> <Content Include="picture\nut_128px_1101797_easyicon.net.ico" />
<None Include="Resources\logo.png" /> <None Include="Resources\logo.png" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<ItemGroup />
<ItemGroup Condition="$(VisualStudioVersion) == '15.0'">
<Reference Include="Microsoft.Data.Tools.Schema.Sql, Version=13.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>$(SSDTPath)\Microsoft.Data.Tools.Schema.Sql.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.Tools.Schema.Sql.UnitTesting, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>$(SSDTUnitTestPath)\Microsoft.Data.Tools.Schema.Sql.UnitTesting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.Tools.Schema.Sql.UnitTestingAdapter, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>$(SSDTUnitTestPath)\Microsoft.Data.Tools.Schema.Sql.UnitTestingAdapter.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<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>
<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)' == ''" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">
......
...@@ -8,6 +8,7 @@ namespace ModbusDemo ...@@ -8,6 +8,7 @@ namespace ModbusDemo
{ {
class _414P class _414P
{ {
//铂电阻+2路开关 //铂电阻+2路开关
const int MAX_AN_CH_NUM = 4; const int MAX_AN_CH_NUM = 4;
ushort[] typeData = new ushort[MAX_AN_CH_NUM];//当前量程读数 ushort[] typeData = new ushort[MAX_AN_CH_NUM];//当前量程读数
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ModbusDemo.windows;
using ModbusDemo;
using ModbusDemo.MessageFormat;
namespace ModbusDemo.application
{
class ModbusToPool
{
public class pcModbusOperationPumb
{
public byte byModbusID;
public ushort[] usRegAddList;
public bool[] bGetStatus;
public bool[] bSetStatus;
public bool[] bOpenStatus;
public bool bCommunicationStatus;
public pcModbusOperationPumb(byte byInitModbusID, ushort[] usInitRegAddressInit, bool[] bInitOPen)
{
this.byModbusID = byInitModbusID;
for (int i = 0; i < usInitRegAddressInit.Length; i++)
{
this.usRegAddList[i] = usInitRegAddressInit[i];
this.bOpenStatus[i] = bInitOPen[i];
}
}
public void vGet_Module_Channel_Status()
{
ModbusComm.Modbus_ReadCoilsTask(out this.bGetStatus, this.byModbusID, this.usRegAddList[0], (ushort)this.usRegAddList.Length, out this.bCommunicationStatus);
}
public void vSet_Module_Channel_Status()
{
ModbusComm.Modbus_WriteMultipleCoils(this.byModbusID, this.usRegAddList[0], this.bSetStatus, out this.bCommunicationStatus);
}
}
public class pcReadPumbOtherData
{
public byte byModbusID;
public ushort[] usRegAddList;
public ushort[] usReadData;
public bool bCommunicationStatus;
public pcReadPumbOtherData(byte byInitModbusID, ushort[] usInitRegAddressInit)
{
this.byModbusID = byInitModbusID;
for (int i = 0; i < usInitRegAddressInit.Length; i++)
{
this.usRegAddList[i] = usInitRegAddressInit[i];
}
}
public void vRead_Pumb_Data()
{
ModbusComm.Modbus_ReadHoldingRegistersTask(out this.usReadData, this.byModbusID, this.usRegAddList[0], (ushort)this.usRegAddList.Length, out this.bCommunicationStatus);
}
}
public class pcPoolTemperature
{
public byte byModbusID;
public ushort usTemperRegAddress;
public ushort usTypeRegAddress;
public ushort[] usReadData;
public ushort[] usReadType;
public bool bCommunicationStatus;
public pcPoolTemperature(byte byInitModbusID, ushort usDataRegAddress, ushort usTypeAddress)
{
this.byModbusID = byInitModbusID;
this.usTemperRegAddress = usDataRegAddress;
this.usTypeRegAddress = usTypeAddress;
}
public void vRead_Pumb_Data()
{
ModbusComm.Modbus_ReadHoldingRegistersTask(out this.usReadData, this.byModbusID, this.usTemperRegAddress, 1, out this.bCommunicationStatus);
}
public void vRead_Pumb_Type()
{
ModbusComm.Modbus_ReadHoldingRegistersTask(out this.usReadType, this.byModbusID, this.usTypeRegAddress, 1, out this.bCommunicationStatus);
}
}
public class pcPoolLevel
{
public byte byModbusID;
public ushort[] usTemperRegAddress;
public ushort[] usTypeRegAddress;
public ushort[] usReadData;
public ushort[] usReadType;
public bool bCommunicationStatus;
public pcPoolLevel(byte byInitModbusID, ushort[] usDataRegAddress, ushort[] usDataTypeRegAddress)
{
this.byModbusID = byInitModbusID;
for (int i = 0; i < usDataRegAddress.Length; i++)
{
this.usTemperRegAddress[i] = usDataRegAddress[i];
this.usTypeRegAddress[i] = usDataTypeRegAddress[i];
}
}
public void vRead_Level_Data()
{
ModbusComm.Modbus_ReadHoldingRegistersTask(out this.usReadData, this.byModbusID, this.usTemperRegAddress[0], (ushort)this.usTemperRegAddress.Length, out this.bCommunicationStatus);
}
public void vRead_Level_Type()
{
ModbusComm.Modbus_ReadHoldingRegistersTask(out this.usReadType, this.byModbusID, this.usTypeRegAddress[0], (ushort)this.usTypeRegAddress.Length, out this.bCommunicationStatus);
}
}
}
}
...@@ -7,50 +7,235 @@ using System.Windows.Forms; ...@@ -7,50 +7,235 @@ using System.Windows.Forms;
using ModbusDemo; using ModbusDemo;
namespace ModbusDemo.application namespace ModbusDemo.application
{ {
class Pond // public class Pond
{ // {
private string Sname;//池子的名称 //private string Sname;//池子的名称
private bool state; //private bool state;
private double Dlevel;//池子的液位 //private double Dlevel;//池子的液位
private double Lowwaterlevel;//液位最低水位 //private double Lowwaterlevel;//液位最低水位
private double Peaklevel;//液位最高水位 //private double Peaklevel;//液位最高水位
private string Agitator;//搅拌器 //private string Agitator;//搅拌器
private ushort current;//电流 //private ushort current;//电流
private ushort Voltage;//电压 //private ushort Voltage;//电压
public string Sname1 { get => Sname; set => Sname = value; } //private float vs;
public bool State { get => state; set => state = value; }
public double Dlevel1 { get => Dlevel; set => Dlevel = value; }
public double Lowwaterlevel1 { get => Lowwaterlevel; set => Lowwaterlevel = value; }
public double Peaklevel1 { get => Peaklevel; set => Peaklevel = value; }
public ushort Voltage1 { get => Voltage; set => Voltage = value; }
public ushort Current { get => current; set => current = value; }
public string Agitator1 { get => Agitator; set => Agitator = value; }
//public class Agitator : ModbusAttribute //搅拌器
//{
// public bool state;
// public ushort[] current;//电流
// public ushort[] Voltage;//电压
//}
//public class pump : ModbusAttribute//泵
//{
// public bool state;
// public ushort[] current;//电流
// public ushort[] Voltage;//电压
//}
//public class Level : ModbusAttribute //液位属性 //public string Sname1 { get => Sname; set => Sname = value; }
//{ //public bool State { get => state; set => state = value; }
// public ushort[] level;//液位 //public double Dlevel1 { get => Dlevel; set => Dlevel = value; }
// public ushort[] passageway; //液位通道 //public double Lowwaterlevel1 { get => Lowwaterlevel; set => Lowwaterlevel = value; }
// public double[] Lowwaterlevel; // 液位最低水位 //public double Peaklevel1 { get => Peaklevel; set => Peaklevel = value; }
// public double[] Peaklevel; // 液位最高水位 //public ushort Voltage1 { get => Voltage; set => Voltage = value; }
//} //public ushort Current { get => current; set => current = value; }
//public class ModbusAttribute //modbus属性 //public string Agitator1 { get => Agitator; set => Agitator = value; }
//public float Vs { get => vs; set => vs = value; }
//public class Agitator : ModbusAttribute //搅拌器
//{
// public bool state;
// public ushort[] current;//电流
// public ushort[] Voltage;//电压
//}
//public class pump : ModbusAttribute//泵
//{
// public bool state;
// public ushort[] current;//电流
// public ushort[] Voltage;//电压
//}
//public class Level : ModbusAttribute //液位属性
//{
// public ushort[] level;//液位
// public ushort[] passageway; //液位通道
// public double[] Lowwaterlevel; // 液位最低水位
// public double[] Peaklevel; // 液位最高水位
//}
//public class ModbusAttribute //modbus属性
//{
// public byte[] SlaveAddress;
// public ushort[] RegisterAddress;
//}
//public class pcPool
//{ //{
// public byte[] SlaveAddress; // public pePondName ePondName;
// public ushort[] RegisterAddress; // private double dPondCurentTemper1;
// private double dPondCurrentLevel1;
// public double dMaxlevel;//液位最低水位
// public double dMinlevel;//液位最高水位
// public double dMaxTemper;
// public double dMinTemper;
// public pcPump[] cPump;
// public pcAgitator[] cAgitator;
// public double dPondCurentTemper { get => dPondCurentTemper1; }
// public double dPondCurrentLevel { get => dPondCurrentLevel1; }
// public pcPool(pePondName eInitPondName)
// {
// this.ePondName = eInitPondName;
// }
//} //}
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 vGet_Pump_Status()
{
}
} }
public class pcTemperatureData
{
public double Temperature;
}
public class pcLevelHigh
{
}
public class pcSensorModule
{
public double dCalculateData;
public peSensorType eSensorType;
public ushort[] usCollectData;
public ushort[] usGetCurrentType;
public byte bDataCollectModualID;
public ushort[] usDataRegAddress;
public ushort usTypeRegAddress;
//public bool ReadDataStatus;
public int uReadModbusStatus;
public pcSensorModule(peSensorType eInitSensorType,byte bInitModbusID, ushort[] usInitRegAddress,ushort usInitTypedefAddress)
{
this.eSensorType = eInitSensorType;
this.bDataCollectModualID = bInitModbusID;
this.usTypeRegAddress = usInitTypedefAddress;
for (ushort i = 0; i < usInitRegAddress.Length; i++)
{
this.usDataRegAddress[i] = usInitRegAddress[i];
}
}
public void vGet_CollectData()
{
//Form1.Modbus_ReadHoldingRegistersTask(out this.usCollectData, this.bDataCollectModualID, this.usDataRegAddress[0], ((ushort)this.usDataRegAddress.Length), out this.uReadModbusStatus);
}
public void vGet_Sensor_Type()
{
//Form1.Modbus_ReadHoldingRegistersTask(out this.usGetCurrentType, this.bDataCollectModualID, this.usTypeRegAddress, 1, out this.uReadModbusStatus);
}
public void vGetCalculate_Send_Data()
{
if (this.eSensorType == peSensorType.X14p)
{
//this.dCalculateData =
}
else if (this.eSensorType == peSensorType.X78C)
{
//this.dCalculateData =
}
}
}
public class pcControlModule
{
public peControlName eControlMachineName;
public bool bMachineStatus;
public bool[] bMachineOpenSatus;
public bool[] bGetChannelStatus;
public bool[] bSetChannelStatus;
public byte bControlModualID;
public ushort[] usRegNumberlist;
public ulong[] ulControlChannel;
public int uReadModbusStatus;
public pcControlModule(peControlName eInitMachineName,byte bInitModbusID, ushort[] usInitRegAddress,bool[] bDefineMachineOpenStatus)
{
this.eControlMachineName = eInitMachineName;
this.bControlModualID = bInitModbusID;
for (ushort i = 0; i < usInitRegAddress.Length; i++)
{
this.usRegNumberlist[i] = usInitRegAddress[i];
this.bMachineOpenSatus[i] = bDefineMachineOpenStatus[i];
}
}
public void vOpen_Machine()
{
}
public void vClose_Machine()
{
}
public void vGet_Machine_Modual_Channel_Status()
{
//Form1.Modbus_ReadCoilsTask(out this.bGetChannelStatus, this.bControlModualID,this.usRegNumberlist[0],(ushort)this.usRegNumberlist.Length, out this.uReadModbusStatus);
}
public void vSet_Machinae_Modual_Channel_Status()
{
//Form1.Modbus_WriteSingleCoil(out this.bGetChannelStatus[0], this.bControlModualID, this.usRegNumberlist[0], bSetChannelStatus[0], out this.uReadModbusStatus);
}
}
public enum peSensorType : sbyte
{
Idle = 0,
X14p,
X78C
}
public enum pePondName : sbyte
{
Idel = 0,
EastTransitPool,
WestTransitPool,
GritBasinPool,
OctagonalPool,
SlurryPool,
SeparateRoom,
FermentationPool,
DischargePool
}
public enum peControlName : sbyte
{
Idle = 0,
NorthSouthWater,
Pump0Machine = 10,
Pump1Machine,
Pump2Machine,
Pump3Machine,
Pump4Machine,
Pump5Machine,
Pump6Machine,
Pump7Machine,
Pump8Machine,
Pump9Machine,
Stir0Machine = 100,
Stir1Machine,
Stir2Machine,
Stir3Machine,
Stir4Machine,
Stir5Machine,
Stir6Machine,
Stir7Machine,
Stir8Machine,
Stir9Machine,
}
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModbusDemo.application
{
public struct psTemperData
{
public byte byModbusID;
public ushort usTemperRegAddress;
public ushort usTypeRegAddress;
public psTemperData(byte bySetModbusID, ushort usSetTempRegAddress, ushort usSetRegAddress)
{
this.byModbusID = bySetModbusID;
this.usTemperRegAddress = usSetTempRegAddress;
this.usTypeRegAddress = usSetRegAddress;
}
}
public struct psBumpData
{
public byte byModbusID;
public ushort[] usRegAddList;
public bool[] bOpenStatus;
public psBumpData(byte byInitModbusID, ushort[] usInitRegAddressInit, bool[] bInitOPen)
{
this.byModbusID = byInitModbusID;
this.usRegAddList = usInitRegAddressInit;
this.bOpenStatus = bInitOPen;
}
}
public struct psLevel
{
public byte byModbusID;
public ushort[] usDataAddressList;
public ushort[] usTypeAddressList;
public psLevel(byte byInitModbusID, ushort[] usDataRegAddress, ushort[] usDataTypeRegAddress)
{
this.byModbusID = byInitModbusID;
this.usDataAddressList = usDataRegAddress;
this.usTypeAddressList = usDataTypeRegAddress;
}
}
//public struct psPumbPeration
//{
// byte byInitModbusID, ushort[] usInitRegAddressInit, bool[] bInitOPen
//}
static class pcPoolInit
{
private static psTemperData sTemper = new psTemperData(1, 2, 3);
private static ModbusToPool.pcPoolTemperature cPoolTemperature = new ModbusToPool.pcPoolTemperature(sTemper.byModbusID, sTemper.usTemperRegAddress, sTemper.usTypeRegAddress);
public static double Get_Current_temper()
{
cPoolTemperature.vRead_Pumb_Data();
cPoolTemperature.vRead_Pumb_Type();
return cPoolTemperature.usReadData[0];
}
public const sbyte PoolBumpNumber = 4;
static ushort[] Data = { 12, 4 };
static bool[] Regdat = { true, false };
private static psBumpData psBumpData = new psBumpData(1, Data, Regdat);
public static ModbusToPool.pcModbusOperationPumb[] cPumb = new ModbusToPool.pcModbusOperationPumb[PoolBumpNumber] { new ModbusToPool.pcModbusOperationPumb(psBumpData.byModbusID, psBumpData.usRegAddList, psBumpData.bOpenStatus),
new ModbusToPool.pcModbusOperationPumb(psBumpData.byModbusID, psBumpData.usRegAddList, psBumpData.bOpenStatus) ,
new ModbusToPool.pcModbusOperationPumb( psBumpData.byModbusID, psBumpData.usRegAddList, psBumpData.bOpenStatus) ,
new ModbusToPool.pcModbusOperationPumb( psBumpData.byModbusID, psBumpData.usRegAddList, psBumpData.bOpenStatus)
};
//public static void vBumpSetting()
//{
// cPumb[0].vGet_Module_Channel_Status();
//}
}
public class pcAgitator
{
private double[] dBumpCurrent;
public double dBumpVoltage;
public bool bBumpStatus;
public bool bAgitatorHasOpen;
public double[] DBumpCurrent = new double[pcPoolInit.PoolBumpNumber] { pcPoolInit.cPumb[0].byModbusID, pcPoolInit.cPumb[0].byModbusID , pcPoolInit.cPumb[0].byModbusID , pcPoolInit.cPumb[0].byModbusID };
public void vOpen_Agitator()
{
}
public void vClose_Agitator()
{
}
public void vGet_Agitator_Status()
{
}
}
public class pcPoolConfig
{
public class pcPool
{
public pePondName ePondName;
public double dMaxlevel;//液位最低水位
public double dMinlevel;//液位最高水位
public double dMaxTemper;
public double dMinTemper;
public pcPump[] cPump;
public pcAgitator[] cAgitator;
public double dPondCurrentLevel;
public double dPondCurentTemper { get => pcPoolInit.Get_Current_temper(); }
public pcPool(pePondName eInitPondName)
{
this.ePondName = eInitPondName;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net452" /> <package id="MQTTnet" version="2.8.5" targetFramework="net452" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net452" />
<package id="NModbus4" version="2.1.0" targetFramework="net452" /> <package id="NModbus4" version="2.1.0" targetFramework="net452" />
</packages> </packages>
\ No newline at end of file
...@@ -68,18 +68,20 @@ ...@@ -68,18 +68,20 @@
this.cmbStopBit.Items.AddRange(new object[] { this.cmbStopBit.Items.AddRange(new object[] {
"1", "1",
"2"}); "2"});
this.cmbStopBit.Location = new System.Drawing.Point(346, 78); this.cmbStopBit.Location = new System.Drawing.Point(461, 98);
this.cmbStopBit.Margin = new System.Windows.Forms.Padding(4);
this.cmbStopBit.Name = "cmbStopBit"; this.cmbStopBit.Name = "cmbStopBit";
this.cmbStopBit.Size = new System.Drawing.Size(56, 22); this.cmbStopBit.Size = new System.Drawing.Size(73, 24);
this.cmbStopBit.TabIndex = 70; this.cmbStopBit.TabIndex = 70;
// //
// btCloseCOM // btCloseCOM
// //
this.btCloseCOM.Enabled = false; this.btCloseCOM.Enabled = false;
this.btCloseCOM.Font = new System.Drawing.Font("Arial", 8F); this.btCloseCOM.Font = new System.Drawing.Font("Arial", 8F);
this.btCloseCOM.Location = new System.Drawing.Point(324, 104); this.btCloseCOM.Location = new System.Drawing.Point(432, 130);
this.btCloseCOM.Margin = new System.Windows.Forms.Padding(4);
this.btCloseCOM.Name = "btCloseCOM"; this.btCloseCOM.Name = "btCloseCOM";
this.btCloseCOM.Size = new System.Drawing.Size(78, 23); this.btCloseCOM.Size = new System.Drawing.Size(104, 29);
this.btCloseCOM.TabIndex = 72; this.btCloseCOM.TabIndex = 72;
this.btCloseCOM.Text = "Close"; this.btCloseCOM.Text = "Close";
this.btCloseCOM.Click += new System.EventHandler(this.btCloseCOM_Click); this.btCloseCOM.Click += new System.EventHandler(this.btCloseCOM_Click);
...@@ -91,17 +93,19 @@ ...@@ -91,17 +93,19 @@
"0-None Parity", "0-None Parity",
"1-Odd Parity", "1-Odd Parity",
"2-Even Parity"}); "2-Even Parity"});
this.cmbParity.Location = new System.Drawing.Point(233, 78); this.cmbParity.Location = new System.Drawing.Point(311, 98);
this.cmbParity.Margin = new System.Windows.Forms.Padding(4);
this.cmbParity.Name = "cmbParity"; this.cmbParity.Name = "cmbParity";
this.cmbParity.Size = new System.Drawing.Size(100, 22); this.cmbParity.Size = new System.Drawing.Size(132, 24);
this.cmbParity.TabIndex = 69; this.cmbParity.TabIndex = 69;
// //
// btOpenCOM // btOpenCOM
// //
this.btOpenCOM.Font = new System.Drawing.Font("Arial", 8F); this.btOpenCOM.Font = new System.Drawing.Font("Arial", 8F);
this.btOpenCOM.Location = new System.Drawing.Point(233, 104); this.btOpenCOM.Location = new System.Drawing.Point(311, 130);
this.btOpenCOM.Margin = new System.Windows.Forms.Padding(4);
this.btOpenCOM.Name = "btOpenCOM"; this.btOpenCOM.Name = "btOpenCOM";
this.btOpenCOM.Size = new System.Drawing.Size(78, 23); this.btOpenCOM.Size = new System.Drawing.Size(104, 29);
this.btOpenCOM.TabIndex = 71; this.btOpenCOM.TabIndex = 71;
this.btOpenCOM.Text = "Open"; this.btOpenCOM.Text = "Open";
this.btOpenCOM.Click += new System.EventHandler(this.btOpenCOM_Click); this.btOpenCOM.Click += new System.EventHandler(this.btOpenCOM_Click);
...@@ -112,17 +116,19 @@ ...@@ -112,17 +116,19 @@
this.cmbDataBit.Items.AddRange(new object[] { this.cmbDataBit.Items.AddRange(new object[] {
"7", "7",
"8"}); "8"});
this.cmbDataBit.Location = new System.Drawing.Point(177, 78); this.cmbDataBit.Location = new System.Drawing.Point(236, 98);
this.cmbDataBit.Margin = new System.Windows.Forms.Padding(4);
this.cmbDataBit.Name = "cmbDataBit"; this.cmbDataBit.Name = "cmbDataBit";
this.cmbDataBit.Size = new System.Drawing.Size(48, 22); this.cmbDataBit.Size = new System.Drawing.Size(63, 24);
this.cmbDataBit.TabIndex = 68; this.cmbDataBit.TabIndex = 68;
// //
// labStopBit // labStopBit
// //
this.labStopBit.Font = new System.Drawing.Font("Arial", 8F); this.labStopBit.Font = new System.Drawing.Font("Arial", 8F);
this.labStopBit.Location = new System.Drawing.Point(346, 63); this.labStopBit.Location = new System.Drawing.Point(461, 79);
this.labStopBit.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labStopBit.Name = "labStopBit"; this.labStopBit.Name = "labStopBit";
this.labStopBit.Size = new System.Drawing.Size(56, 15); this.labStopBit.Size = new System.Drawing.Size(75, 19);
this.labStopBit.TabIndex = 73; this.labStopBit.TabIndex = 73;
this.labStopBit.Text = "Stop Bit"; this.labStopBit.Text = "Stop Bit";
this.labStopBit.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labStopBit.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -130,9 +136,10 @@ ...@@ -130,9 +136,10 @@
// labParity // labParity
// //
this.labParity.Font = new System.Drawing.Font("Arial", 8F); this.labParity.Font = new System.Drawing.Font("Arial", 8F);
this.labParity.Location = new System.Drawing.Point(235, 63); this.labParity.Location = new System.Drawing.Point(313, 79);
this.labParity.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labParity.Name = "labParity"; this.labParity.Name = "labParity";
this.labParity.Size = new System.Drawing.Size(98, 15); this.labParity.Size = new System.Drawing.Size(131, 19);
this.labParity.TabIndex = 74; this.labParity.TabIndex = 74;
this.labParity.Text = "Parity"; this.labParity.Text = "Parity";
this.labParity.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labParity.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -140,9 +147,10 @@ ...@@ -140,9 +147,10 @@
// labBaud // labBaud
// //
this.labBaud.Font = new System.Drawing.Font("Arial", 8F); this.labBaud.Font = new System.Drawing.Font("Arial", 8F);
this.labBaud.Location = new System.Drawing.Point(93, 63); this.labBaud.Location = new System.Drawing.Point(124, 79);
this.labBaud.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labBaud.Name = "labBaud"; this.labBaud.Name = "labBaud";
this.labBaud.Size = new System.Drawing.Size(75, 15); this.labBaud.Size = new System.Drawing.Size(100, 19);
this.labBaud.TabIndex = 75; this.labBaud.TabIndex = 75;
this.labBaud.Text = "Baudrate"; this.labBaud.Text = "Baudrate";
this.labBaud.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labBaud.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -150,9 +158,10 @@ ...@@ -150,9 +158,10 @@
// labDataBit // labDataBit
// //
this.labDataBit.Font = new System.Drawing.Font("Arial", 8F); this.labDataBit.Font = new System.Drawing.Font("Arial", 8F);
this.labDataBit.Location = new System.Drawing.Point(177, 63); this.labDataBit.Location = new System.Drawing.Point(236, 79);
this.labDataBit.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labDataBit.Name = "labDataBit"; this.labDataBit.Name = "labDataBit";
this.labDataBit.Size = new System.Drawing.Size(48, 15); this.labDataBit.Size = new System.Drawing.Size(64, 19);
this.labDataBit.TabIndex = 76; this.labDataBit.TabIndex = 76;
this.labDataBit.Text = "Data Bit"; this.labDataBit.Text = "Data Bit";
this.labDataBit.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labDataBit.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -169,17 +178,19 @@ ...@@ -169,17 +178,19 @@
"38400", "38400",
"57600", "57600",
"115200"}); "115200"});
this.cmbBaud.Location = new System.Drawing.Point(92, 78); this.cmbBaud.Location = new System.Drawing.Point(123, 98);
this.cmbBaud.Margin = new System.Windows.Forms.Padding(4);
this.cmbBaud.Name = "cmbBaud"; this.cmbBaud.Name = "cmbBaud";
this.cmbBaud.Size = new System.Drawing.Size(76, 22); this.cmbBaud.Size = new System.Drawing.Size(100, 24);
this.cmbBaud.TabIndex = 67; this.cmbBaud.TabIndex = 67;
// //
// labPort // labPort
// //
this.labPort.Font = new System.Drawing.Font("Arial", 8F); this.labPort.Font = new System.Drawing.Font("Arial", 8F);
this.labPort.Location = new System.Drawing.Point(12, 63); this.labPort.Location = new System.Drawing.Point(16, 79);
this.labPort.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labPort.Name = "labPort"; this.labPort.Name = "labPort";
this.labPort.Size = new System.Drawing.Size(75, 15); this.labPort.Size = new System.Drawing.Size(100, 19);
this.labPort.TabIndex = 66; this.labPort.TabIndex = 66;
this.labPort.Text = "COM Port"; this.labPort.Text = "COM Port";
this.labPort.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labPort.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -187,33 +198,39 @@ ...@@ -187,33 +198,39 @@
// cmbPort // cmbPort
// //
this.cmbPort.Font = new System.Drawing.Font("Arial", 8F); this.cmbPort.Font = new System.Drawing.Font("Arial", 8F);
this.cmbPort.Location = new System.Drawing.Point(12, 78); this.cmbPort.Location = new System.Drawing.Point(16, 98);
this.cmbPort.Margin = new System.Windows.Forms.Padding(4);
this.cmbPort.Name = "cmbPort"; this.cmbPort.Name = "cmbPort";
this.cmbPort.Size = new System.Drawing.Size(75, 22); this.cmbPort.Size = new System.Drawing.Size(99, 24);
this.cmbPort.TabIndex = 65; this.cmbPort.TabIndex = 65;
// //
// label7 // label7
// //
this.label7.Font = new System.Drawing.Font("Arial", 8F); this.label7.Font = new System.Drawing.Font("Arial", 8F);
this.label7.ForeColor = System.Drawing.Color.Blue; this.label7.ForeColor = System.Drawing.Color.Blue;
this.label7.Location = new System.Drawing.Point(12, 46); this.label7.Location = new System.Drawing.Point(16, 58);
this.label7.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label7.Name = "label7"; this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(140, 17); this.label7.Size = new System.Drawing.Size(187, 21);
this.label7.TabIndex = 64; this.label7.TabIndex = 64;
this.label7.Text = "Connection Status"; this.label7.Text = "Connection Status";
// //
// txtReceiveMessage // txtReceiveMessage
// //
this.txtReceiveMessage.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.txtReceiveMessage.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.txtReceiveMessage.Location = new System.Drawing.Point(422, 11); this.txtReceiveMessage.Location = new System.Drawing.Point(563, 14);
this.txtReceiveMessage.Margin = new System.Windows.Forms.Padding(2); this.txtReceiveMessage.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.txtReceiveMessage.Multiline = true; this.txtReceiveMessage.Multiline = true;
this.txtReceiveMessage.Name = "txtReceiveMessage"; this.txtReceiveMessage.Name = "txtReceiveMessage";
this.txtReceiveMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtReceiveMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtReceiveMessage.Size = new System.Drawing.Size(451, 513); this.txtReceiveMessage.Size = new System.Drawing.Size(600, 640);
this.txtReceiveMessage.TabIndex = 77; this.txtReceiveMessage.TabIndex = 77;
this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged); this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged);
// //
// modbus_Timer
//
this.modbus_Timer.Tick += new System.EventHandler(this.Modbus_Timer_Tick);
//
// updateops // updateops
// //
this.updateops.Controls.Add(this.imagesname); this.updateops.Controls.Add(this.imagesname);
...@@ -222,35 +239,40 @@ ...@@ -222,35 +239,40 @@
this.updateops.Controls.Add(this.label13); this.updateops.Controls.Add(this.label13);
this.updateops.Controls.Add(this.UpdateBar); this.updateops.Controls.Add(this.UpdateBar);
this.updateops.Controls.Add(this.UpdateBtn); this.updateops.Controls.Add(this.UpdateBtn);
this.updateops.Location = new System.Drawing.Point(15, 174); this.updateops.Location = new System.Drawing.Point(20, 218);
this.updateops.Margin = new System.Windows.Forms.Padding(4);
this.updateops.Name = "updateops"; this.updateops.Name = "updateops";
this.updateops.Size = new System.Drawing.Size(387, 111); this.updateops.Padding = new System.Windows.Forms.Padding(4);
this.updateops.Size = new System.Drawing.Size(516, 139);
this.updateops.TabIndex = 92; this.updateops.TabIndex = 92;
this.updateops.TabStop = false; this.updateops.TabStop = false;
this.updateops.Text = "更新操作"; this.updateops.Text = "更新操作";
// //
// imagesname // imagesname
// //
this.imagesname.Location = new System.Drawing.Point(56, 70); this.imagesname.Location = new System.Drawing.Point(75, 88);
this.imagesname.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.imagesname.Name = "imagesname"; this.imagesname.Name = "imagesname";
this.imagesname.Size = new System.Drawing.Size(108, 12); this.imagesname.Size = new System.Drawing.Size(144, 15);
this.imagesname.TabIndex = 5; this.imagesname.TabIndex = 5;
this.imagesname.Text = "wtd.bin"; this.imagesname.Text = "wtd.bin";
// //
// label12 // label12
// //
this.label12.AutoSize = true; this.label12.AutoSize = true;
this.label12.Location = new System.Drawing.Point(16, 70); this.label12.Location = new System.Drawing.Point(21, 88);
this.label12.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label12.Name = "label12"; this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(41, 12); this.label12.Size = new System.Drawing.Size(52, 15);
this.label12.TabIndex = 4; this.label12.TabIndex = 4;
this.label12.Text = "镜像:"; this.label12.Text = "镜像:";
// //
// choosebth // choosebth
// //
this.choosebth.Location = new System.Drawing.Point(182, 65); this.choosebth.Location = new System.Drawing.Point(243, 81);
this.choosebth.Margin = new System.Windows.Forms.Padding(4);
this.choosebth.Name = "choosebth"; this.choosebth.Name = "choosebth";
this.choosebth.Size = new System.Drawing.Size(75, 23); this.choosebth.Size = new System.Drawing.Size(100, 29);
this.choosebth.TabIndex = 3; this.choosebth.TabIndex = 3;
this.choosebth.Text = "选择bin"; this.choosebth.Text = "选择bin";
this.choosebth.UseVisualStyleBackColor = true; this.choosebth.UseVisualStyleBackColor = true;
...@@ -259,24 +281,27 @@ ...@@ -259,24 +281,27 @@
// label13 // label13
// //
this.label13.AutoSize = true; this.label13.AutoSize = true;
this.label13.Location = new System.Drawing.Point(16, 26); this.label13.Location = new System.Drawing.Point(21, 32);
this.label13.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label13.Name = "label13"; this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(41, 12); this.label13.Size = new System.Drawing.Size(52, 15);
this.label13.TabIndex = 2; this.label13.TabIndex = 2;
this.label13.Text = "进度:"; this.label13.Text = "进度:";
// //
// UpdateBar // UpdateBar
// //
this.UpdateBar.Location = new System.Drawing.Point(58, 20); this.UpdateBar.Location = new System.Drawing.Point(77, 25);
this.UpdateBar.Margin = new System.Windows.Forms.Padding(4);
this.UpdateBar.Name = "UpdateBar"; this.UpdateBar.Name = "UpdateBar";
this.UpdateBar.Size = new System.Drawing.Size(280, 23); this.UpdateBar.Size = new System.Drawing.Size(373, 29);
this.UpdateBar.TabIndex = 1; this.UpdateBar.TabIndex = 1;
// //
// UpdateBtn // UpdateBtn
// //
this.UpdateBtn.Location = new System.Drawing.Point(263, 65); this.UpdateBtn.Location = new System.Drawing.Point(351, 81);
this.UpdateBtn.Margin = new System.Windows.Forms.Padding(4);
this.UpdateBtn.Name = "UpdateBtn"; this.UpdateBtn.Name = "UpdateBtn";
this.UpdateBtn.Size = new System.Drawing.Size(75, 23); this.UpdateBtn.Size = new System.Drawing.Size(100, 29);
this.UpdateBtn.TabIndex = 0; this.UpdateBtn.TabIndex = 0;
this.UpdateBtn.Text = "一键更新"; this.UpdateBtn.Text = "一键更新";
this.UpdateBtn.UseVisualStyleBackColor = true; this.UpdateBtn.UseVisualStyleBackColor = true;
...@@ -291,9 +316,10 @@ ...@@ -291,9 +316,10 @@
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(33, 357); this.label1.Location = new System.Drawing.Point(44, 446);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16); this.label1.Size = new System.Drawing.Size(69, 20);
this.label1.TabIndex = 93; this.label1.TabIndex = 93;
this.label1.Text = "温度:"; this.label1.Text = "温度:";
// //
...@@ -301,35 +327,39 @@ ...@@ -301,35 +327,39 @@
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 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(33, 394); this.label2.Location = new System.Drawing.Point(44, 492);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16); this.label2.Size = new System.Drawing.Size(69, 20);
this.label2.TabIndex = 94; this.label2.TabIndex = 94;
this.label2.Text = "水位:"; this.label2.Text = "水位:";
// //
// label3 // label3
// //
this.label3.AutoSize = true; this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(81, 361); this.label3.Location = new System.Drawing.Point(108, 451);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3"; this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 12); this.label3.Size = new System.Drawing.Size(55, 15);
this.label3.TabIndex = 95; this.label3.TabIndex = 95;
this.label3.Text = "label3"; this.label3.Text = "label3";
// //
// label4 // label4
// //
this.label4.AutoSize = true; this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(81, 398); this.label4.Location = new System.Drawing.Point(108, 498);
this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label4.Name = "label4"; this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(41, 12); this.label4.Size = new System.Drawing.Size(55, 15);
this.label4.TabIndex = 96; this.label4.TabIndex = 96;
this.label4.Text = "label4"; this.label4.Text = "label4";
// //
// button2 // button2
// //
this.button2.Location = new System.Drawing.Point(278, 381); this.button2.Location = new System.Drawing.Point(371, 476);
this.button2.Margin = new System.Windows.Forms.Padding(4);
this.button2.Name = "button2"; this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23); this.button2.Size = new System.Drawing.Size(100, 29);
this.button2.TabIndex = 97; this.button2.TabIndex = 97;
this.button2.Text = "test"; this.button2.Text = "test";
this.button2.UseVisualStyleBackColor = true; this.button2.UseVisualStyleBackColor = true;
...@@ -337,9 +367,10 @@ ...@@ -337,9 +367,10 @@
// //
// SubscribeBut // SubscribeBut
// //
this.SubscribeBut.Location = new System.Drawing.Point(278, 346); this.SubscribeBut.Location = new System.Drawing.Point(371, 432);
this.SubscribeBut.Margin = new System.Windows.Forms.Padding(4);
this.SubscribeBut.Name = "SubscribeBut"; this.SubscribeBut.Name = "SubscribeBut";
this.SubscribeBut.Size = new System.Drawing.Size(75, 23); this.SubscribeBut.Size = new System.Drawing.Size(100, 29);
this.SubscribeBut.TabIndex = 98; this.SubscribeBut.TabIndex = 98;
this.SubscribeBut.Text = "订阅消息"; this.SubscribeBut.Text = "订阅消息";
this.SubscribeBut.UseVisualStyleBackColor = true; this.SubscribeBut.UseVisualStyleBackColor = true;
...@@ -347,9 +378,9 @@ ...@@ -347,9 +378,9 @@
// //
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(888, 538); this.ClientSize = new System.Drawing.Size(1184, 672);
this.Controls.Add(this.SubscribeBut); this.Controls.Add(this.SubscribeBut);
this.Controls.Add(this.button2); this.Controls.Add(this.button2);
this.Controls.Add(this.label4); this.Controls.Add(this.label4);
...@@ -372,6 +403,7 @@ ...@@ -372,6 +403,7 @@
this.Controls.Add(this.cmbPort); this.Controls.Add(this.cmbPort);
this.Controls.Add(this.label7); this.Controls.Add(this.label7);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1"; this.Name = "Form1";
this.Text = "牧场项目"; this.Text = "牧场项目";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
......
...@@ -26,11 +26,10 @@ namespace ModbusDemo ...@@ -26,11 +26,10 @@ namespace ModbusDemo
public partial class Form1 : Form public partial class Form1 : Form
{ {
bool Debug_test = true;//打开测试功能 bool Debug_test = true;//打开测试功能
_414P rtd = new _414P(); _414P rtd = new _414P();
_478C state = new _478C(); _478C state = new _478C();
_418X Analog = new _418X(); _418X Analog = new _418X();
Pond FunctionalService = new Pond(); //Pond FunctionalService = new Pond();
private bool bStart = false; private bool bStart = false;
FileStream fsl; FileStream fsl;
AutoResetEvent exitEvent; AutoResetEvent exitEvent;
...@@ -43,7 +42,7 @@ namespace ModbusDemo ...@@ -43,7 +42,7 @@ namespace ModbusDemo
bool m_Isbgw0_CanContinueRun = true; bool m_Isbgw0_CanContinueRun = true;
bool State_enable = true; bool State_enable = true;
ModbusDemo.windows.Totxt totxt = new windows.Totxt(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); public static ModbusDemo.windows.Totxt totxt = new windows.Totxt(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
public Form1() public Form1()
{ {
...@@ -83,11 +82,7 @@ namespace ModbusDemo ...@@ -83,11 +82,7 @@ namespace ModbusDemo
Parity parity; Parity parity;
StopBits stopBits; StopBits stopBits;
private SerialPort comPort = new SerialPort(); private SerialPort comPort = new SerialPort();
ModbusMaster master; public static ModbusMaster master;
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
string mqttIp; string mqttIp;
string mqttClientId; string mqttClientId;
...@@ -158,10 +153,10 @@ namespace ModbusDemo ...@@ -158,10 +153,10 @@ namespace ModbusDemo
/// 获取时间戳 /// 获取时间戳
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static string GetTimeStamp() public static UInt64 GetTimeStamp()
{ {
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString(); return Convert.ToUInt64(ts.TotalSeconds);
} }
#endregion #endregion
...@@ -450,7 +445,7 @@ namespace ModbusDemo ...@@ -450,7 +445,7 @@ namespace ModbusDemo
ssses[0].op.Add("duration", ctrl.ss[0].op.getDuration()); ssses[0].op.Add("duration", ctrl.ss[0].op.getDuration());
ssses[0].op.Add("startTime", ctrl.ss[0].op.getStartTime()); ssses[0].op.Add("startTime", ctrl.ss[0].op.getStartTime());
ssses[0].op.Add(passageway, readyState); ssses[0].op.Add(passageway, readyState);
ssses[0].op.Add("ts",long.Parse(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 ||
...@@ -616,7 +611,7 @@ namespace ModbusDemo ...@@ -616,7 +611,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 = null; OutputValue = new ushort[numberOfPoints];
Caution = -1; Caution = -1;
} }
//The server return error code. //The server return error code.
...@@ -674,7 +669,7 @@ namespace ModbusDemo ...@@ -674,7 +669,7 @@ namespace ModbusDemo
break; break;
} }
} }
OutputValue = null; OutputValue = new ushort[numberOfPoints];
Caution = -1; Caution = -1;
} }
} }
...@@ -695,7 +690,7 @@ namespace ModbusDemo ...@@ -695,7 +690,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 = null; OutputValue = new bool [numberOfPoints];
Caution = -1; Caution = -1;
} }
//The server return error code. //The server return error code.
...@@ -753,7 +748,7 @@ namespace ModbusDemo ...@@ -753,7 +748,7 @@ namespace ModbusDemo
break; break;
} }
} }
OutputValue = null; OutputValue = new bool[numberOfPoints];
Caution = -1; Caution = -1;
} }
} }
...@@ -837,11 +832,12 @@ namespace ModbusDemo ...@@ -837,11 +832,12 @@ namespace ModbusDemo
Caution = -1; Caution = -1;
} }
} }
private 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;
try try
{ {
ushort[] temperature = { }; ushort[] temperature = { };
...@@ -859,42 +855,45 @@ namespace ModbusDemo ...@@ -859,42 +855,45 @@ namespace ModbusDemo
int[] EOnOff = { 0 }; int[] EOnOff = { 0 };
int[] SOnOff = { 0 }; int[] SOnOff = { 0 };
int[] WOnOff = { 0 }; int[] WOnOff = { 0 };
Modbus_ReadHoldingRegistersTask(out temperature, 1, 0, 4, out int signT); Modbus_ReadHoldingRegistersTask(out temperature, 1, 0, 4, out int signT);
Modbus_ReadHoldingRegistersTask(out type, 1, 10, 4, out int sign); Modbus_ReadHoldingRegistersTask(out type, 1, 10, 4, out sign);
string time_1 = GetTimeStamp(); UInt64 time_1 = GetTimeStamp();
Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr); Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr);
Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI); Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI);
string time_2 = GetTimeStamp(); UInt64 time_2 = GetTimeStamp();
Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre); Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre);
Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn); Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn);
string time_3 = GetTimeStamp(); UInt64 time_3 = GetTimeStamp();
Modbus_ReadCoilsTask(out EIO, 5, 16, 6, out int signE); Modbus_ReadCoilsTask(out EIO, 5, 16, 6, out int signE);
Modbus_ReadCoilsTask(out SIO, 8, 16, 6, out int signS); Modbus_ReadCoilsTask(out SIO, 8, 16, 6, out int signS);
Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW); Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW);
if (sign == -1 || signT ==-1|| signW ==-1 || signr == -1 || signI == -1 || signre == -1 || signIn == -1 || signE == -1 || signS == -1)
{
vs = null; //if (sign == -1 || signT ==-1|| signW ==-1 || signr == -1 || signI == -1 || signre == -1 || signIn == -1 || signE == -1 || signS == -1)
type = null; //{
EastTemperature = 0; // vs = null;
Easternwaterlevel = 0; // type = null;
WestWaterLevel = 0; // EastTemperature = 0;
} // Easternwaterlevel = 0;
else // WestWaterLevel = 0;
{ //}
vs = rtd.RtdValue(temperature, type); //else
EastTemperature = float.Parse(vs[0]); //{
s = Analog.Value(register, Inputtype); vs = rtd.RtdValue(temperature, type);
Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6; EastTemperature = float.Parse(vs[0]);
s6 = Analog.Value(registerN, InputtypeN); s = Analog.Value(register, Inputtype);
WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6; Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6;
label3.Text = EastTemperature.ToString(); s6 = Analog.Value(registerN, InputtypeN);
label4.Text = Easternwaterlevel.ToString(); WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6;
EOnOff = state.IO(EIO);////东中转池 label3.Text = EastTemperature.ToString();
SOnOff = state.IO(SIO);////西中转池 label4.Text = Easternwaterlevel.ToString();
WOnOff = state.IO(WIO); ////东南北水 EOnOff = state.IO(EIO);////东中转池
} SOnOff = state.IO(SIO);////西中转池
WOnOff = state.IO(WIO); ////东南北水
//}
...@@ -1000,6 +999,77 @@ namespace ModbusDemo ...@@ -1000,6 +999,77 @@ namespace ModbusDemo
return; return;
} }
} }
finally
{
if (sign == -1)
{
pcDeviceAlarm pcDeviceAlarmdata = new pcDeviceAlarm();
ushort bAddress = 8;
ushort[] bChannel = new ushort[pcDeviceAlarm.usChannelNumber];
pcDeviceAlarm.eAlarmCode[] eAlarmCode = new pcDeviceAlarm.eAlarmCode[pcDeviceAlarm.usChannelNumber];
for (ushort i = 0; i < pcDeviceAlarm.usChannelNumber; i++)
{
eAlarmCode[i] = pcDeviceAlarm.eAlarmCode.ChannelCommunError;
}
pcDeviceAlarmdata.vSetAlarmList(bAddress, bChannel, eAlarmCode);
await pcDeviceAlarmdata.vPublishAlarmPackageJson(mqttClient);
}
if (sign == -1)
{
pcDeviceAlarm pcDeviceAlarmdata = new pcDeviceAlarm();
ushort bAddress = 8;
ushort[] bChannel = new ushort[pcDeviceAlarm.usChannelNumber];
pcDeviceAlarm.eAlarmCode[] eAlarmCode = new pcDeviceAlarm.eAlarmCode[pcDeviceAlarm.usChannelNumber];
for (ushort i = 0; i < pcDeviceAlarm.usChannelNumber; i++)
{
eAlarmCode[i] = pcDeviceAlarm.eAlarmCode.ChannelCollectDataError;
}
pcDeviceAlarmdata.vSetAlarmList(bAddress, bChannel, eAlarmCode);
await pcDeviceAlarmdata.vPublishAlarmPackageJson(mqttClient);
}
if (sign == -1)
{
pcDeviceAlarm pcDeviceAlarmdata = new pcDeviceAlarm();
ushort bAddress = 8;
ushort[] bChannel = new ushort[pcDeviceAlarm.usChannelNumber];
pcDeviceAlarm.eAlarmCode[] eAlarmCode = new pcDeviceAlarm.eAlarmCode[pcDeviceAlarm.usChannelNumber];
for (ushort i = 0; i < pcDeviceAlarm.usChannelNumber; i++)
{
eAlarmCode[i] = pcDeviceAlarm.eAlarmCode.ChannelDataRespError;
}
pcDeviceAlarmdata.vSetAlarmList(bAddress, bChannel, eAlarmCode);
await pcDeviceAlarmdata.vPublishAlarmPackageJson(mqttClient);
}
if (sign == -1)
{
pcDeviceAlarm pcDeviceAlarmdata = new pcDeviceAlarm();
ushort bAddress = 8;
ushort[] bChannel = new ushort[pcDeviceAlarm.usChannelNumber];
pcDeviceAlarm.eAlarmCode[] eAlarmCode = new pcDeviceAlarm.eAlarmCode[pcDeviceAlarm.usChannelNumber];
for (ushort i = 0; i < pcDeviceAlarm.usChannelNumber; i++)
{
eAlarmCode[i] = pcDeviceAlarm.eAlarmCode.ChannelDataChangeError;
}
pcDeviceAlarmdata.vSetAlarmList(bAddress, bChannel, eAlarmCode);
await pcDeviceAlarmdata.vPublishAlarmPackageJson(mqttClient);
}
if (sign == -1)
{
pcDeviceAlarm pcDeviceAlarmdata = new pcDeviceAlarm();
ushort bAddress = 8;
ushort[] bChannel = new ushort[pcDeviceAlarm.usChannelNumber];
pcDeviceAlarm.eAlarmCode[] eAlarmCode = new pcDeviceAlarm.eAlarmCode[pcDeviceAlarm.usChannelNumber];
for (ushort i = 0; i < pcDeviceAlarm.usChannelNumber; i++)
{
eAlarmCode[i] = pcDeviceAlarm.eAlarmCode.ChannelNoError;
}
pcDeviceAlarmdata.vSetAlarmList(bAddress, bChannel, eAlarmCode);
await pcDeviceAlarmdata.vPublishAlarmPackageJson(mqttClient);
}
}
return; return;
} }
#endregion #endregion
...@@ -1200,6 +1270,7 @@ namespace ModbusDemo ...@@ -1200,6 +1270,7 @@ namespace ModbusDemo
return isconn; return isconn;
} }
#endregion #endregion
#region 更新线程 #region 更新线程
...@@ -1561,16 +1632,16 @@ namespace ModbusDemo ...@@ -1561,16 +1632,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("串口打开错误");
...@@ -1599,7 +1670,7 @@ namespace ModbusDemo ...@@ -1599,7 +1670,7 @@ namespace ModbusDemo
{ {
comPort.StopBits = StopBits.One; comPort.StopBits = StopBits.One;
} }
} //}
try try
{ {
comPort.Open(); comPort.Open();
...@@ -1613,10 +1684,10 @@ namespace ModbusDemo ...@@ -1613,10 +1684,10 @@ namespace ModbusDemo
MQTT_thread.Start(); MQTT_thread.Start();
Task.Run(async () => { await ConnectMqttServerAsync(); }); Task.Run(async () => { await ConnectMqttServerAsync(); });
master = ModbusSerialMaster.CreateRtu(comPort); master = ModbusSerialMaster.CreateRtu(comPort);
master.Transport.Retries = 2; //重试次数 master.Transport.Retries = 0; //重试次数
master.Transport.ReadTimeout = 1500; //读取串口数据超时时间(ms) master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms)
master.Transport.WriteTimeout = 1500;//写入串口数据超时时间(ms) master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms)
master.Transport.WaitToRetryMilliseconds = 100;//重试间隔(ms) master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms)
modbus_Timer.Enabled = true; modbus_Timer.Enabled = true;
btOpenCOM.Enabled = false; btOpenCOM.Enabled = false;
btCloseCOM.Enabled = true; btCloseCOM.Enabled = true;
...@@ -1642,6 +1713,11 @@ namespace ModbusDemo ...@@ -1642,6 +1713,11 @@ namespace ModbusDemo
comPort.Close(); comPort.Close();
totxt.Log(DateTime.Now.ToString() + " =>Disconnect " + comPort.PortName); totxt.Log(DateTime.Now.ToString() + " =>Disconnect " + comPort.PortName);
} }
private void Modbus_Timer_Tick(object sender, EventArgs e)
{
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment