Commit 2b471f9a authored by 95Jack's avatar 95Jack

Modifying Log Errors

parent c089422c
...@@ -36,17 +36,6 @@ namespace ModbusDemo.windows ...@@ -36,17 +36,6 @@ namespace ModbusDemo.windows
} }
writer.WriteLine(DateTime.Now + ": " + info); writer.WriteLine(DateTime.Now + ": " + info);
} }
catch (Exception e)
{
if (writer != null)
{
writer.Close();
writer.Dispose();
fileStream.Close();
fileStream.Dispose();
}
throw;
}
finally finally
{ {
if (writer != null) if (writer != null)
......
...@@ -46,7 +46,7 @@ namespace ModbusDemo.MessageFormat ...@@ -46,7 +46,7 @@ namespace ModbusDemo.MessageFormat
if (exception.Source.Equals("System")) if (exception.Source.Equals("System"))
{ {
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message); Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message); Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_ReadHoldingRegistersTask");
OutputValue = new ushort[numberOfPoints]; OutputValue = new ushort[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT; ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++; usCommunFailTimes++;
...@@ -127,7 +127,7 @@ namespace ModbusDemo.MessageFormat ...@@ -127,7 +127,7 @@ namespace ModbusDemo.MessageFormat
if (exception.Source.Equals("System")) if (exception.Source.Equals("System"))
{ {
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message); Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message); Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_ReadCoilsTask");
OutputValue = new bool[numberOfPoints]; OutputValue = new bool[numberOfPoints];
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT; ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
usCommunFailTimes++; usCommunFailTimes++;
...@@ -208,7 +208,7 @@ namespace ModbusDemo.MessageFormat ...@@ -208,7 +208,7 @@ namespace ModbusDemo.MessageFormat
if (exception.Source.Equals("System")) if (exception.Source.Equals("System"))
{ {
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message); Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message); Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_WriteSingleCoil");
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT; ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
} }
//The server return error code. //The server return error code.
...@@ -286,7 +286,7 @@ namespace ModbusDemo.MessageFormat ...@@ -286,7 +286,7 @@ namespace ModbusDemo.MessageFormat
if (exception.Source.Equals("System")) if (exception.Source.Equals("System"))
{ {
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message); Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message); Form1.totxt.Log(DateTime.Now.ToString() + " " + exception.Message+ "Modbus_WriteMultipleCoils");
ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT; ModbusErrorCode = eModbusErrorCode.MB_ETIMEDOUT;
} }
//The server return error code. //The server return error code.
......
...@@ -70,20 +70,18 @@ ...@@ -70,20 +70,18 @@
this.cmbStopBit.Items.AddRange(new object[] { this.cmbStopBit.Items.AddRange(new object[] {
"1", "1",
"2"}); "2"});
this.cmbStopBit.Location = new System.Drawing.Point(461, 98); this.cmbStopBit.Location = new System.Drawing.Point(346, 78);
this.cmbStopBit.Margin = new System.Windows.Forms.Padding(4);
this.cmbStopBit.Name = "cmbStopBit"; this.cmbStopBit.Name = "cmbStopBit";
this.cmbStopBit.Size = new System.Drawing.Size(73, 24); this.cmbStopBit.Size = new System.Drawing.Size(56, 22);
this.cmbStopBit.TabIndex = 70; this.cmbStopBit.TabIndex = 70;
// //
// btCloseCOM // btCloseCOM
// //
this.btCloseCOM.Enabled = false; this.btCloseCOM.Enabled = false;
this.btCloseCOM.Font = new System.Drawing.Font("Arial", 8F); this.btCloseCOM.Font = new System.Drawing.Font("Arial", 8F);
this.btCloseCOM.Location = new System.Drawing.Point(432, 130); this.btCloseCOM.Location = new System.Drawing.Point(324, 104);
this.btCloseCOM.Margin = new System.Windows.Forms.Padding(4);
this.btCloseCOM.Name = "btCloseCOM"; this.btCloseCOM.Name = "btCloseCOM";
this.btCloseCOM.Size = new System.Drawing.Size(104, 29); this.btCloseCOM.Size = new System.Drawing.Size(78, 23);
this.btCloseCOM.TabIndex = 72; this.btCloseCOM.TabIndex = 72;
this.btCloseCOM.Text = "Close"; this.btCloseCOM.Text = "Close";
this.btCloseCOM.Click += new System.EventHandler(this.btCloseCOM_Click); this.btCloseCOM.Click += new System.EventHandler(this.btCloseCOM_Click);
...@@ -95,19 +93,17 @@ ...@@ -95,19 +93,17 @@
"0-None Parity", "0-None Parity",
"1-Odd Parity", "1-Odd Parity",
"2-Even Parity"}); "2-Even Parity"});
this.cmbParity.Location = new System.Drawing.Point(311, 98); this.cmbParity.Location = new System.Drawing.Point(233, 78);
this.cmbParity.Margin = new System.Windows.Forms.Padding(4);
this.cmbParity.Name = "cmbParity"; this.cmbParity.Name = "cmbParity";
this.cmbParity.Size = new System.Drawing.Size(132, 24); this.cmbParity.Size = new System.Drawing.Size(100, 22);
this.cmbParity.TabIndex = 69; this.cmbParity.TabIndex = 69;
// //
// btOpenCOM // btOpenCOM
// //
this.btOpenCOM.Font = new System.Drawing.Font("Arial", 8F); this.btOpenCOM.Font = new System.Drawing.Font("Arial", 8F);
this.btOpenCOM.Location = new System.Drawing.Point(311, 130); this.btOpenCOM.Location = new System.Drawing.Point(233, 104);
this.btOpenCOM.Margin = new System.Windows.Forms.Padding(4);
this.btOpenCOM.Name = "btOpenCOM"; this.btOpenCOM.Name = "btOpenCOM";
this.btOpenCOM.Size = new System.Drawing.Size(104, 29); this.btOpenCOM.Size = new System.Drawing.Size(78, 23);
this.btOpenCOM.TabIndex = 71; this.btOpenCOM.TabIndex = 71;
this.btOpenCOM.Text = "Open"; this.btOpenCOM.Text = "Open";
this.btOpenCOM.Click += new System.EventHandler(this.btOpenCOM_Click); this.btOpenCOM.Click += new System.EventHandler(this.btOpenCOM_Click);
...@@ -118,19 +114,17 @@ ...@@ -118,19 +114,17 @@
this.cmbDataBit.Items.AddRange(new object[] { this.cmbDataBit.Items.AddRange(new object[] {
"7", "7",
"8"}); "8"});
this.cmbDataBit.Location = new System.Drawing.Point(236, 98); this.cmbDataBit.Location = new System.Drawing.Point(177, 78);
this.cmbDataBit.Margin = new System.Windows.Forms.Padding(4);
this.cmbDataBit.Name = "cmbDataBit"; this.cmbDataBit.Name = "cmbDataBit";
this.cmbDataBit.Size = new System.Drawing.Size(63, 24); this.cmbDataBit.Size = new System.Drawing.Size(48, 22);
this.cmbDataBit.TabIndex = 68; this.cmbDataBit.TabIndex = 68;
// //
// labStopBit // labStopBit
// //
this.labStopBit.Font = new System.Drawing.Font("Arial", 8F); this.labStopBit.Font = new System.Drawing.Font("Arial", 8F);
this.labStopBit.Location = new System.Drawing.Point(461, 79); this.labStopBit.Location = new System.Drawing.Point(346, 63);
this.labStopBit.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labStopBit.Name = "labStopBit"; this.labStopBit.Name = "labStopBit";
this.labStopBit.Size = new System.Drawing.Size(75, 19); this.labStopBit.Size = new System.Drawing.Size(56, 15);
this.labStopBit.TabIndex = 73; this.labStopBit.TabIndex = 73;
this.labStopBit.Text = "Stop Bit"; this.labStopBit.Text = "Stop Bit";
this.labStopBit.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labStopBit.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -138,10 +132,9 @@ ...@@ -138,10 +132,9 @@
// labParity // labParity
// //
this.labParity.Font = new System.Drawing.Font("Arial", 8F); this.labParity.Font = new System.Drawing.Font("Arial", 8F);
this.labParity.Location = new System.Drawing.Point(313, 79); this.labParity.Location = new System.Drawing.Point(235, 63);
this.labParity.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labParity.Name = "labParity"; this.labParity.Name = "labParity";
this.labParity.Size = new System.Drawing.Size(131, 19); this.labParity.Size = new System.Drawing.Size(98, 15);
this.labParity.TabIndex = 74; this.labParity.TabIndex = 74;
this.labParity.Text = "Parity"; this.labParity.Text = "Parity";
this.labParity.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labParity.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -149,10 +142,9 @@ ...@@ -149,10 +142,9 @@
// labBaud // labBaud
// //
this.labBaud.Font = new System.Drawing.Font("Arial", 8F); this.labBaud.Font = new System.Drawing.Font("Arial", 8F);
this.labBaud.Location = new System.Drawing.Point(124, 79); this.labBaud.Location = new System.Drawing.Point(93, 63);
this.labBaud.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labBaud.Name = "labBaud"; this.labBaud.Name = "labBaud";
this.labBaud.Size = new System.Drawing.Size(100, 19); this.labBaud.Size = new System.Drawing.Size(75, 15);
this.labBaud.TabIndex = 75; this.labBaud.TabIndex = 75;
this.labBaud.Text = "Baudrate"; this.labBaud.Text = "Baudrate";
this.labBaud.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labBaud.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -160,10 +152,9 @@ ...@@ -160,10 +152,9 @@
// labDataBit // labDataBit
// //
this.labDataBit.Font = new System.Drawing.Font("Arial", 8F); this.labDataBit.Font = new System.Drawing.Font("Arial", 8F);
this.labDataBit.Location = new System.Drawing.Point(236, 79); this.labDataBit.Location = new System.Drawing.Point(177, 63);
this.labDataBit.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labDataBit.Name = "labDataBit"; this.labDataBit.Name = "labDataBit";
this.labDataBit.Size = new System.Drawing.Size(64, 19); this.labDataBit.Size = new System.Drawing.Size(48, 15);
this.labDataBit.TabIndex = 76; this.labDataBit.TabIndex = 76;
this.labDataBit.Text = "Data Bit"; this.labDataBit.Text = "Data Bit";
this.labDataBit.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labDataBit.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -180,19 +171,17 @@ ...@@ -180,19 +171,17 @@
"38400", "38400",
"57600", "57600",
"115200"}); "115200"});
this.cmbBaud.Location = new System.Drawing.Point(123, 98); this.cmbBaud.Location = new System.Drawing.Point(92, 78);
this.cmbBaud.Margin = new System.Windows.Forms.Padding(4);
this.cmbBaud.Name = "cmbBaud"; this.cmbBaud.Name = "cmbBaud";
this.cmbBaud.Size = new System.Drawing.Size(100, 24); this.cmbBaud.Size = new System.Drawing.Size(76, 22);
this.cmbBaud.TabIndex = 67; this.cmbBaud.TabIndex = 67;
// //
// labPort // labPort
// //
this.labPort.Font = new System.Drawing.Font("Arial", 8F); this.labPort.Font = new System.Drawing.Font("Arial", 8F);
this.labPort.Location = new System.Drawing.Point(16, 79); this.labPort.Location = new System.Drawing.Point(12, 63);
this.labPort.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labPort.Name = "labPort"; this.labPort.Name = "labPort";
this.labPort.Size = new System.Drawing.Size(100, 19); this.labPort.Size = new System.Drawing.Size(75, 15);
this.labPort.TabIndex = 66; this.labPort.TabIndex = 66;
this.labPort.Text = "COM Port"; this.labPort.Text = "COM Port";
this.labPort.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.labPort.TextAlign = System.Drawing.ContentAlignment.TopCenter;
...@@ -200,32 +189,30 @@ ...@@ -200,32 +189,30 @@
// cmbPort // cmbPort
// //
this.cmbPort.Font = new System.Drawing.Font("Arial", 8F); this.cmbPort.Font = new System.Drawing.Font("Arial", 8F);
this.cmbPort.Location = new System.Drawing.Point(16, 98); this.cmbPort.Location = new System.Drawing.Point(12, 78);
this.cmbPort.Margin = new System.Windows.Forms.Padding(4);
this.cmbPort.Name = "cmbPort"; this.cmbPort.Name = "cmbPort";
this.cmbPort.Size = new System.Drawing.Size(99, 24); this.cmbPort.Size = new System.Drawing.Size(75, 22);
this.cmbPort.TabIndex = 65; this.cmbPort.TabIndex = 65;
// //
// label7 // label7
// //
this.label7.Font = new System.Drawing.Font("Arial", 8F); this.label7.Font = new System.Drawing.Font("Arial", 8F);
this.label7.ForeColor = System.Drawing.Color.Blue; this.label7.ForeColor = System.Drawing.Color.Blue;
this.label7.Location = new System.Drawing.Point(16, 58); this.label7.Location = new System.Drawing.Point(12, 46);
this.label7.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label7.Name = "label7"; this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(187, 21); this.label7.Size = new System.Drawing.Size(140, 17);
this.label7.TabIndex = 64; this.label7.TabIndex = 64;
this.label7.Text = "Connection Status"; this.label7.Text = "Connection Status";
// //
// txtReceiveMessage // txtReceiveMessage
// //
this.txtReceiveMessage.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.txtReceiveMessage.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.txtReceiveMessage.Location = new System.Drawing.Point(563, 14); this.txtReceiveMessage.Location = new System.Drawing.Point(422, 11);
this.txtReceiveMessage.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.txtReceiveMessage.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.txtReceiveMessage.Multiline = true; this.txtReceiveMessage.Multiline = true;
this.txtReceiveMessage.Name = "txtReceiveMessage"; this.txtReceiveMessage.Name = "txtReceiveMessage";
this.txtReceiveMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtReceiveMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtReceiveMessage.Size = new System.Drawing.Size(600, 640); this.txtReceiveMessage.Size = new System.Drawing.Size(509, 529);
this.txtReceiveMessage.TabIndex = 77; this.txtReceiveMessage.TabIndex = 77;
this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged); this.txtReceiveMessage.TextChanged += new System.EventHandler(this.txtReceiveMessage_TextChanged);
// //
...@@ -237,40 +224,35 @@ ...@@ -237,40 +224,35 @@
this.updateops.Controls.Add(this.label13); this.updateops.Controls.Add(this.label13);
this.updateops.Controls.Add(this.UpdateBar); this.updateops.Controls.Add(this.UpdateBar);
this.updateops.Controls.Add(this.UpdateBtn); this.updateops.Controls.Add(this.UpdateBtn);
this.updateops.Location = new System.Drawing.Point(20, 218); this.updateops.Location = new System.Drawing.Point(15, 174);
this.updateops.Margin = new System.Windows.Forms.Padding(4);
this.updateops.Name = "updateops"; this.updateops.Name = "updateops";
this.updateops.Padding = new System.Windows.Forms.Padding(4); this.updateops.Size = new System.Drawing.Size(387, 111);
this.updateops.Size = new System.Drawing.Size(516, 139);
this.updateops.TabIndex = 92; this.updateops.TabIndex = 92;
this.updateops.TabStop = false; this.updateops.TabStop = false;
this.updateops.Text = "更新操作"; this.updateops.Text = "更新操作";
// //
// imagesname // imagesname
// //
this.imagesname.Location = new System.Drawing.Point(75, 88); this.imagesname.Location = new System.Drawing.Point(56, 70);
this.imagesname.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.imagesname.Name = "imagesname"; this.imagesname.Name = "imagesname";
this.imagesname.Size = new System.Drawing.Size(144, 15); this.imagesname.Size = new System.Drawing.Size(108, 12);
this.imagesname.TabIndex = 5; this.imagesname.TabIndex = 5;
this.imagesname.Text = "wtd.bin"; this.imagesname.Text = "wtd.bin";
// //
// label12 // label12
// //
this.label12.AutoSize = true; this.label12.AutoSize = true;
this.label12.Location = new System.Drawing.Point(21, 88); this.label12.Location = new System.Drawing.Point(16, 70);
this.label12.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label12.Name = "label12"; this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(52, 15); this.label12.Size = new System.Drawing.Size(41, 12);
this.label12.TabIndex = 4; this.label12.TabIndex = 4;
this.label12.Text = "镜像:"; this.label12.Text = "镜像:";
// //
// choosebth // choosebth
// //
this.choosebth.Location = new System.Drawing.Point(243, 81); this.choosebth.Location = new System.Drawing.Point(182, 65);
this.choosebth.Margin = new System.Windows.Forms.Padding(4);
this.choosebth.Name = "choosebth"; this.choosebth.Name = "choosebth";
this.choosebth.Size = new System.Drawing.Size(100, 29); this.choosebth.Size = new System.Drawing.Size(75, 23);
this.choosebth.TabIndex = 3; this.choosebth.TabIndex = 3;
this.choosebth.Text = "选择bin"; this.choosebth.Text = "选择bin";
this.choosebth.UseVisualStyleBackColor = true; this.choosebth.UseVisualStyleBackColor = true;
...@@ -279,27 +261,24 @@ ...@@ -279,27 +261,24 @@
// label13 // label13
// //
this.label13.AutoSize = true; this.label13.AutoSize = true;
this.label13.Location = new System.Drawing.Point(21, 32); this.label13.Location = new System.Drawing.Point(16, 26);
this.label13.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label13.Name = "label13"; this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(52, 15); this.label13.Size = new System.Drawing.Size(41, 12);
this.label13.TabIndex = 2; this.label13.TabIndex = 2;
this.label13.Text = "进度:"; this.label13.Text = "进度:";
// //
// UpdateBar // UpdateBar
// //
this.UpdateBar.Location = new System.Drawing.Point(77, 25); this.UpdateBar.Location = new System.Drawing.Point(58, 20);
this.UpdateBar.Margin = new System.Windows.Forms.Padding(4);
this.UpdateBar.Name = "UpdateBar"; this.UpdateBar.Name = "UpdateBar";
this.UpdateBar.Size = new System.Drawing.Size(373, 29); this.UpdateBar.Size = new System.Drawing.Size(280, 23);
this.UpdateBar.TabIndex = 1; this.UpdateBar.TabIndex = 1;
// //
// UpdateBtn // UpdateBtn
// //
this.UpdateBtn.Location = new System.Drawing.Point(351, 81); this.UpdateBtn.Location = new System.Drawing.Point(263, 65);
this.UpdateBtn.Margin = new System.Windows.Forms.Padding(4);
this.UpdateBtn.Name = "UpdateBtn"; this.UpdateBtn.Name = "UpdateBtn";
this.UpdateBtn.Size = new System.Drawing.Size(100, 29); this.UpdateBtn.Size = new System.Drawing.Size(75, 23);
this.UpdateBtn.TabIndex = 0; this.UpdateBtn.TabIndex = 0;
this.UpdateBtn.Text = "一键更新"; this.UpdateBtn.Text = "一键更新";
this.UpdateBtn.UseVisualStyleBackColor = true; this.UpdateBtn.UseVisualStyleBackColor = true;
...@@ -314,10 +293,9 @@ ...@@ -314,10 +293,9 @@
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(44, 446); this.label1.Location = new System.Drawing.Point(33, 357);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(69, 20); this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 93; this.label1.TabIndex = 93;
this.label1.Text = "温度:"; this.label1.Text = "温度:";
// //
...@@ -325,39 +303,35 @@ ...@@ -325,39 +303,35 @@
// //
this.label2.AutoSize = true; this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(44, 487); this.label2.Location = new System.Drawing.Point(33, 390);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(69, 20); this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 94; this.label2.TabIndex = 94;
this.label2.Text = "水位:"; this.label2.Text = "水位:";
// //
// label3 // label3
// //
this.label3.AutoSize = true; this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(108, 451); this.label3.Location = new System.Drawing.Point(81, 361);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3"; this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(55, 15); this.label3.Size = new System.Drawing.Size(41, 12);
this.label3.TabIndex = 95; this.label3.TabIndex = 95;
this.label3.Text = "label3"; this.label3.Text = "label3";
// //
// label4 // label4
// //
this.label4.AutoSize = true; this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(108, 492); this.label4.Location = new System.Drawing.Point(81, 394);
this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label4.Name = "label4"; this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(55, 15); this.label4.Size = new System.Drawing.Size(41, 12);
this.label4.TabIndex = 96; this.label4.TabIndex = 96;
this.label4.Text = "label4"; this.label4.Text = "label4";
// //
// button2 // button2
// //
this.button2.Location = new System.Drawing.Point(371, 476); this.button2.Location = new System.Drawing.Point(278, 381);
this.button2.Margin = new System.Windows.Forms.Padding(4);
this.button2.Name = "button2"; this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(100, 29); this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 97; this.button2.TabIndex = 97;
this.button2.Text = "test"; this.button2.Text = "test";
this.button2.UseVisualStyleBackColor = true; this.button2.UseVisualStyleBackColor = true;
...@@ -365,10 +339,9 @@ ...@@ -365,10 +339,9 @@
// //
// SubscribeBut // SubscribeBut
// //
this.SubscribeBut.Location = new System.Drawing.Point(371, 432); this.SubscribeBut.Location = new System.Drawing.Point(278, 346);
this.SubscribeBut.Margin = new System.Windows.Forms.Padding(4);
this.SubscribeBut.Name = "SubscribeBut"; this.SubscribeBut.Name = "SubscribeBut";
this.SubscribeBut.Size = new System.Drawing.Size(100, 29); this.SubscribeBut.Size = new System.Drawing.Size(75, 23);
this.SubscribeBut.TabIndex = 98; this.SubscribeBut.TabIndex = 98;
this.SubscribeBut.Text = "订阅消息"; this.SubscribeBut.Text = "订阅消息";
this.SubscribeBut.UseVisualStyleBackColor = true; this.SubscribeBut.UseVisualStyleBackColor = true;
...@@ -377,26 +350,28 @@ ...@@ -377,26 +350,28 @@
// label5 // label5
// //
this.label5.AutoSize = true; this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(111, 528); this.label5.Location = new System.Drawing.Point(83, 422);
this.label5.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label5.Name = "label5"; this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(55, 15); this.label5.Size = new System.Drawing.Size(41, 12);
this.label5.TabIndex = 99; this.label5.TabIndex = 99;
this.label5.Text = "label5"; this.label5.Text = "label5";
// //
// label6 // label6
// //
this.label6.AutoSize = true; this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(23, 528); this.label6.Location = new System.Drawing.Point(17, 422);
this.label6.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label6.Name = "label6"; this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(82, 15); this.label6.Size = new System.Drawing.Size(65, 12);
this.label6.TabIndex = 100; this.label6.TabIndex = 100;
this.label6.Text = "超时次数:"; this.label6.Text = "超时次数:";
// //
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1184, 672); this.ClientSize = new System.Drawing.Size(942, 551);
this.Controls.Add(this.label6); this.Controls.Add(this.label6);
this.Controls.Add(this.label5); this.Controls.Add(this.label5);
this.Controls.Add(this.SubscribeBut); this.Controls.Add(this.SubscribeBut);
...@@ -421,7 +396,6 @@ ...@@ -421,7 +396,6 @@
this.Controls.Add(this.cmbPort); this.Controls.Add(this.cmbPort);
this.Controls.Add(this.label7); this.Controls.Add(this.label7);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "Form1"; this.Name = "Form1";
this.Text = "牧场项目"; this.Text = "牧场项目";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
......
...@@ -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
...@@ -112,11 +113,11 @@ namespace ModbusDemo ...@@ -112,11 +113,11 @@ namespace ModbusDemo
mqttTopic = "Witium/WTDS78X/" + mqttSn + "/Data"; mqttTopic = "Witium/WTDS78X/" + mqttSn + "/Data";
if (Debug_test == true) if (Debug_test == true)
{ {
mqttIp = "172.16.1.24"; mqttIp = "47.101.50.24";
mqttClientId = GetTimeStamp() + "sxz"; mqttClientId = GetTimeStamp() + "sxz";
mqttPort = 1883; mqttPort = 1883;
mqttUsername = "pasture"; mqttUsername = "root";
mqttPwd = "Pasture37774020"; mqttPwd = "public";
} }
else else
{ {
...@@ -488,7 +489,7 @@ namespace ModbusDemo ...@@ -488,7 +489,7 @@ namespace ModbusDemo
{ {
control(ctrl.ss[0].getAddr(), address, on_off);//根据下发确定控制通道 control(ctrl.ss[0].getAddr(), address, on_off);//根据下发确定控制通道
mqttData_Back = JsonConvert.SerializeObject(ctrl2); mqttData_Back = JsonConvert.SerializeObject(ctrl2);
Modbus_polling(); //Modbus_polling();
await Publish(); await Publish();
await Back(); await Back();
} }
...@@ -524,7 +525,7 @@ namespace ModbusDemo ...@@ -524,7 +525,7 @@ namespace ModbusDemo
{ {
control(ID, address, false);//根据下发确定控制通道 control(ID, address, false);//根据下发确定控制通道
control(ID, (ushort)(address+1), false);//根据下发确定控制通道 control(ID, (ushort)(address+1), false);//根据下发确定控制通道
Modbus_polling(); //Modbus_polling();
await Publish(); await Publish();
time = 0; time = 0;
n = 0; n = 0;
...@@ -539,7 +540,7 @@ namespace ModbusDemo ...@@ -539,7 +540,7 @@ namespace ModbusDemo
private void GetData() private void GetData()
{ {
timer.Interval = 9000; timer.Interval = 120000;
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 = 120500;
MQTTTimer.Enabled = true; MQTTTimer.Enabled = true;
MQTTTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true); MQTTTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
MQTTTimer.Start(); MQTTTimer.Start();
...@@ -560,8 +561,8 @@ namespace ModbusDemo ...@@ -560,8 +561,8 @@ namespace ModbusDemo
{ {
try try
{ {
label5.Text =ModbusComm.usCommunFailTimes.ToString();
await Publish(); await Publish();
totxt.Log(mqttData + Environment.NewLine + "\n");
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -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("执行结束");
}); });
} }
...@@ -609,12 +613,12 @@ namespace ModbusDemo ...@@ -609,12 +613,12 @@ namespace ModbusDemo
else else
{ {
//txtbox.Text = message; //txtbox.Text = message;
totxt.Log(message); //totxt.Log(message);
} }
} }
public void Modbus_ReadHoldingRegistersTask(out ushort[] OutputValue, byte slaveAddress, ushort startAddress, ushort numberOfPoints, out int Caution) public void Modbus_ReadHoldingRegistersTask(out ushort[] OutputValue, byte slaveAddress, ushort startAddress, ushort numberOfPoints, out int Caution)
{ {
label5.Text= usCommunFailTimes.ToString(); label5.Text = usCommunFailTimes.ToString();
try try
{ {
OutputValue = master.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints); OutputValue = master.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
...@@ -711,7 +715,7 @@ namespace ModbusDemo ...@@ -711,7 +715,7 @@ namespace ModbusDemo
timer.Stop(); timer.Stop();
Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message); Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
totxt.Log(DateTime.Now.ToString() + " " + exception.Message); totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
OutputValue = new bool [numberOfPoints]; OutputValue = new bool[numberOfPoints];
Caution = -1; Caution = -1;
usCommunFailTimes++; usCommunFailTimes++;
} }
...@@ -856,227 +860,172 @@ namespace ModbusDemo ...@@ -856,227 +860,172 @@ namespace ModbusDemo
Caution = -1; Caution = -1;
} }
} }
private async void Modbus_polling() //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); // //float EastTemperature = 0;
EastTemperature = float.Parse(vs[0]); // //float Easternwaterlevel = 0;
s = Analog.Value(register, Inputtype); // //float WestWaterLevel = 0;
//(实时电流值 - 设定电流最小值) /(20Ma - 设定电流最小值) *设定测量最大值 // //int sign = 0;
//(实时电流值 - 4ma) /(20ma - 4ma) *设定测量最大值(也就是设定的满量程) // //try
// //{
Easternwaterlevel = (float.Parse(s[0]) - 4) / 16 * 6; // // ushort[] temperature = { };
s6 = Analog.Value(registerN, InputtypeN); // // ushort[] type = { };
WestWaterLevel = (float.Parse(s6[0]) - 4) / 16 * 6; // // string[] vs = { };
label3.Text = EastTemperature.ToString(); // // string[] s = { };
label4.Text = Easternwaterlevel.ToString(); // // string[] s6 = { };
EOnOff = state.IO(EIO);////东中转池 // // ushort[] register = { };
SOnOff = state.IO(SIO);////西中转池 // // ushort[] Inputtype = { };
WOnOff = state.IO(WIO); ////东南北水 // // 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)
// // + "]";
// //}
WTDR14P x = new WTDR14P("27", EastTemperature, 0, 0, 0, time_1); // //catch (Exception exception)
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); // //Connection exception
// //No response from server.
//东搅拌 // //The server maybe close the com port, or response timeout.
WTDR66C EastStir = new WTDR66C("40", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp()); // // if (exception.Source.Equals("System"))
//东南北水 // // {
WTDR66C WestStir = new WTDR66C("41", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp()); // // timer.Stop();
//西搅拌 // // Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message);
WTDR66C SouthWater = new WTDR66C("19", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp()); // // totxt.Log(DateTime.Now.ToString() + " " + exception.Message);
// // return;
mqttData = "[" + // // }
JsonConvert.SerializeObject(x) + "," + // // //The server return error code.
JsonConvert.SerializeObject(p) + "," + // // //You can get the function code and exception code.
JsonConvert.SerializeObject(q) + "," + // // if (exception.Source.Equals("nModbusPC"))
JsonConvert.SerializeObject(EastStir) + "," + // // {
JsonConvert.SerializeObject(WestStir) + "," + // // string str = exception.Message;
JsonConvert.SerializeObject(SouthWater) // // int FunctionCode;
+ "]"; // // string ExceptionCode;
}
// // str = str.Remove(0, str.IndexOf("\r\n") + 17);
// Modbus_ReadHoldingRegistersTask(out temperature, 1, 0, 4, out int signT); // // FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n")));
// Modbus_ReadHoldingRegistersTask(out type, 1, 10, 4, out sign); // // Console.WriteLine("Function Code: " + FunctionCode.ToString("X"));
// UInt64 time_1 = GetTimeStamp(); // // totxt.Log("Function Code: " + FunctionCode.ToString("X"));
// Modbus_ReadHoldingRegistersTask(out register, 3, 0, 8, out int signr); // // //MessageBox.Show("Function Code: " + FunctionCode.ToString("X"));
// Modbus_ReadHoldingRegistersTask(out Inputtype, 3, 10, 8, out int signI);
// UInt64 time_2 = GetTimeStamp(); // // str = str.Remove(0, str.IndexOf("\r\n") + 17);
// Modbus_ReadHoldingRegistersTask(out registerN, 6, 0, 8, out int signre); // // ExceptionCode = str.Remove(str.IndexOf("-"));
// Modbus_ReadHoldingRegistersTask(out InputtypeN, 6, 10, 8, out int signIn); // // switch (ExceptionCode.Trim())
// UInt64 time_3 = GetTimeStamp(); // // {
// Modbus_ReadCoilsTask(out EIO, 5, 16, 6, out int signE); // // case "1":
// // Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
// Modbus_ReadCoilsTask(out SIO, 8, 16, 6, out int signS); // // //MessageBox.Show("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
// // totxt.Log("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!");
// Modbus_ReadCoilsTask(out WIO, 4, 16, 6, out int signW); // // break;
// // case "2":
// if (sign == -1) // // 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;
// //else // // 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
// //{ // //{
// 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); // return;
// WTDR18X p = new WTDR18X("3", Easternwaterlevel, 0, 0, 0, 0, 0, 0, 0, time_2);
// WTDR18X q = new WTDR18X("6", WestWaterLevel, 0, 0, 0, 0, 0, 0, 0, time_3);
// //东搅拌
// WTDR66C EastStir = new WTDR66C("5", EOnOff[0], EOnOff[1], EOnOff[2], EOnOff[3], EOnOff[4], EOnOff[5], GetTimeStamp());
// //东南北水
// WTDR66C WestStir = new WTDR66C("4", WOnOff[0], WOnOff[1], WOnOff[2], WOnOff[3], WOnOff[4], WOnOff[5], GetTimeStamp());
// //西搅拌
// WTDR66C SouthWater = new WTDR66C("8", SOnOff[0], SOnOff[1], SOnOff[2], SOnOff[3], SOnOff[4], SOnOff[5], GetTimeStamp());
// mqttData = "[" +
// JsonConvert.SerializeObject(x) + "," +
// JsonConvert.SerializeObject(p) + "," +
// JsonConvert.SerializeObject(q) + "," +
// JsonConvert.SerializeObject(EastStir) + "," +
// JsonConvert.SerializeObject(WestStir) + "," +
// JsonConvert.SerializeObject(SouthWater)
// + "]";
//} //}
catch (Exception exception)
{
//Connection exception
//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字符串转对象
...@@ -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)
...@@ -1642,16 +1591,16 @@ namespace ModbusDemo ...@@ -1642,16 +1591,16 @@ namespace ModbusDemo
private void btOpenCOM_Click(object sender, EventArgs e) private void btOpenCOM_Click(object sender, EventArgs e)
{ {
//if (Debug_test == true) if (Debug_test == true)
//{ {
// comPort.PortName = "COM2"; comPort.PortName = "COM2";
// comPort.BaudRate = 9600; comPort.BaudRate = 9600;
// comPort.Parity = Parity.None; comPort.Parity = Parity.None;
// comPort.StopBits = StopBits.One; comPort.StopBits = StopBits.One;
// comPort.DataBits = 8; comPort.DataBits = 8;
//} }
//else else
//{ {
if (cmbPort.Text == "") if (cmbPort.Text == "")
{ {
MessageBox.Show("串口打开错误"); MessageBox.Show("串口打开错误");
...@@ -1680,7 +1629,7 @@ namespace ModbusDemo ...@@ -1680,7 +1629,7 @@ namespace ModbusDemo
{ {
comPort.StopBits = StopBits.One; comPort.StopBits = StopBits.One;
} }
//} }
try try
{ {
comPort.Open(); comPort.Open();
...@@ -1691,11 +1640,14 @@ namespace ModbusDemo ...@@ -1691,11 +1640,14 @@ 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); ModbusComm.Modbus = ModbusSerialMaster.CreateRtu(comPort);
master.Transport.Retries = 0; //重试次数 ModbusComm.Modbus.Transport.Retries = 0;
master.Transport.ReadTimeout = 1000; //读取串口数据超时时间(ms) ModbusComm.Modbus.Transport.ReadTimeout = 1000;
master.Transport.WriteTimeout = 1000;//写入串口数据超时时间(ms) ModbusComm.Modbus.Transport.WriteTimeout = 1000;
master.Transport.WaitToRetryMilliseconds = 0;//重试间隔(ms) 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