Commit ae9d62a8 authored by leon.huang's avatar leon.huang

modify mqtt control

parent 7728813d
......@@ -12,7 +12,7 @@ namespace ModbusDemo.Common
public static class ModualAlarm
{
public const ushort usChannelNumber = 8;
private const string sAlarmTopic = "Witium/WTDS78X/20193261/Alarm";
private const string sAlarmTopic = "Witium/WTDS78X/66666666/Alarm";
public static string sAlarmJson;
public static string sAlarmJsonPackage;
//private IMqttClient mqttClient = null;
......@@ -24,7 +24,7 @@ namespace ModbusDemo.Common
ChannelDataRespError = 4,
ChannelDataChangeError = 8
}
public static void vSetAlarmList(ushort usAddress, ushort[] usChannel, ushort[] usChannelAlarmcode,bool bStringIncrease)
public static void vSetAlarmList(ushort usAddress, ushort[] usChannel, ushort[] usChannelAlarmcode)
{
ushort i = 0;
Hashtable hMqtttAlarmMap = new Hashtable();
......@@ -36,11 +36,12 @@ namespace ModbusDemo.Common
i++;
}
hMqtttAlarmMap.Add("ts", Form1.GetTimeStamp());
sAlarmJson += JsonConvert.SerializeObject(hMqtttAlarmMap);
if (bStringIncrease == true)
if (sAlarmJson != "[")
{
sAlarmJson += ",";
}
sAlarmJson += JsonConvert.SerializeObject(hMqtttAlarmMap);
}
public static async Task vPublishAlarmPackageJson(IMqttClient mqttClient)
{
......
......@@ -13,6 +13,7 @@ namespace ModbusDemo.windows
private string logFile;
private StreamWriter writer;
private FileStream fileStream = null;
private static object oTxtLock = new object();
public Totxt(string fileName)
{
logFile = fileName;
......@@ -21,33 +22,35 @@ namespace ModbusDemo.windows
public void Log(string info)
{
try
lock (oTxtLock)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile);
if (!fileInfo.Exists)
try
{
fileStream = fileInfo.Create();
writer = new StreamWriter(fileStream);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile);
if (!fileInfo.Exists)
{
fileStream = fileInfo.Create();
writer = new StreamWriter(fileStream);
}
else
{
fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write);
writer = new StreamWriter(fileStream);
}
writer.WriteLine(DateTime.Now + ": " + info);
}
else
finally
{
fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write);
writer = new StreamWriter(fileStream);
if (writer != null)
{
writer.Close();
writer.Dispose();
fileStream.Close();
fileStream.Dispose();
}
}
writer.WriteLine(DateTime.Now + ": " + info);
}
finally
{
if (writer != null)
{
writer.Close();
writer.Dispose();
fileStream.Close();
fileStream.Dispose();
}
}
}
public void CreateDirectory(string infoPath)
{
DirectoryInfo directoryInfo = Directory.GetParent(infoPath);
......
......@@ -13,7 +13,7 @@ namespace ModbusDemo.MessageFormat
{
public static class ModbusComm
{
public enum eModbusErrorCode : byte
public enum eModbusErrorCode : byte
{
MB_ENOERR, /*!< no error. */
MB_ENOREG, /*!< illegal register address. */
......@@ -28,423 +28,343 @@ namespace ModbusDemo.MessageFormat
MB_EMASTERSENT,
MB_RTUERNOERR
}
public static ModbusMaster Modbus;
public static ModbusMaster Modbus;
public static ushort usCommunFailTimes = 0;
public static eModbusErrorCode Modbus_ReadHoldingRegistersTask(out ushort[] OutputValue, byte slaveAddress, ushort startAddress, ushort numberOfPoints)
{
eModbusErrorCode ModbusErrorCode;
try
object oLock = new object();
lock (oLock)
{
OutputValue = Modbus.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
ModbusErrorCode = eModbusErrorCode.MB_EMASTERNOERR;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
eModbusErrorCode ModbusErrorCode;
try
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_ReadHoldingRegistersTask");
OutputValue = new ushort[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++;
OutputValue = Modbus.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
ModbusErrorCode = eModbusErrorCode.MB_EMASTERNOERR;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
catch (Exception exception)
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
//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 + "Modbus_ReadHoldingRegistersTask");
OutputValue = new ushort[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++;
}
//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);
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;
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];
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
}
OutputValue = new ushort[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
return ModbusErrorCode;
}
return ModbusErrorCode;
}
public static eModbusErrorCode Modbus_ReadCoilsTask(out bool[] OutputValue, byte slaveAddress, ushort startAddress, ushort numberOfPoints)
{
eModbusErrorCode ModbusErrorCode;
try
{
OutputValue = Modbus.ReadCoils(slaveAddress, startAddress, numberOfPoints);
ModbusErrorCode = eModbusErrorCode.MB_EMASTERNOERR;
}
catch (Exception exception)
object oLock = new object();
lock (oLock)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
eModbusErrorCode ModbusErrorCode;
try
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_ReadCoilsTask");
OutputValue = new bool[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++;
OutputValue = Modbus.ReadCoils(slaveAddress, startAddress, numberOfPoints);
ModbusErrorCode = eModbusErrorCode.MB_EMASTERNOERR;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
catch (Exception exception)
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
//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 + "Modbus_ReadCoilsTask");
OutputValue = new bool[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++;
}
//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);
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;
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];
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
}
OutputValue = new bool[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
return ModbusErrorCode;
}
return ModbusErrorCode;
}
public static eModbusErrorCode Modbus_WriteSingleCoil(byte slaveAddress, ushort startAddress, bool[] value)
{
eModbusErrorCode ModbusErrorCode;
try
{
Modbus.WriteMultipleCoils(slaveAddress, startAddress, value);
ModbusErrorCode = eModbusErrorCode.MB_ENOERR;
}
catch (Exception exception)
object oLock = new object();
lock (oLock)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
eModbusErrorCode ModbusErrorCode;
try
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_WriteSingleCoil");
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
Modbus.WriteMultipleCoils(slaveAddress, startAddress, value);
ModbusErrorCode = eModbusErrorCode.MB_ENOERR;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
catch (Exception exception)
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
//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 + "Modbus_WriteSingleCoil");
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++;
}
//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);
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;
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;
}
}
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
}
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
return ModbusErrorCode;
}
return ModbusErrorCode;
}
public static eModbusErrorCode Modbus_WriteMultipleCoils(byte slaveAddress, ushort startAddress, bool[] data)
{
eModbusErrorCode ModbusErrorCode;
try
{
Modbus.WriteMultipleCoils(slaveAddress, startAddress, data);
ModbusErrorCode = eModbusErrorCode.MB_ENOERR;
}
catch (Exception exception)
object oLock = new object();
lock (oLock)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
eModbusErrorCode ModbusErrorCode;
try
{
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_WriteMultipleCoils");
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
Modbus.WriteMultipleCoils(slaveAddress, startAddress, data);
ModbusErrorCode = eModbusErrorCode.MB_ENOERR;
}
//The server return error code.
//You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC"))
catch (Exception exception)
{
string str = exception.Message;
int FunctionCode;
string ExceptionCode;
//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 + "Modbus_WriteMultipleCoils");
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++;
}
//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);
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;
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;
}
}
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
}
ModbusErrorCode = eModbusErrorCode.MB_EMASTERERR;
return ModbusErrorCode;
}
return ModbusErrorCode;
}
//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)
// + "]";
//}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Client;
using Newtonsoft.Json;
namespace ModbusDemo.MessageFormat
{
public class ModualControl
{
public class Op
{
public UInt64 duration { get; set; }
public UInt64 startTime { get; set;}
public int d0 { get; set; }
public int d1 { get; set; }
public int d2 { get; set; }
public int d3 { get; set; }
public int d4 { get; set; }
public int d5 { get; set; }
public int d6 { get; set; }
public int d7 { get; set; }
public UInt64 ts { get; set; }
}
public class SsItem
{
public string addr { get; set; }
public Op op { get; set; }
}
public class MqttRoot
{
public string taskId { get; set; }
public List<SsItem> ss { get; set; }
}
public static List<MqttRoot> liModuualControl = new List<MqttRoot>();
public static void vGetaWay_Receive_Control(string sDataString)
{
MqttRoot cControl = JsonConvert.DeserializeObject<MqttRoot>(sDataString);
}
}
}
......@@ -132,7 +132,6 @@ namespace ModbusDemo
}
public class Ctrl
{
public string taskId;
public void settaskId(string taskId)
{
......
......@@ -2,6 +2,20 @@
<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>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup>
<SSDTPath Condition="'$(SSDTPath)' == ''">$(VsInstallRoot)\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\130</SSDTPath>
......@@ -22,20 +36,6 @@
<FileAlignment>512</FileAlignment>
<IsWebBootstrapper>false</IsWebBootstrapper>
<TargetFrameworkProfile />
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
......@@ -43,7 +43,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;DEBUG_TEST</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
......@@ -78,7 +78,6 @@
<HintPath>..\packages\NModbus4.2.1.0\lib\net40\NModbus4.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
......@@ -94,6 +93,7 @@
<Compile Include="application\Pond.cs" />
<Compile Include="application\UsThread.cs" />
<Compile Include="Common\ModualAlarm.cs" />
<Compile Include="MessageFormat\ModualControl.cs" />
<Compile Include="MessageFormat\ModbusComm.cs" />
<Compile Include="Modular\414P.cs" />
<Compile Include="Modular\478C.cs" />
......@@ -168,10 +168,10 @@
<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> -->
<!-- </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)' != ''" />
......
......@@ -9,64 +9,77 @@ using System.Collections;
using MQTTnet;
using MQTTnet.Client;
using Newtonsoft.Json;
using ModbusDemo;
namespace ModbusDemo.Modular
{
class ModualPoll
class ModualPoll
{
public const byte byX14pNumber = 2;
private const byte byX14pReadDataStartAddress = 0;
public const byte byX14pReadDataNumber = 4;
private const byte byX14pReadTypeStartAddress = 10;
private const byte byX14pReadTypeNumber = 4;
public const double dX14pMinCovertData = 0;
public const double dx14pMaxConverData = 60;
public const ushort us14pMaxJumTemper = 10;
#if DEBUG_TEST
public const byte byX14pNumber = 1;
public static byte[] byX14pIdlist = {27};
private static modual.eX14pSensorChannelType[,] sX14pChannelType = new modual.eX14pSensorChannelType[byX14pNumber, byX14pReadTypeNumber] { { modual.eX14pSensorChannelType.Normal, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null }};
#else
public const byte byX14pNumber = 2;
public static byte[] byX14pIdlist = { 1,17};
private static modual.eX14pSensorChannelType[,] sX14pChannelType = new modual.eX14pSensorChannelType[byX14pNumber, byX14pReadTypeNumber] { { modual.eX14pSensorChannelType.Normal, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null },
{ modual.eX14pSensorChannelType.Normal, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null }};
private static modual.eX14pSensorChannelType[,] sX14pChannelType = new modual.eX14pSensorChannelType[byX14pNumber, byX14pReadTypeNumber] { { modual.eX14pSensorChannelType.Normal, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null }, { modual.eX14pSensorChannelType.Normal, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null, modual.eX14pSensorChannelType.Null }};
#endif
private const byte byX14pReadDataStartAddress = 0;
public const byte byX14pReadDataNumber = 4;
private const byte byX14pReadTypeStartAddress = 10;
private const byte byX14pReadTypeNumber = 4;
public const double dX14pMinCovertData = 0;
public const double dx14pMaxConverData = 60;
public const ushort us14pMaxJumTemper = 10;
public static modual.pcx14P cx14P = new modual.pcx14P(byX14pIdlist, sX14pChannelType);
public static _414P c14formulasormual = new _414P();
public const byte byX18xNumber = 6;
private const byte byX18xReadDataStartAddress = 0;
public const byte byX18xReadDataNumber = 8;
private const byte byX18xReadTypeStartAddress = 10;
private const byte byX18xReadTypeNumber = 8;
public const double dX18xMinConvertData = 0;
private const ushort dX18xMaxJumpData = 2;
public static byte[] byX18xIdlist = {3,53,6,12,21,25};
#if DEBUG_TEST
public const byte byX18xNumber = 1;
public static byte[] byX18xIdlist = {13};
private static modual.eX18xSensorChannelType[,] sX18xChannelType = new modual.eX18xSensorChannelType[byX18xNumber, byX18xReadDataNumber] { { modual.eX18xSensorChannelType.Mike_5, modual.eX18xSensorChannelType.Null, modual.eX18xSensorChannelType.Null, modual.eX18xSensorChannelType.Null, modual.eX18xSensorChannelType.Null, modual.eX18xSensorChannelType.Null, modual.eX18xSensorChannelType.Null, modual.eX18xSensorChannelType.Null }};
#else
public const byte byX18xNumber = 6;
public static byte[] byX18xIdlist = {3,53,6,12,21,25};
private static modual.eX18xSensorChannelType[,] sX18xChannelType = new modual.eX18xSensorChannelType[byX18xNumber, byX18xReadDataNumber]{{modual.eX18xSensorChannelType.Weiertai,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Mike_7,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null}, //东中转池 ID 3
{modual.eX18xSensorChannelType.NormalCurrent,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null}, //东中转池 ID 53
{modual.eX18xSensorChannelType.Mike_7,modual.eX18xSensorChannelType.Weiertai,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null}, //西西中转池 ID 6
{modual.eX18xSensorChannelType.Mike_6,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null}, //八角池 ID 21
{modual.eX18xSensorChannelType.Mike_5,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null}, //沉砂池 ID 12
{modual.eX18xSensorChannelType.Mike_6,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null,modual.eX18xSensorChannelType.Null}}; //匀浆池 ID 25
#endif
private const byte byX18xReadDataStartAddress = 0;
public const byte byX18xReadDataNumber = 8;
private const byte byX18xReadTypeStartAddress = 10;
private const byte byX18xReadTypeNumber = 8;
public const double dX18xMinConvertData = 0;
public const double dX18xMaxConvertData = 80;
private const ushort dX18xMaxJumpData = 2;
public static modual.pcx18X cx18x = new modual.pcx18X(byX18xIdlist, sX18xChannelType);
private static _418X c18xformulasormual = new _418X();
public static modual.pcx18X cx18x = new modual.pcx18X(byX18xIdlist, sX18xChannelType);
private static _418X c18xformulasormual = new _418X();
#if DEBUG_TEST
public const byte byX78cNumber = 1;
public static byte[] byX78cIdlist = {20};
#else
public const byte byX78cNumber = 0;
public static byte[] byX78cIdlist = {11,20,33,34,55,56};
#endif
private const byte byX78cInputRegStartAddress = 0;
public const byte byX78cInputRegNumber = 8;
private const byte byX78cOutputRegStartAddress = 16;
public const byte byX78cOutputRegNumber = 8;
public static byte[] byX78cIdlist = {11,20,33,34,55,56};
public static modual.pcx78C cx78c = new modual.pcx78C(byX78cIdlist);
public static modual.pcx78C cx78c = new modual.pcx78C(byX78cIdlist);
#if DEBUG_TEST
public const byte byX66cNumber = 2;
public static byte[] byX66cIdlist = {16,31};
#else
public const byte byX66cNumber = 32;
public static byte[] byX66cIdlist = {4,5,7,8,9,10,14,15,19,22,23,24,26,30,32,36,37,38,40,41,42,43,44,46,47,48,49,50,51,52,54,57};
#endif
private const byte byX66cRelayRegStartAddress = 16;
public const byte byX66cRelayRegNumber = 6;
public static byte[] byX66cIdlist = {4,5,7,8,9,10,14,15,19,22,23,24,26,30,32,36,37,38,40,41,42,43,44,46,47,48,49,50,51,52,54,57};
public static modual.pcx66C cx66c = new modual.pcx66C(byX66cIdlist);
public static modual.pcx66C cx66c = new modual.pcx66C(byX66cIdlist);
//public static ModualAlarm cModualAlarm = new ModualAlarm();
public static void vAllNodePoll()
{
......@@ -110,17 +123,25 @@ namespace ModbusDemo.Modular
{
cx14P.sX14pDataStruct[i].dNowConvertData[j] = 0;
}
if (cx14P.sX14PAlarmStruct[i].bDataNotFirstIncreaseOrDecrease == true)
if (cx14P.sX14PAlarmStruct[i].bDataNotFirstIncreaseOrDecrease == true && sX14pChannelType[i,j] != modual.eX14pSensorChannelType.Null)
{
if (cx14P.sX14pDataStruct[i].dNowConvertData[j] - cx14P.sX14pDataStruct[i].dBeforeConvertData[j] >= us14pMaxJumTemper && cx14P.sX14pDataStruct[i].dBeforeConvertData[j] - cx14P.sX14pDataStruct[i].dNowConvertData[j] >= us14pMaxJumTemper)
if (cx14P.sX14pDataStruct[i].dNowConvertData[j] - cx14P.sX14pDataStruct[i].dBeforeConvertData[j] >= us14pMaxJumTemper || cx14P.sX14pDataStruct[i].dBeforeConvertData[j] - cx14P.sX14pDataStruct[i].dNowConvertData[j] >= us14pMaxJumTemper)
{
cx14P.sX14PAlarmStruct[i].usNowAlarmCode[j] |= (ushort)ModualAlarm.eAlarmCode.ChannelDataChangeError;
}
else
{
cx14P.sX14PAlarmStruct[i].usNowAlarmCode[j] |= (ushort)~ModualAlarm.eAlarmCode.ChannelDataChangeError;
cx14P.sX14PAlarmStruct[i].usNowAlarmCode[j] &= (ushort)~ModualAlarm.eAlarmCode.ChannelDataChangeError;
}
}
if (cx14P.sX14pDataStruct[i].dNowConvertData[j] >= dx14pMaxConverData && sX14pChannelType[i, j] != modual.eX14pSensorChannelType.Null)
{
cx14P.sX14PAlarmStruct[i].usNowAlarmCode[j] |= (ushort)ModualAlarm.eAlarmCode.ChannelCollectDataError;
}
else
{
cx14P.sX14PAlarmStruct[i].usNowAlarmCode[j] &= (ushort)~ModualAlarm.eAlarmCode.ChannelCollectDataError;
}
cx14P.sX14pDataStruct[i].dBeforeConvertData[j] = cx14P.sX14pDataStruct[i].dNowConvertData[j];
}
cx14P.sX14PAlarmStruct[i].bDataNotFirstIncreaseOrDecrease = true;
......@@ -166,7 +187,7 @@ namespace ModbusDemo.Modular
cx18x.sX18xDataStruct[i].dNowConvertData[j] = 7 - ((double.Parse(sTempString[i][j]) - 4) / 16 * 7);
break;
case modual.eX18xSensorChannelType.NormalCurrent:
cx18x.sX18xDataStruct[i].dNowConvertData[j] = cx18x.sX18xDataStruct[i].dNowConvertData[j];
cx18x.sX18xDataStruct[i].dNowConvertData[j] = double.Parse(sTempString[i][j]);
break;
case modual.eX18xSensorChannelType.Null:
cx18x.sX18xDataStruct[i].dNowConvertData[j] = 0;
......@@ -179,7 +200,7 @@ namespace ModbusDemo.Modular
{
cx18x.sX18xDataStruct[i].dNowConvertData[j] = 0;
}
if (cx18x.sX18xAlarmstruct[i].bDataNotFirstIncreaseOrDecrease == true)
if (cx18x.sX18xAlarmstruct[i].bDataNotFirstIncreaseOrDecrease == true && sX18xChannelType[i,j] != modual.eX18xSensorChannelType.Null)
{
if (cx18x.sX18xDataStruct[i].dNowConvertData[j] - cx18x.sX18xDataStruct[i].dBeforeConvertData[j] >= dX18xMaxJumpData || cx18x.sX18xDataStruct[i].dBeforeConvertData[j] - cx18x.sX18xDataStruct[i].dNowConvertData[j] >= dX18xMaxJumpData)
{
......@@ -190,6 +211,14 @@ namespace ModbusDemo.Modular
cx18x.sX18xAlarmstruct[i].usNowAlarmCode[j] &= (ushort)~ModualAlarm.eAlarmCode.ChannelDataChangeError;
}
}
if ((cx18x.sX18xDataStruct[i].dNowConvertData[j] >= dX18xMaxConvertData || cx18x.sX18xDataStruct[i].dNowConvertData[j] <= dX18xMinConvertData) && sX18xChannelType[i, j] != modual.eX18xSensorChannelType.Null)
{
cx18x.sX18xAlarmstruct[i].usNowAlarmCode[j] |= (ushort)ModualAlarm.eAlarmCode.ChannelCollectDataError;
}
else
{
cx18x.sX18xAlarmstruct[i].usNowAlarmCode[j] &= (ushort)~ModualAlarm.eAlarmCode.ChannelCollectDataError;
}
cx18x.sX18xDataStruct[i].dBeforeConvertData[j] = cx18x.sX18xDataStruct[i].dNowConvertData[j];
}
cx18x.sX18xAlarmstruct[i].bDataNotFirstIncreaseOrDecrease = true;
......@@ -303,7 +332,7 @@ namespace ModbusDemo.Modular
{
ushort[] usChannelList = usChannelErrorlist.ToArray();
ushort[] usCodeList = usChannelErrorCodeList.ToArray();
ModualAlarm.vSetAlarmList(i, usChannelList, usCodeList, true);
ModualAlarm.vSetAlarmList(cx14P.byModbusID[i], usChannelList, usCodeList);
}
}
for (byte i = 0; i < byX18xNumber; i++)
......@@ -318,7 +347,7 @@ namespace ModbusDemo.Modular
bX18xError = true;
bOccurErrorChange = true;
usChannelErrorlist.Add (j);
usChannelErrorlist.Add((ushort)cx18x.sX18xAlarmstruct[i].usNowAlarmCode[j]);
usChannelErrorCodeList.Add((ushort)cx18x.sX18xAlarmstruct[i].usNowAlarmCode[j]);
}
cx18x.sX18xAlarmstruct[i].usBeforeAlarmCode[j] = cx18x.sX18xAlarmstruct[i].usNowAlarmCode[j];
}
......@@ -326,7 +355,7 @@ namespace ModbusDemo.Modular
{
ushort[] usChannelList = usChannelErrorlist.ToArray();
ushort[] usCodeList = usChannelErrorCodeList.ToArray();
ModualAlarm.vSetAlarmList(i, usChannelList, usCodeList, true);
ModualAlarm.vSetAlarmList(cx18x.byModbusID[i], usChannelList, usCodeList);
}
}
......@@ -342,7 +371,7 @@ namespace ModbusDemo.Modular
bOccurErrorChange = true;
bX78CError = true;
usChannelErrorlist.Add(j);
usChannelErrorlist.Add((ushort)cx78c.sX78cAlarmStruct[i].usNowAlarmCode[j]);
usChannelErrorCodeList.Add((ushort)cx78c.sX78cAlarmStruct[i].usNowAlarmCode[j]);
}
cx78c.sX78cAlarmStruct[i].usBeforeAlarmCode[j] = cx78c.sX78cAlarmStruct[i].usNowAlarmCode[j];
}
......@@ -350,7 +379,7 @@ namespace ModbusDemo.Modular
{
ushort[] usChannelList = usChannelErrorlist.ToArray();
ushort[] usCodeList = usChannelErrorCodeList.ToArray();
ModualAlarm.vSetAlarmList(i, usChannelList, usCodeList, true);
ModualAlarm.vSetAlarmList(cx78c.byModbusID[i], usChannelList, usCodeList);
}
}
......@@ -366,7 +395,7 @@ namespace ModbusDemo.Modular
bOccurErrorChange = true;
bX66cError = true;
usChannelErrorlist.Add(j);
usChannelErrorlist.Add((ushort)cx66c.sX66cAlarmStruct[i].usNowAlarmCode[j]);
usChannelErrorCodeList.Add((ushort)cx66c.sX66cAlarmStruct[i].usNowAlarmCode[j]);
}
cx66c.sX66cAlarmStruct[i].usBeforeAlarmCode[j] = cx66c.sX66cAlarmStruct[i].usNowAlarmCode[j];
}
......@@ -374,7 +403,7 @@ namespace ModbusDemo.Modular
{
ushort[] usChannelList = usChannelErrorlist.ToArray();
ushort[] usCodeList = usChannelErrorCodeList.ToArray();
ModualAlarm.vSetAlarmList(i, usChannelList, usCodeList, i == byX66cNumber - 1 ? true : false);
ModualAlarm.vSetAlarmList(cx66c.byModbusID[i], usChannelList, usCodeList);
}
}
if (bOccurErrorChange == true)
......
......@@ -70,18 +70,20 @@
this.cmbStopBit.Items.AddRange(new object[] {
"1",
"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, 4, 4, 4);
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;
//
// btCloseCOM
//
this.btCloseCOM.Enabled = false;
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, 4, 4, 4);
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.Text = "Close";
this.btCloseCOM.Click += new System.EventHandler(this.btCloseCOM_Click);
......@@ -93,17 +95,19 @@
"0-None Parity",
"1-Odd 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, 4, 4, 4);
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;
//
// btOpenCOM
//
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, 4, 4, 4);
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.Text = "Open";
this.btOpenCOM.Click += new System.EventHandler(this.btOpenCOM_Click);
......@@ -114,17 +118,19 @@
this.cmbDataBit.Items.AddRange(new object[] {
"7",
"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, 4, 4, 4);
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;
//
// labStopBit
//
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.Size = new System.Drawing.Size(56, 15);
this.labStopBit.Size = new System.Drawing.Size(75, 19);
this.labStopBit.TabIndex = 73;
this.labStopBit.Text = "Stop Bit";
this.labStopBit.TextAlign = System.Drawing.ContentAlignment.TopCenter;
......@@ -132,9 +138,10 @@
// labParity
//
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.Size = new System.Drawing.Size(98, 15);
this.labParity.Size = new System.Drawing.Size(131, 19);
this.labParity.TabIndex = 74;
this.labParity.Text = "Parity";
this.labParity.TextAlign = System.Drawing.ContentAlignment.TopCenter;
......@@ -142,9 +149,10 @@
// labBaud
//
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.Size = new System.Drawing.Size(75, 15);
this.labBaud.Size = new System.Drawing.Size(100, 19);
this.labBaud.TabIndex = 75;
this.labBaud.Text = "Baudrate";
this.labBaud.TextAlign = System.Drawing.ContentAlignment.TopCenter;
......@@ -152,9 +160,10 @@
// labDataBit
//
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.Size = new System.Drawing.Size(48, 15);
this.labDataBit.Size = new System.Drawing.Size(64, 19);
this.labDataBit.TabIndex = 76;
this.labDataBit.Text = "Data Bit";
this.labDataBit.TextAlign = System.Drawing.ContentAlignment.TopCenter;
......@@ -171,17 +180,19 @@
"38400",
"57600",
"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, 4, 4, 4);
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;
//
// labPort
//
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.Size = new System.Drawing.Size(75, 15);
this.labPort.Size = new System.Drawing.Size(100, 19);
this.labPort.TabIndex = 66;
this.labPort.Text = "COM Port";
this.labPort.TextAlign = System.Drawing.ContentAlignment.TopCenter;
......@@ -189,30 +200,32 @@
// cmbPort
//
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, 4, 4, 4);
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;
//
// label7
//
this.label7.Font = new System.Drawing.Font("Arial", 8F);
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.Size = new System.Drawing.Size(140, 17);
this.label7.Size = new System.Drawing.Size(187, 21);
this.label7.TabIndex = 64;
this.label7.Text = "Connection Status";
//
// txtReceiveMessage
//
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.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.txtReceiveMessage.Location = new System.Drawing.Point(563, 14);
this.txtReceiveMessage.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.txtReceiveMessage.Multiline = true;
this.txtReceiveMessage.Name = "txtReceiveMessage";
this.txtReceiveMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtReceiveMessage.Size = new System.Drawing.Size(509, 529);
this.txtReceiveMessage.Size = new System.Drawing.Size(677, 660);
this.txtReceiveMessage.TabIndex = 77;
this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged);
//
......@@ -224,35 +237,40 @@
this.updateops.Controls.Add(this.label13);
this.updateops.Controls.Add(this.UpdateBar);
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, 4, 4, 4);
this.updateops.Name = "updateops";
this.updateops.Size = new System.Drawing.Size(387, 111);
this.updateops.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.updateops.Size = new System.Drawing.Size(516, 139);
this.updateops.TabIndex = 92;
this.updateops.TabStop = false;
this.updateops.Text = "更新操作";
//
// 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.Size = new System.Drawing.Size(108, 12);
this.imagesname.Size = new System.Drawing.Size(144, 15);
this.imagesname.TabIndex = 5;
this.imagesname.Text = "wtd.bin";
//
// label12
//
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.Size = new System.Drawing.Size(41, 12);
this.label12.Size = new System.Drawing.Size(52, 15);
this.label12.TabIndex = 4;
this.label12.Text = "镜像:";
//
// 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, 4, 4, 4);
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.Text = "选择bin";
this.choosebth.UseVisualStyleBackColor = true;
......@@ -261,24 +279,27 @@
// label13
//
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.Size = new System.Drawing.Size(41, 12);
this.label13.Size = new System.Drawing.Size(52, 15);
this.label13.TabIndex = 2;
this.label13.Text = "进度:";
//
// 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, 4, 4, 4);
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;
//
// 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, 4, 4, 4);
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.Text = "一键更新";
this.UpdateBtn.UseVisualStyleBackColor = true;
......@@ -293,9 +314,10 @@
//
this.label1.AutoSize = true;
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.Size = new System.Drawing.Size(56, 16);
this.label1.Size = new System.Drawing.Size(69, 20);
this.label1.TabIndex = 93;
this.label1.Text = "温度:";
//
......@@ -303,35 +325,39 @@
//
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(33, 390);
this.label2.Location = new System.Drawing.Point(44, 488);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
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.Text = "水位:";
//
// label3
//
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.Size = new System.Drawing.Size(41, 12);
this.label3.Size = new System.Drawing.Size(55, 15);
this.label3.TabIndex = 95;
this.label3.Text = "label3";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(81, 394);
this.label4.Location = new System.Drawing.Point(108, 492);
this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
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.Text = "label4";
//
// 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, 4, 4, 4);
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.Text = "test";
this.button2.UseVisualStyleBackColor = true;
......@@ -339,9 +365,10 @@
//
// 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, 4, 4, 4);
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.Text = "订阅消息";
this.SubscribeBut.UseVisualStyleBackColor = true;
......@@ -350,28 +377,26 @@
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(83, 422);
this.label5.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label5.Location = new System.Drawing.Point(111, 528);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(41, 12);
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(17, 422);
this.label6.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label6.Location = new System.Drawing.Point(23, 528);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(65, 12);
this.label6.Size = new System.Drawing.Size(82, 15);
this.label6.TabIndex = 100;
this.label6.Text = "超时次数:";
//
// 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.ClientSize = new System.Drawing.Size(942, 551);
this.ClientSize = new System.Drawing.Size(1256, 689);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.SubscribeBut);
......@@ -396,6 +421,7 @@
this.Controls.Add(this.cmbPort);
this.Controls.Add(this.label7);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Form1";
this.Text = "牧场项目";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
......
......@@ -26,11 +26,8 @@ namespace ModbusDemo
{
public partial class Form1 : Form
{
bool Debug_test = false;//打开测试功能
_414P rtd = new _414P();
_478C state = new _478C();
_418X Analog = new _418X();
//Pond FunctionalService = new Pond();
const bool LOCALDEBUG = false;
private bool bStart = false;
FileStream fsl;
AutoResetEvent exitEvent;
......@@ -93,7 +90,7 @@ namespace ModbusDemo
string mqttTopic;
string mqttSubscribeTopic;
string mqttBackTopic;
string mqttSn = "20193261";
string mqttSn = "66666666";
string mqttData;//数据
string mqttData_Back;//数据
private IMqttClient mqttClient = null;
......@@ -111,7 +108,7 @@ namespace ModbusDemo
private void SetMqtt()
{
mqttTopic = "Witium/WTDS78X/" + mqttSn + "/Data";
if (Debug_test == true)
if (LOCALDEBUG == true)
{
mqttIp = "47.101.50.24";
mqttClientId = GetTimeStamp() + "sxz";
......@@ -127,7 +124,6 @@ namespace ModbusDemo
mqttUsername = "pasture";
mqttPwd = "Pasture37774020";
}
}
private void SetTopic()
{
......@@ -137,7 +133,6 @@ namespace ModbusDemo
}
void m_bgw0_DoWork(object sender, DoWorkEventArgs e)
{
while (m_Isbgw0_CanContinueRun)
{
int _n = 2;
......@@ -149,7 +144,7 @@ namespace ModbusDemo
Thread.Sleep(1000);
}
}
#region 时间戳
#region 时间戳
/// <summary>
/// 获取时间戳
/// </summary>
......@@ -160,17 +155,17 @@ namespace ModbusDemo
return Convert.ToUInt64(ts.TotalSeconds);
}
#endregion
#endregion
#region 方法
#region 方法
public void control(byte SlaveAddress, ushort startAddress, bool value)
{
bool OutputValue;
int Caution = 0;
bool[] StateValue = { };
int[] WOnOff = { 0 };
Modbus_ReadCoilsTask(out StateValue, SlaveAddress, 16, 6, out int signW);
WOnOff = state.IO(StateValue);
//Modbus_ReadCoilsTask(out StateValue, SlaveAddress, 16, 6, out int signW);
//WOnOff = state.IO(StateValue);
if (WOnOff[0] == 1)
{
if (startAddress == 18)
......@@ -179,8 +174,8 @@ namespace ModbusDemo
}
else
{
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
else if(WOnOff[2]==1)
......@@ -191,17 +186,16 @@ namespace ModbusDemo
}
else
{
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
else
{
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, startAddress, value, out Caution);
//Modbus_WriteSingleCoil(out OutputValue, SlaveAddress, (ushort)(startAddress + 1), value, out Caution);
}
}
public static byte[] getByte(bool[] array)
{
byte[] data = new byte[array.Length];
......@@ -214,11 +208,9 @@ namespace ModbusDemo
}
return data;
}
#endregion
#endregion
#region mqtt服务
#region mqtt服务
//创建一个委托,是为访问TextBox控件服务的。
public delegate void UpdateTxt(string msg);
//定义一个委托变量
......@@ -414,10 +406,13 @@ namespace ModbusDemo
{
bool on_off = true;
string passageway;
string stConvertString;
//消息的内容
txtReceiveMessage.AppendText($">> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}");
totxt.Log($">> Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}{Environment.NewLine}");
Console.WriteLine(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
stConvertString = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
Console.WriteLine(stConvertString);
ModualControl.vGetaWay_Receive_Control(stConvertString);
Ctrl ctrl = JsonConvert.DeserializeObject<Ctrl>(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
Ctrl ctrl_Back = new Ctrl();
Ctrl2 ctrl2 = new Ctrl2();
......@@ -452,7 +447,7 @@ namespace ModbusDemo
ssses[0].op.Add(passageway, readyState);
ssses[0].op.Add("ts",GetTimeStamp());
ctrl2.ss = ssses;
}
}
if (ctrl.ss[0].op.d1 == 1 ||
ctrl.ss[0].op.d0 == 1 ||
ctrl.ss[0].op.d2 == 1 ||
......@@ -483,8 +478,6 @@ namespace ModbusDemo
objThread.Start();
}
if (State_enable == true)
{
control(ctrl.ss[0].getAddr(), address, on_off);//根据下发确定控制通道
......@@ -534,26 +527,41 @@ namespace ModbusDemo
}
this.BeginInvoke(updateTxt, "线程结束");
}
#endregion
#region modbus线程
#endregion
#region modbus线程
private static bool bSetDataIsRunning = false;
private Thread ModbusPollThread;
private bool bModbusPollThreadStart = false;
private void GetData()
{
timer.Interval = 120000;
timer.Enabled = true;
timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
timer.Start();
timer.Elapsed += (o, a) =>
{
SetData();
};
}
//timer.Interval = 12000;
//timer.Enabled = true;
//timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
//timer.Start();
//timer.Elapsed += (o, a) =>
//{
// SetData();
//};
}
private void vModbusPoll()
{
while(bModbusPollThreadStart)
{
bSetDataIsRunning = true;
totxt.Log("开始执行");
ModualPoll.vAllNodePoll();
mqttData = ModualPoll.stAll_Poll_Package();
ModualPoll.vCheckAlarm_And_Send(mqttClient);
totxt.Log("执行结束");
label5.Text = ModbusComm.usCommunFailTimes.ToString();
}
}
private void Sendout()
{
Control.CheckForIllegalCrossThreadCalls = false;
MQTTTimer.Interval = 120500;
MQTTTimer.Interval = 20000;//120500
MQTTTimer.Enabled = true;
MQTTTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
MQTTTimer.Start();
......@@ -561,7 +569,6 @@ namespace ModbusDemo
{
try
{
label5.Text =ModbusComm.usCommunFailTimes.ToString();
await Publish();
}
catch (Exception ex)
......@@ -582,26 +589,26 @@ namespace ModbusDemo
}
else
{
await Task.Run(() =>
if (bSetDataIsRunning == false)
{
totxt.Log("开始执行");
ModualPoll.vAllNodePoll();
mqttData = ModualPoll.stAll_Poll_Package();
totxt.Log("执行结束");
});
bSetDataIsRunning = true;
await Task.Run(() =>
{
totxt.Log("开始执行");
ModualPoll.vAllNodePoll();
mqttData = ModualPoll.stAll_Poll_Package();
ModualPoll.vCheckAlarm_And_Send(mqttClient);
totxt.Log("执行结束");
});
bSetDataIsRunning = false;
}
}
}
#region PoolTest
//pcPool cPool1 = new pcPool("East_transfer_pond");
//double PoolTemper = 0;
}
#region PoolTest
#endregion
#endregion
//声明委托
public static ushort usCommunFailTimes = 0;
private delegate void ShowMessageDelegate(string message);
private void ShowMessage(string message)
{
......@@ -616,420 +623,9 @@ namespace ModbusDemo
//totxt.Log(message);
}
}
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);
Caution = 0;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
timer.Stop();
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
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.
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"));
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!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
OutputValue = new ushort[numberOfPoints];
Caution = -1;
}
}
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);
Caution = 0;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
timer.Stop();
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
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.
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"));
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!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
OutputValue = new bool[numberOfPoints];
Caution = -1;
}
}
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);
OutputValue = value;
Caution = 0;
}
catch (Exception exception)
{
//Connection exception
//No response from server.
//The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System"))
{
timer.Stop();
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
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.
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"));
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!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break;
case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break;
case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break;
case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break;
case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break;
case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break;
case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break;
case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break;
case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break;
}
}
OutputValue = false;
Caution = -1;
}
}
private async void Modbus_polling()
{
//float EastTemperature = 0;
//float Easternwaterlevel = 0;
//float WestWaterLevel = 0;
//int sign = 0;
//try
//{
// 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 };
// 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, 21, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out Inputtype, 21, 10, 8, out int signI);
// UInt64 time_2 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out registerN, 53, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out InputtypeN, 53, 10, 8, out int signIn);
// UInt64 time_3 = GetTimeStamp();
// Modbus_ReadCoilsTask(out EIO, 40, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out SIO, 19, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out WIO, 41, 16, 6, out int signW);
// if (sign == -1)
// {
// }
// //else
// //{
// vs = rtd.RtdValue(temperature, type);
// EastTemperature = float.Parse(vs[0]);
// s = Analog.Value(register, Inputtype);
// //(实时电流值 - 设定电流最小值) /(20Ma - 设定电流最小值) *设定测量最大值
// //(实时电流值 - 4ma) /(20ma - 4ma) *设定测量最大值(也就是设定的满量程)
// Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6;
// s6 = Analog.Value(registerN, InputtypeN);
// WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6;
// label3.Text = EastTemperature.ToString();
// label4.Text = Easternwaterlevel.ToString();
// EOnOff = state.IO(EIO);////东中转池
// SOnOff = state.IO(SIO);////西中转池
// WOnOff = state.IO(WIO); ////东南北水
// //}
// WTDR14P x = new WTDR14P("27", EastTemperature, 0, 0, 0, time_1);
// WTDR18X p = new WTDR18X("21", Easternwaterlevel, 0, 0, 0, 0, 0, 0, 0, time_2);
// WTDR18X q = new WTDR18X("53", WestWaterLevel, 0, 0, 0, 0, 0, 0, 0, time_3);
// //东搅拌
// WTDR66C EastStir = new WTDR66C("40", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// //东南北水
// WTDR66C WestStir = new WTDR66C("41", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp());
// //西搅拌
// WTDR66C SouthWater = new WTDR66C("19", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp());
// mqttData = "[" +
// JsonConvert.SerializeObject(x) + "," +
// JsonConvert.SerializeObject(p) + "," +
// JsonConvert.SerializeObject(q) + "," +
// JsonConvert.SerializeObject(EastStir) + "," +
// JsonConvert.SerializeObject(WestStir) + "," +
// JsonConvert.SerializeObject(SouthWater)
// + "]";
//}
//catch (Exception exception)
//{
// //Connection exception
// //No response from server.
// //The server maybe close the com port, or response timeout.
// if (exception.Source.Equals("System"))
// {
// timer.Stop();
// Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
// totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
// return;
// }
// //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"));
// totxt.Log("Function Code: " + FunctionCode.ToString("X"));
// //MessageBox.Show("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!");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
// break;
// case "2":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
// break;
// case "3":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
// break;
// case "4":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
// break;
// case "5":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
// break;
// case "6":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
// break;
// case "8":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
// break;
// case "A":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
// break;
// case "B":
// Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
// totxt.Log("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
// //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY TARGET DEVICE FAILED TO RESPOND!");
// break;
// }
// return;
// }
//}
//finally
//{
//}
return;
}
#endregion
#endregion
#region json字符串转对象
#region json字符串转对象
/// <summary>
/// json字符串转对象
/// </summary>
......@@ -1094,9 +690,9 @@ namespace ModbusDemo
return "";
}
}
#endregion
#endregion
#region 网络检测
#region 网络检测
ManualResetEvent network = new ManualResetEvent(false);
Thread MonitorThread;
......@@ -1225,9 +821,9 @@ namespace ModbusDemo
}
#endregion
#endregion
#region 更新线程
#region 更新线程
string ImagefilePath = null;
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
......@@ -1562,8 +1158,7 @@ namespace ModbusDemo
}
}
}
#endregion
#endregion
private void txtReceiveMessage_TextChanged(object sender, EventArgs e)
{
......@@ -1588,11 +1183,12 @@ namespace ModbusDemo
private MethodInvoker simpleDelegate1;
private MethodInvoker MQTT_thread;
[Obsolete]
private void btOpenCOM_Click(object sender, EventArgs e)
{
if (Debug_test == true)
if (LOCALDEBUG == true)
{
comPort.PortName = "COM2";
comPort.PortName = "COM14";
comPort.BaudRate = 9600;
comPort.Parity = Parity.None;
comPort.StopBits = StopBits.One;
......@@ -1634,10 +1230,19 @@ namespace ModbusDemo
comPort.Open();
isReconnect = true;
SetMqtt();
simpleDelegate1 = new MethodInvoker(GetData);
simpleDelegate1.BeginInvoke(null, null);
if (ModbusPollThread == null)
{
ModbusPollThread = new Thread(vModbusPoll);
ModbusPollThread.Start();
}
else
ModbusPollThread.Resume();
bModbusPollThreadStart = true;
//simpleDelegate1 = new MethodInvoker(GetData);
//simpleDelegate1.BeginInvoke(null, null);
MQTT_thread = new MethodInvoker(Sendout);
MQTT_thread.BeginInvoke(null,null);
MQTT_thread.BeginInvoke(null, null);
Task.Run(async () => { await ConnectMqttServerAsync(); });
ModbusComm.Modbus = ModbusSerialMaster.CreateRtu(comPort);
ModbusComm.Modbus.Transport.Retries = 0;
ModbusComm.Modbus.Transport.ReadTimeout = 1000;
......@@ -1649,6 +1254,7 @@ namespace ModbusDemo
btOpenCOM.Enabled = false;
btCloseCOM.Enabled = true;
totxt.Log(DateTime.Now.ToString() + " =>Open " + comPort.PortName + " sucessfully!");
}
catch (Exception ex)
{
......@@ -1657,10 +1263,14 @@ namespace ModbusDemo
return;
}
}
[Obsolete]
private void btCloseCOM_Click(object sender, EventArgs e)
{
//Close comport first,then stop and dispose slave.
timer.Stop();
//timer.Stop();
bModbusPollThreadStart = false;
if(ModbusPollThread.IsAlive)
ModbusPollThread.Suspend();
isReconnect = false;
modbus_Timer.Enabled = false;
btOpenCOM.Enabled = true;
......
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