Commit 4559c01e authored by shaxuezheng's avatar shaxuezheng

Modify Form1.cs

parent df7736b6
......@@ -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,13 +108,13 @@ namespace ModbusDemo
private void SetMqtt()
{
mqttTopic = "Witium/WTDS78X/" + mqttSn + "/Data";
if (Debug_test == true)
if (LOCALDEBUG == true)
{
mqttIp = "172.16.1.24";
mqttIp = "47.101.50.24";
mqttClientId = GetTimeStamp() + "sxz";
mqttPort = 1883;
mqttUsername = "pasture";
mqttPwd = "Pasture37774020";
mqttUsername = "root";
mqttPwd = "public";
}
else
{
......@@ -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();
......@@ -483,13 +478,11 @@ namespace ModbusDemo
objThread.Start();
}
if (State_enable == true)
{
control(ctrl.ss[0].getAddr(), address, on_off);//根据下发确定控制通道
mqttData_Back = JsonConvert.SerializeObject(ctrl2);
Modbus_polling();
//Modbus_polling();
await Publish();
await Back();
}
......@@ -525,7 +518,7 @@ namespace ModbusDemo
{
control(ID, address, false);//根据下发确定控制通道
control(ID, (ushort)(address+1), false);//根据下发确定控制通道
Modbus_polling();
//Modbus_polling();
await Publish();
time = 0;
n = 0;
......@@ -534,13 +527,15 @@ 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 = 240000;
timer.Interval = 9000;
timer.Enabled = true;
timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
timer.Start();
......@@ -553,7 +548,7 @@ namespace ModbusDemo
private void Sendout()
{
Control.CheckForIllegalCrossThreadCalls = false;
MQTTTimer.Interval = 480000;
MQTTTimer.Interval = 7000;
MQTTTimer.Enabled = true;
MQTTTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
MQTTTimer.Start();
......@@ -562,7 +557,6 @@ namespace ModbusDemo
try
{
await Publish();
totxt.Log(mqttData + Environment.NewLine + "\n");
}
catch (Exception ex)
{
......@@ -584,24 +578,15 @@ namespace ModbusDemo
{
await Task.Run(() =>
{
totxt.Log("开始执行");
ModualPoll.vAllNodePoll();
mqttData = ModualPoll.stAll_Poll_Package();
totxt.Log("执行结束");
Modbus_polling();
});
}
}
#region PoolTest
//pcPool cPool1 = new pcPool("East_transfer_pond");
//double PoolTemper = 0;
#endregion
#endregion
//声明委托
public static ushort usCommunFailTimes = 0;
private delegate void ShowMessageDelegate(string message);
private void ShowMessage(string message)
{
......@@ -862,41 +847,99 @@ namespace ModbusDemo
}
private async void Modbus_polling()
{
//float EastTemperature = 0;
//float Easternwaterlevel = 0;
//float WestWaterLevel = 0;
//int sign = 0;
//try
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
//{
// 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);
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();
// Modbus_ReadHoldingRegistersTask(out register, 21, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out Inputtype, 21, 10, 8, out int signI);
// Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr);
// Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI);
// UInt64 time_2 = GetTimeStamp();
// Modbus_ReadHoldingRegistersTask(out registerN, 53, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out InputtypeN, 53, 10, 8, out int signIn);
// Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre);
// Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn);
// UInt64 time_3 = GetTimeStamp();
// Modbus_ReadCoilsTask(out EIO, 40, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out EIO, 5, 16, 6, out int signE);
// Modbus_ReadCoilsTask(out SIO, 19, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out SIO, 8, 16, 6, out int signS);
// Modbus_ReadCoilsTask(out WIO, 41, 16, 6, out int signW);
// Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW);
// if (sign == -1)
// {
......@@ -907,9 +950,6 @@ namespace ModbusDemo
// 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;
......@@ -920,16 +960,16 @@ namespace ModbusDemo
// 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);
// 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("40", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// WTDR66C EastStir = new WTDR66C("5", 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 WestStir = new WTDR66C("4", 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());
// WTDR66C SouthWater = new WTDR66C("8", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp());
// mqttData = "[" +
// JsonConvert.SerializeObject(x) + "," +
......@@ -941,94 +981,94 @@ namespace ModbusDemo
// + "]";
//}
//catch (Exception exception)
//{
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
//{
//}
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
#region json字符串转对象
#region json字符串转对象
/// <summary>
/// json字符串转对象
/// </summary>
......@@ -1093,10 +1133,9 @@ namespace ModbusDemo
return "";
}
}
#endregion
#endregion
#region 网络检测
#region 网络检测
ManualResetEvent network = new ManualResetEvent(false);
Thread MonitorThread;
......@@ -1225,9 +1264,9 @@ namespace ModbusDemo
}
#endregion
#endregion
#region 更新线程
#region 更新线程
string ImagefilePath = null;
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
......@@ -1562,8 +1601,7 @@ namespace ModbusDemo
}
}
}
#endregion
#endregion
private void txtReceiveMessage_TextChanged(object sender, EventArgs e)
{
......@@ -1575,7 +1613,6 @@ namespace ModbusDemo
private void button2_Click(object sender, EventArgs e)
{
//Modbus_polling();
ModualPoll.vAllNodePoll();
mqttData=ModualPoll.stAll_Poll_Package();
}
......@@ -1589,18 +1626,19 @@ namespace ModbusDemo
private MethodInvoker simpleDelegate1;
private MethodInvoker MQTT_thread;
[Obsolete]
private void btOpenCOM_Click(object sender, EventArgs e)
{
//if (Debug_test == true)
//{
// comPort.PortName = "COM2";
// comPort.BaudRate = 9600;
// comPort.Parity = Parity.None;
// comPort.StopBits = StopBits.One;
// comPort.DataBits = 8;
//}
//else
//{
if (LOCALDEBUG == true)
{
comPort.PortName = "COM14";
comPort.BaudRate = 9600;
comPort.Parity = Parity.None;
comPort.StopBits = StopBits.One;
comPort.DataBits = 8;
}
else
{
if (cmbPort.Text == "")
{
MessageBox.Show("串口打开错误");
......@@ -1629,34 +1667,35 @@ namespace ModbusDemo
{
comPort.StopBits = StopBits.One;
}
//}
}
try
{
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(); });
//master = ModbusSerialMaster.CreateRtu(comPort);
//master.Transport.Retries = 0; //重试次数
//master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms)
//master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms)
//master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms)
ModbusComm.Modbus = ModbusSerialMaster.CreateRtu(comPort);
ModbusComm.Modbus.Transport.Retries = 0;
ModbusComm.Modbus.Transport.ReadTimeout = 1000;
ModbusComm.Modbus.Transport.WriteTimeout = 1000;
ModbusComm.Modbus.Transport.WaitToRetryMilliseconds = 0;
master = ModbusSerialMaster.CreateRtu(comPort);
master.Transport.Retries = 0; //重试次数
master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms)
master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms)
master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms)
modbus_Timer.Enabled = true;
btOpenCOM.Enabled = false;
btCloseCOM.Enabled = true;
totxt.Log(DateTime.Now.ToString() + " =>Open " + comPort.PortName + " sucessfully!");
}
catch (Exception ex)
{
......@@ -1665,11 +1704,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;
......@@ -1679,6 +1721,4 @@ namespace ModbusDemo
totxt.Log(DateTime.Now.ToString() + " =>Disconnect " + comPort.PortName);
}
}
}
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