Commit df7736b6 authored by shaxuezheng's avatar shaxuezheng

参数修改

parent c089422c
...@@ -21,6 +21,7 @@ using System.Timers; ...@@ -21,6 +21,7 @@ using System.Timers;
using ModbusDemo.application; using ModbusDemo.application;
using System.Collections; using System.Collections;
using ModbusDemo.Modular; using ModbusDemo.Modular;
using ModbusDemo.MessageFormat;
namespace ModbusDemo namespace ModbusDemo
{ {
public partial class Form1 : Form public partial class Form1 : Form
...@@ -539,7 +540,7 @@ namespace ModbusDemo ...@@ -539,7 +540,7 @@ namespace ModbusDemo
private void GetData() private void GetData()
{ {
timer.Interval = 9000; timer.Interval = 240000;
timer.Enabled = true; timer.Enabled = true;
timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true); timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
timer.Start(); timer.Start();
...@@ -552,7 +553,7 @@ namespace ModbusDemo ...@@ -552,7 +553,7 @@ namespace ModbusDemo
private void Sendout() private void Sendout()
{ {
Control.CheckForIllegalCrossThreadCalls = false; Control.CheckForIllegalCrossThreadCalls = false;
MQTTTimer.Interval = 7000; MQTTTimer.Interval = 480000;
MQTTTimer.Enabled = true; MQTTTimer.Enabled = true;
MQTTTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true); MQTTTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
MQTTTimer.Start(); MQTTTimer.Start();
...@@ -583,7 +584,10 @@ namespace ModbusDemo ...@@ -583,7 +584,10 @@ namespace ModbusDemo
{ {
await Task.Run(() => await Task.Run(() =>
{ {
Modbus_polling(); totxt.Log("开始执行");
ModualPoll.vAllNodePoll();
mqttData = ModualPoll.stAll_Poll_Package();
totxt.Log("执行结束");
}); });
} }
...@@ -858,99 +862,41 @@ namespace ModbusDemo ...@@ -858,99 +862,41 @@ namespace ModbusDemo
} }
private async 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; //int sign = 0;
try //try
{ //{
ushort[] temperature = { }; // ushort[] temperature = { };
ushort[] type = { }; // ushort[] type = { };
string[] vs = { }; // string[] vs = { };
string[] s = { }; // string[] s = { };
string[] s6 = { }; // string[] s6 = { };
ushort[] register = { }; // ushort[] register = { };
ushort[] Inputtype = { }; // ushort[] Inputtype = { };
ushort[] registerN = { }; // ushort[] registerN = { };
ushort[] InputtypeN = { }; // ushort[] InputtypeN = { };
bool[] EIO = { }; // bool[] EIO = { };
bool[] SIO = { }; // bool[] SIO = { };
bool[] WIO = { }; // bool[] WIO = { };
int[] EOnOff = { 0 }; // int[] EOnOff = { 0 };
int[] SOnOff = { 0 }; // int[] SOnOff = { 0 };
int[] WOnOff = { 0 }; // int[] WOnOff = { 0 };
Modbus_ReadHoldingRegistersTask(out temperature, 27, 0, 4, out int signT); // Modbus_ReadHoldingRegistersTask(out temperature, 27, 0, 4, out int signT);
Modbus_ReadHoldingRegistersTask(out type, 27, 10, 4, out sign); // 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)
+ "]";
}
// Modbus_ReadHoldingRegistersTask(out temperature, 1, 0, 4, out int signT);
// Modbus_ReadHoldingRegistersTask(out type, 1, 10, 4, out sign);
// UInt64 time_1 = GetTimeStamp(); // UInt64 time_1 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr); // Modbus_ReadHoldingRegistersTask(out register, 21, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI); // Modbus_ReadHoldingRegistersTask(out Inputtype, 21, 10, 8, out int signI);
// UInt64 time_2 = GetTimeStamp(); // UInt64 time_2 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre); // Modbus_ReadHoldingRegistersTask(out registerN, 53, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn); // Modbus_ReadHoldingRegistersTask(out InputtypeN, 53, 10, 8, out int signIn);
// UInt64 time_3 = GetTimeStamp(); // UInt64 time_3 = GetTimeStamp();
// Modbus_ReadCoilsTask(out EIO, 5, 16, 6, out int signE); // Modbus_ReadCoilsTask(out EIO, 40, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out SIO, 8, 16, 6, out int signS); // Modbus_ReadCoilsTask(out SIO, 19, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW); // Modbus_ReadCoilsTask(out WIO, 41, 16, 6, out int signW);
// if (sign == -1) // if (sign == -1)
// { // {
...@@ -961,6 +907,9 @@ namespace ModbusDemo ...@@ -961,6 +907,9 @@ namespace ModbusDemo
// vs = rtd.RtdValue(temperature, type); // vs = rtd.RtdValue(temperature, type);
// EastTemperature = float.Parse(vs[0]); // EastTemperature = float.Parse(vs[0]);
// s = Analog.Value(register, Inputtype); // s = Analog.Value(register, Inputtype);
// //(实时电流值 - 设定电流最小值) /(20Ma - 设定电流最小值) *设定测量最大值
// //(实时电流值 - 4ma) /(20ma - 4ma) *设定测量最大值(也就是设定的满量程)
// Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6; // Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6;
// s6 = Analog.Value(registerN, InputtypeN); // s6 = Analog.Value(registerN, InputtypeN);
// WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6; // WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6;
...@@ -971,16 +920,16 @@ namespace ModbusDemo ...@@ -971,16 +920,16 @@ namespace ModbusDemo
// WOnOff = state.IO(WIO); ////东南北水 // WOnOff = state.IO(WIO); ////东南北水
// //} // //}
// WTDR14P x = new WTDR14P("1", EastTemperature, 0, 0, 0, time_1); // WTDR14P x = new WTDR14P("27", EastTemperature, 0, 0, 0, time_1);
// WTDR18X p = new WTDR18X("3", Easternwaterlevel, 0, 0, 0, 0, 0, 0, 0, time_2); // WTDR18X p = new WTDR18X("21", 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); // WTDR18X q = new WTDR18X("53", WestWaterLevel, 0, 0, 0, 0, 0, 0, 0, time_3);
// //东搅拌 // //东搅拌
// WTDR66C EastStir = new WTDR66C("5", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp()); // WTDR66C EastStir = new WTDR66C("40", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// //东南北水 // //东南北水
// WTDR66C WestStir = new WTDR66C("4", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp()); // WTDR66C WestStir = new WTDR66C("41", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp());
// //西搅拌 // //西搅拌
// WTDR66C SouthWater = new WTDR66C("8", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp()); // WTDR66C SouthWater = new WTDR66C("19", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp());
// mqttData = "[" + // mqttData = "[" +
// JsonConvert.SerializeObject(x) + "," + // JsonConvert.SerializeObject(x) + "," +
...@@ -992,88 +941,88 @@ namespace ModbusDemo ...@@ -992,88 +941,88 @@ namespace ModbusDemo
// + "]"; // + "]";
//} //}
catch (Exception exception) //catch (Exception exception)
{ //{
//Connection exception //Connection exception
//No response from server. //No response from server.
//The server maybe close the com port, or response timeout. //The server maybe close the com port, or response timeout.
if (exception.Source.Equals("System")) // if (exception.Source.Equals("System"))
{ // {
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);
return; // return;
} // }
//The server return error code. // //The server return error code.
//You can get the function code and exception code. // //You can get the function code and exception code.
if (exception.Source.Equals("nModbusPC")) // if (exception.Source.Equals("nModbusPC"))
{ // {
string str = exception.Message; // string str = exception.Message;
int FunctionCode; // int FunctionCode;
string ExceptionCode; // string ExceptionCode;
str = str.Remove(0, str.IndexOf("\r\n") + 17); // str = str.Remove(0, str.IndexOf("\r\n") + 17);
FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n"))); // FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
Console.WriteLine("Function Code: " + FunctionCode.ToString("X")); // Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
totxt.Log("Function Code: " + FunctionCode.ToString("X")); // totxt.Log("Function Code: " + FunctionCode.ToString("X"));
//MessageBox.Show("Function Code: " + FunctionCode.ToString("X")); // //MessageBox.Show("Function Code: " + FunctionCode.ToString("X"));
str = str.Remove(0, str.IndexOf("\r\n") + 17); // str = str.Remove(0, str.IndexOf("\r\n") + 17);
ExceptionCode = str.Remove(str.IndexOf("-")); // ExceptionCode = str.Remove(str.IndexOf("-"));
switch (ExceptionCode.Trim()) // switch (ExceptionCode.Trim())
{ // {
case "1": // case "1":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!"); // totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
break; // break;
case "2": // case "2":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
totxt.Log("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!"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!");
break; // break;
case "3": // case "3":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
totxt.Log("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!"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!");
break; // break;
case "4": // case "4":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
totxt.Log("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!"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!");
break; // break;
case "5": // case "5":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!"); // totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
//MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> ACKNOWLEDGE!");
break; // break;
case "6": // case "6":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
totxt.Log("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 !"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> SLAVE DEVICE BUSY !");
break; // break;
case "8": // case "8":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
totxt.Log("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 !"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> MEMORY PARITY ERROR !");
break; // break;
case "A": // case "A":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
totxt.Log("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 !"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY PATH UNAVAILABLE !");
break; // break;
case "B": // case "B":
Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!"); // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "---->GATEWAY TARGET DEVICE FAILED TO RESPOND!");
totxt.Log("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!"); // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> GATEWAY TARGET DEVICE FAILED TO RESPOND!");
break; // break;
} // }
return; // return;
} // }
} //}
finally //finally
{ //{
} //}
return; return;
} }
...@@ -1628,7 +1577,7 @@ namespace ModbusDemo ...@@ -1628,7 +1577,7 @@ namespace ModbusDemo
{ {
//Modbus_polling(); //Modbus_polling();
ModualPoll.vAllNodePoll(); ModualPoll.vAllNodePoll();
ModualPoll.stAll_Poll_Package(); mqttData=ModualPoll.stAll_Poll_Package();
} }
private async void SubscribeBut_Click(object sender, EventArgs e) private async void SubscribeBut_Click(object sender, EventArgs e)
...@@ -1691,11 +1640,19 @@ namespace ModbusDemo ...@@ -1691,11 +1640,19 @@ namespace ModbusDemo
MQTT_thread = new MethodInvoker(Sendout); MQTT_thread = new MethodInvoker(Sendout);
MQTT_thread.BeginInvoke(null,null); MQTT_thread.BeginInvoke(null,null);
Task.Run(async () => { await ConnectMqttServerAsync(); }); Task.Run(async () => { await ConnectMqttServerAsync(); });
master = ModbusSerialMaster.CreateRtu(comPort); //master = ModbusSerialMaster.CreateRtu(comPort);
master.Transport.Retries = 0; //重试次数 //master.Transport.Retries = 0; //重试次数
master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms) //master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms)
master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms) //master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms)
master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms) //master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms)
ModbusComm.Modbus = ModbusSerialMaster.CreateRtu(comPort);
ModbusComm.Modbus.Transport.Retries = 0;
ModbusComm.Modbus.Transport.ReadTimeout = 1000;
ModbusComm.Modbus.Transport.WriteTimeout = 1000;
ModbusComm.Modbus.Transport.WaitToRetryMilliseconds = 0;
modbus_Timer.Enabled = true; modbus_Timer.Enabled = true;
btOpenCOM.Enabled = false; btOpenCOM.Enabled = false;
btCloseCOM.Enabled = true; btCloseCOM.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