using HslCommunication; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Threading; using System.Windows.Forms; namespace C_Windows_1 { public partial class TestPara : Form { int stage; public TestPara() { InitializeComponent(); } private void TestPara_Load(object sender, EventArgs e) { try { //Form1.f1.Timer1.Stop(); //Form1.f1.SerialPort1.Close(); //ParaNum.SelectedIndex = 0;//Form1.f1.WorkStation .SelectedIndex ; //SerialPort1.PortName = Form1.f1.SerialPort1.PortName; //SerialPort1.BaudRate = Form1.f1.SerialPort1.BaudRate; //SerialPort1.DataBits = 8; //SerialPort1.StopBits = System.IO.Ports.StopBits.One; //SerialPort1.Parity = System.IO.Ports.Parity.None; //SerialPort1.Open(); cb_yiqi.SelectedIndex = 0; cb_ParamID.SelectedIndex = 0; label37.Visible = false; tb_ParaName.Visible = false; //ReadParameters(1); ReadParam("1", "1"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public static byte[] XdrFloat(float value) { byte[] bytes = BitConverter.GetBytes(value); if (BitConverter.IsLittleEndian) Array.Reverse(bytes); { Byte BYTE1; BYTE1 = bytes[0]; bytes[0] = bytes[2]; bytes[2] = BYTE1; BYTE1 = bytes[1]; bytes[1] = bytes[3]; bytes[3] = BYTE1; return bytes; } } public OperateResult operateResult24; public static bool Delay(int delayTime) { DateTime now = DateTime.Now; int s; do { TimeSpan spand = DateTime.Now - now; s = spand.Seconds;//延时秒数 Application.DoEvents(); } while (s < delayTime); return true; } // 上传参数按钮 private void BtnUpload_Click(object sender, EventArgs e) { try { // 保存参数-本地 //int p = Convert.ToInt32(cb_ParamID.Text); //SetParameters(p); SaveParam(cb_yiqi.Text, cb_ParamID.Text); tb_BigBalanTime.Text = "0"; tb_SmallBalanTime.Text = "0"; if (tb_FullTime.Text == "" || tb_BigBalanTime.Text == "" || tb_SmallBalanTime.Text == "" || tb_TestTime1.Text == "" || tb_ExhaustTime.Text == "" || tb_BypassTime.Text == "" || tb_DelayTime1.Text == "" || tb_DelayTime2.Text == "" || tb_BigLeaklow.Text == "" || tb_BigLeaktop.Text == "" || cb_LUnit.Text == "" || cb_PUnit.Text == "") { MessageBox.Show("输入参数格式不对"); } else { if (tb_FullTime.Text == "" || tb_BigBalanTime.Text == "" || tb_SmallBalanTime.Text == "" || tb_TestTime1.Text == "" || tb_ExhaustTime.Text == "" || tb_BypassTime.Text == "" || tb_DelayTime1.Text == "" || tb_DelayTime2.Text == "" || tb_BigLeaklow.Text == "" || tb_BigLeaktop.Text == "" || cb_LUnit.Text == "" || cb_PUnit.Text == "") { MessageBox.Show("输入参数格式不对"); } else { BtnUpload.Text = "正在上传"; BtnUpload.ForeColor = Color.Red; if(cb_yiqi.Text == "1") { short a = Convert.ToInt16(Convert.ToDouble(tb_FullTime.Text) * 10);//充气时间 Form1.f1.CH1.Write("25", a); //平衡时间不能写入,为大漏平衡时间和微漏平衡时间的和 //short b = Convert.ToInt16(Convert.ToDouble(BBalanTime.Text) * 10);//平衡时间 short b = Convert.ToInt16(Convert.ToDouble(tb_BalanTime.Text) * 10); Form1.f1.CH1.Write("34", b); short c = Convert.ToInt16(Convert.ToDouble(tb_TestTime1.Text) * 10);//检测时间1 Form1.f1.CH1.Write("27", c); short d = Convert.ToInt16(Convert.ToDouble(tb_ExhaustTime.Text) * 10);//排气时间 Form1.f1.CH1.Write("28", d); short j = Convert.ToInt16(Convert.ToDouble(tb_BypassTime.Text) * 10);//旁路时间 Form1.f1.CH1.Write("29", j); short f = Convert.ToInt16(Convert.ToDouble(tb_DelayTime1.Text) * 10);//延时1 Form1.f1.CH1.Write("30", f); short g = Convert.ToInt16(Convert.ToDouble(tb_DelayTime2.Text) * 10);//延时2 Form1.f1.CH1.Write("31", g); short h = Convert.ToInt16(Convert.ToDouble(tb_BigLeaktop.Text));//大漏压差上限 Form1.f1.CH1.Write("36", h); short i = Convert.ToInt16(Convert.ToDouble(tb_BigLeaklow.Text));//大漏压差下限 Form1.f1.CH1.Write("37", i); //BLeakLPre List parmEvolume = new List();//等效容积 parmEvolume.AddRange(XdrFloat(Convert.ToSingle(tb_Evolume.Text))); byte[] pEvolume = parmEvolume.ToArray(); Form1.f1.CH1.Write("42", pEvolume); List parmFPtoplimit = new List();//充气压力上限 parmFPtoplimit.AddRange(XdrFloat(Convert.ToSingle(tb_FPtoplimit.Text))); byte[] toplimit = parmFPtoplimit.ToArray(); Form1.f1.CH1.Write("32", toplimit); List parmFPlowlimit = new List();//充气压力下限 parmFPlowlimit.AddRange(XdrFloat(Convert.ToSingle(tb_FPlowlimit.Text))); byte[] lowlimit = parmFPlowlimit.ToArray(); Form1.f1.CH1.Write("46", lowlimit); //short BBaTime = Convert.ToInt16(Convert.ToDouble(BBalanTime.Text) * 10);//大漏平衡时间 //Form1.f1.CH1.Write("34", BBaTime); //short SBaTime = Convert.ToInt16(Convert.ToDouble(SBalanTime.Text) * 10);//微漏平衡时间 //Form1.f1.CH1.Write("35", SBaTime); List parmLeaktoplimit = new List();//泄露量上限 parmLeaktoplimit.AddRange(XdrFloat(Convert.ToSingle(tb_Leaktoplimit.Text))); byte[] Leaktop = parmLeaktoplimit.ToArray(); Form1.f1.CH1.Write("38", Leaktop); List parmLeaklowlimit = new List();//泄露量下限 parmLeaklowlimit.AddRange(XdrFloat(Convert.ToSingle(tb_Leaklowlimit.Text))); byte[] Leaklow = parmLeaklowlimit.ToArray(); Form1.f1.CH1.Write("44", Leaklow); //单位 ushort punit1 = Convert.ToUInt16(cb_PUnit.SelectedIndex); ushort lunit1 = Convert.ToUInt16(cb_LUnit.SelectedIndex); Form1.f1.CH1.Write("41", punit1); Form1.f1.CH1.Write("40", lunit1); Form1.f1.CH1.WriteCoil("36", true); Thread.Sleep(500); Form1.f1.CH1.WriteCoil("38", true); Thread.Sleep(500); Form1.f1.CH1.WriteCoil("36", false); Thread.Sleep(500); Form1.f1.CH1.WriteCoil("38", false); } else if (cb_yiqi.Text == "2") { short a = Convert.ToInt16(Convert.ToDouble(tb_FullTime.Text) * 10);//充气时间 Form1.f1.CH2.Write("25", a); //平衡时间不能写入,为大漏平衡时间和微漏平衡时间的和 //short b = Convert.ToInt16(Convert.ToDouble(BBalanTime.Text) * 10);//平衡时间 short b = Convert.ToInt16(Convert.ToDouble(tb_BalanTime.Text) * 10); Form1.f1.CH2.Write("34", b); short c = Convert.ToInt16(Convert.ToDouble(tb_TestTime1.Text) * 10);//检测时间1 Form1.f1.CH2.Write("27", c); short d = Convert.ToInt16(Convert.ToDouble(tb_ExhaustTime.Text) * 10);//排气时间 Form1.f1.CH2.Write("28", d); short j = Convert.ToInt16(Convert.ToDouble(tb_BypassTime.Text) * 10);//旁路时间 Form1.f1.CH2.Write("29", j); short f = Convert.ToInt16(Convert.ToDouble(tb_DelayTime1.Text) * 10);//延时1 Form1.f1.CH2.Write("30", f); short g = Convert.ToInt16(Convert.ToDouble(tb_DelayTime2.Text) * 10);//延时2 Form1.f1.CH2.Write("31", g); short h = Convert.ToInt16(Convert.ToDouble(tb_BigLeaktop.Text));//大漏压差上限 Form1.f1.CH2.Write("36", h); short i = Convert.ToInt16(Convert.ToDouble(tb_BigLeaklow.Text));//大漏压差下限 Form1.f1.CH2.Write("37", i); //BLeakLPre List parmEvolume = new List();//等效容积 parmEvolume.AddRange(XdrFloat(Convert.ToSingle(tb_Evolume.Text))); byte[] pEvolume = parmEvolume.ToArray(); Form1.f1.CH2.Write("42", pEvolume); List parmFPtoplimit = new List();//充气压力上限 parmFPtoplimit.AddRange(XdrFloat(Convert.ToSingle(tb_FPtoplimit.Text))); byte[] toplimit = parmFPtoplimit.ToArray(); Form1.f1.CH2.Write("32", toplimit); List parmFPlowlimit = new List();//充气压力下限 parmFPlowlimit.AddRange(XdrFloat(Convert.ToSingle(tb_FPlowlimit.Text))); byte[] lowlimit = parmFPlowlimit.ToArray(); Form1.f1.CH2.Write("46", lowlimit); //short BBaTime = Convert.ToInt16(Convert.ToDouble(BBalanTime.Text) * 10);//大漏平衡时间 //Form1.f1.CH2.Write("34", BBaTime); //short SBaTime = Convert.ToInt16(Convert.ToDouble(SBalanTime.Text) * 10);//微漏平衡时间 //Form1.f1.CH2.Write("35", SBaTime); List parmLeaktoplimit = new List();//泄露量上限 parmLeaktoplimit.AddRange(XdrFloat(Convert.ToSingle(tb_Leaktoplimit.Text))); byte[] Leaktop = parmLeaktoplimit.ToArray(); Form1.f1.CH2.Write("38", Leaktop); List parmLeaklowlimit = new List();//泄露量下限 parmLeaklowlimit.AddRange(XdrFloat(Convert.ToSingle(tb_Leaklowlimit.Text))); byte[] Leaklow = parmLeaklowlimit.ToArray(); Form1.f1.CH2.Write("44", Leaklow); //单位 ushort punit1 = Convert.ToUInt16(cb_PUnit.SelectedIndex); ushort lunit1 = Convert.ToUInt16(cb_LUnit.SelectedIndex); Form1.f1.CH2.Write("41", punit1); Form1.f1.CH2.Write("40", lunit1); Form1.f1.CH2.WriteCoil("36", true); Thread.Sleep(500); Form1.f1.CH2.WriteCoil("38", true); Thread.Sleep(500); Form1.f1.CH2.WriteCoil("36", false); Thread.Sleep(500); Form1.f1.CH2.WriteCoil("38", false); } Thread.Sleep(500); //Delay(1); MessageBox.Show("上传参数成功"); BtnUpload.Text = "上传参数"; BtnUpload.ForeColor = Color.Black; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Timer5_Tick(object sender, EventArgs e) { //try //{ // int a = Convert.ToInt32(BLeakTPre.Text); // int b = Convert.ToInt32(BLeakLPre.Text); // string a1 = a.ToString("x4"); // string b1 = b.ToString("x4"); // if (b1.Length == 8) // { // b1 = b1.Substring(4, 4); // } // byte[] ltl1 = BitConverter.GetBytes(Convert.ToSingle(Leaktoplimit.Text)); // string ltl2 = BitConverter.ToString(ltl1.Reverse().ToArray()).Replace("-", ""); // string leaktoplim = ltl2.Substring(4, 4) + ltl2.Substring(0, 4); // string lunit = LUnit.SelectedIndex.ToString("x4"); // string punit = PUnit.SelectedIndex.ToString("x4"); // byte[] ev1 = BitConverter.GetBytes(Convert.ToSingle(Evolume.Text)); // string ev2 = BitConverter.ToString(ev1.Reverse().ToArray()).Replace("-", ""); // string evol = ev2.Substring(4, 4) + ev2.Substring(0, 4); // byte[] lllim1 = BitConverter.GetBytes(Convert.ToSingle(Leaklowlimit.Text)); // string lllim2 = BitConverter.ToString(lllim1.Reverse().ToArray()).Replace("-", ""); // string leaklowlim = lllim2.Substring(4, 4) + lllim2.Substring(0, 4); // byte[] fpllim1 = BitConverter.GetBytes(Convert.ToSingle(FPlowlimit.Text)); // string fpllim2 = BitConverter.ToString(fpllim1.Reverse().ToArray()).Replace("-", ""); // string fplowlim = fpllim2.Substring(4, 4) + fpllim2.Substring(0, 4); // //检测时间2对应的地址为0030.在此处写入 // int ttime2 = Convert.ToInt32(TestTime2.Text) * 10; // string testtime2 = ttime2.ToString("x4"); // SendText.Text = Form1.f1.Station.Text + " 10 00 24 00 0D 1A"; // SendText.Text += a1; // SendText.Text += b1; // SendText.Text += leaktoplim; // SendText.Text += lunit; // SendText.Text += punit; // SendText.Text += evol; // SendText.Text += leaklowlim; // SendText.Text += fplowlim; // SendText.Text += testtime2; // Send(); // stage = 10; // Timer5.Stop(); // Timer6.Interval = 1000; // Timer6.Start(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} } private void Timer6_Tick(object sender, EventArgs e) { //try //{ // BtnUpload.Text = "上传参数"; // BtnUpload.ForeColor = Color.Black; // Timer6.Stop(); //} //catch //{ // Timer6.Stop(); //} } // 读取参数按钮 private void BtnRead_Click(object sender, EventArgs e) { try { Parameters(cb_yiqi.Text); MessageBox.Show("参数读取成功"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void Booltomath(int sta, UInt16[] shangweiji) { try { OperateResult reslutget; reslutget = Form1.f1.CH1.ReadBool(Convert.ToString(sta)); if (reslutget.IsSuccess == true) { if (reslutget.Content == true) { shangweiji[sta] = 1; } else { shangweiji[sta] = 0; } } } catch (Exception) { throw; } } public void Booltomath2(int sta, UInt16[] shangweiji) { try { OperateResult reslutget; reslutget = Form1.f1.CH2.ReadBool(Convert.ToString(sta)); if (reslutget.IsSuccess == true) { if (reslutget.Content == true) { shangweiji[sta] = 1; } else { shangweiji[sta] = 0; } } } catch (Exception) { throw; } } public short Digitalconversion(ushort readdata) { int Z; if (readdata > 32768) Z = (int)readdata - 65536; else Z = readdata; return (short)Z; } // 读取参数 public void Parameters(string CH) { if(CH == "1") { Form1.f1.Modbusread(25, 24, Form1.ReadData); Form1.f1.Modbusread(604, 2, Form1.ReadData); Booltomath(604, Form1.ReadData); Booltomath(605, Form1.ReadData); tb_FullTime.Text = ((Form1.ReadData[25] / 10).ToString()); tb_BalanTime.Text = ((Form1.ReadData[34] / 10).ToString()); tb_TestTime1.Text = (Form1.ReadData[27] / 10).ToString(); tb_TestTime2.Text = ((Form1.ReadData[48] / 10).ToString()); tb_ExhaustTime.Text = (Form1.ReadData[28] / 10).ToString(); tb_BypassTime.Text = (Form1.ReadData[29] / 10).ToString(); tb_DelayTime1.Text = (Form1.ReadData[30] / 10).ToString(); tb_DelayTime2.Text = ((Form1.ReadData[31] / 10).ToString()); tb_Evolume.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[43], Form1.ReadData[42]).ToString(); //参数名称 tb_FPtoplimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[33], Form1.ReadData[32]).ToString(); tb_FPlowlimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[47], Form1.ReadData[46]).ToString(); tb_BigBalanTime.Text = ((Form1.ReadData[34] / 10).ToString()); tb_SmallBalanTime.Text = ((Form1.ReadData[35] / 10).ToString()); tb_BigLeaktop.Text = ((Form1.ReadData[36]).ToString()); tb_BigLeaklow.Text = ((Digitalconversion(Form1.ReadData[37])).ToString()); tb_Leaktoplimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[39], Form1.ReadData[38]).ToString(); tb_Leaklowlimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData[45], Form1.ReadData[44]).ToString(); cb_PUnit.SelectedIndex = Form1.ReadData[41]; cb_LUnit.SelectedIndex = Form1.ReadData[40]; lb_PressureUnit.Text = lb_PressureUnit2.Text = cb_PUnit.Text; lb_LeakUnit.Text = lb_LeakUnit2.Text = cb_LUnit.Text; if (Form1.ReadData[604] == 1) { cb_TestType.SelectedIndex = 0; } if (Form1.ReadData[605] == 1) { cb_TestType.SelectedIndex = 1; } } if (CH == "2") { Form1.f1.Modbusread2(25, 24, Form1.ReadData2); Form1.f1.Modbusread2(604, 2, Form1.ReadData2); Booltomath2(604, Form1.ReadData2); Booltomath2(605, Form1.ReadData2); tb_FullTime.Text = ((Form1.ReadData2[25] / 10).ToString()); tb_BalanTime.Text = ((Form1.ReadData2[34] / 10).ToString()); tb_TestTime1.Text = (Form1.ReadData2[27] / 10).ToString(); tb_TestTime2.Text = ((Form1.ReadData2[48] / 10).ToString()); tb_ExhaustTime.Text = (Form1.ReadData2[28] / 10).ToString(); tb_BypassTime.Text = (Form1.ReadData2[29] / 10).ToString(); tb_DelayTime1.Text = (Form1.ReadData2[30] / 10).ToString(); tb_DelayTime2.Text = ((Form1.ReadData2[31] / 10).ToString()); tb_Evolume.Text = Form1.TwoUInt16ToFloat(Form1.ReadData2[43], Form1.ReadData2[42]).ToString(); //参数名称 tb_FPtoplimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData2[33], Form1.ReadData2[32]).ToString(); tb_FPlowlimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData2[47], Form1.ReadData2[46]).ToString(); tb_BigBalanTime.Text = ((Form1.ReadData2[34] / 10).ToString()); tb_SmallBalanTime.Text = ((Form1.ReadData2[35] / 10).ToString()); tb_BigLeaktop.Text = ((Form1.ReadData2[36]).ToString()); tb_BigLeaklow.Text = ((Digitalconversion(Form1.ReadData2[37])).ToString()); tb_Leaktoplimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData2[39], Form1.ReadData2[38]).ToString(); tb_Leaklowlimit.Text = Form1.TwoUInt16ToFloat(Form1.ReadData2[45], Form1.ReadData2[44]).ToString(); cb_PUnit.SelectedIndex = Form1.ReadData2[41]; cb_LUnit.SelectedIndex = Form1.ReadData2[40]; lb_PressureUnit.Text = lb_PressureUnit2.Text = cb_PUnit.Text; lb_LeakUnit.Text = lb_LeakUnit2.Text = cb_LUnit.Text; if (Form1.ReadData2[604] == 1) { cb_TestType.SelectedIndex = 0; } if (Form1.ReadData2[605] == 1) { cb_TestType.SelectedIndex = 1; } } } private void BtnKeep_Click(object sender, EventArgs e) { } private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { System.Threading.Thread.Sleep(100); int len = SerialPort1.BytesToRead;//获取可以读取的字节数 if (len > 1) { byte[] buff = new byte[len];//创建缓存数据数组 SerialPort1.Read(buff, 0, len);//把数据读取到buff数组 //string str = Encoding.Default.GetString(buff); Invoke((new System.Action(() => //串口接收事件会自动创建线程 { ReceiveText.Clear(); ReceiveText.Text = ByteToHexStr(buff); SerialPort1.DiscardInBuffer(); Stagenum(); } ))); } } //十六进制接收 public static string ByteToHexStr(byte[] bytes) { string returnStr = ""; try { if (bytes != null) { for (int i = 0; i < bytes.Length; i++) { returnStr += bytes[i].ToString("X2");//每个字节转换成两位十六进制 // returnStr += " ";//两个16进制用空格隔开,方便看数据 } } return returnStr; } catch (Exception) { return returnStr; } } private void Stagenum() { switch (stage) { case 2://此时为读取参数并数据转换 string str2; str2 = ReceiveText.Text; // str2 = str2.Replace(" ", ""); string pname1 = str2.Substring(8, 2); string pname2 = str2.Substring(6, 2); string pname3 = str2.Substring(12, 2); string pname4 = str2.Substring(10, 2); string pname5 = str2.Substring(16, 2); string pname6 = str2.Substring(14, 2); string pname7 = str2.Substring(20, 2); string pname8 = str2.Substring(18, 2); string pname9 = str2.Substring(24, 2); string pname10 = str2.Substring(22, 2); UInt32 pn1 = Convert.ToUInt32(pname1, 16); byte[] p1 = System.BitConverter.GetBytes(pn1); string paraname1 = System.Text.ASCIIEncoding.ASCII.GetString(p1);//字节数组转成对应的ASCII码 UInt32 pn2 = Convert.ToUInt32(pname2, 16); byte[] p2 = System.BitConverter.GetBytes(pn2); string paraname2 = System.Text.ASCIIEncoding.ASCII.GetString(p2); UInt32 pn3 = Convert.ToUInt32(pname3, 16); byte[] p3 = System.BitConverter.GetBytes(pn3); string paraname3 = System.Text.ASCIIEncoding.ASCII.GetString(p3); UInt32 pn4 = Convert.ToUInt32(pname4, 16); byte[] p4 = System.BitConverter.GetBytes(pn4); string paraname4 = System.Text.ASCIIEncoding.ASCII.GetString(p4); UInt32 pn5 = Convert.ToUInt32(pname5, 16); byte[] p5 = System.BitConverter.GetBytes(pn5); string paraname5 = System.Text.ASCIIEncoding.ASCII.GetString(p5); UInt32 pn6 = Convert.ToUInt32(pname6, 16); byte[] p6 = System.BitConverter.GetBytes(pn6); string paraname6 = System.Text.ASCIIEncoding.ASCII.GetString(p6); UInt32 pn7 = Convert.ToUInt32(pname7, 16); byte[] p7 = System.BitConverter.GetBytes(pn7); string paraname7 = System.Text.ASCIIEncoding.ASCII.GetString(p7); UInt32 pn8 = Convert.ToUInt32(pname8, 16); byte[] p8 = System.BitConverter.GetBytes(pn8); string paraname8 = System.Text.ASCIIEncoding.ASCII.GetString(p8); UInt32 pn9 = Convert.ToUInt32(pname9, 16); byte[] p9 = System.BitConverter.GetBytes(pn9); string paraname9 = System.Text.ASCIIEncoding.ASCII.GetString(p9); UInt32 pn10 = Convert.ToUInt32(pname10, 16); byte[] p10 = System.BitConverter.GetBytes(pn10); string paraname10 = System.Text.ASCIIEncoding.ASCII.GetString(p10); tb_ParaName.Text = paraname1; tb_ParaName.Text += paraname2; tb_ParaName.Text += paraname3; tb_ParaName.Text += paraname4; tb_ParaName.Text += paraname5; tb_ParaName.Text += paraname6; tb_ParaName.Text += paraname7; tb_ParaName.Text += paraname8; tb_ParaName.Text += paraname9; tb_ParaName.Text += paraname10; string hexstring_full = str2.Substring(26, 4); string hexstring_balan = str2.Substring(30, 4); string hexstring_test = str2.Substring(34, 4); string hexstring_exhasut = str2.Substring(38, 4); string hexstring_bypass = str2.Substring(42, 4); string hexstring_delay1 = str2.Substring(46, 4); string hexstring_delay2 = str2.Substring(50, 4); string hexstring_bleakbalan = str2.Substring(62, 4); string hexstring_sleakbalan = str2.Substring(66, 4); string hexstring_bleaktoppre = str2.Substring(70, 4); string hexstring_bleaklowpre = str2.Substring(74, 4); // string hexstring_bleaktest = str2.Substring(124, 4); string hexstring_lunit = str2.Substring(86, 4); string hexstring_punit = str2.Substring(90, 4); //十六进制转十进制 int full = Int32.Parse(hexstring_full, System.Globalization.NumberStyles.HexNumber) / 10; int balan = Int32.Parse(hexstring_balan, System.Globalization.NumberStyles.HexNumber) / 10; int test = Int32.Parse(hexstring_test, System.Globalization.NumberStyles.HexNumber) / 10; double exhaust = Convert.ToDouble(Convert.ToInt32(hexstring_exhasut, 16)) / 10; int bypass = Int32.Parse(hexstring_bypass, System.Globalization.NumberStyles.HexNumber) / 10; double delay1 = Int32.Parse(hexstring_delay1, System.Globalization.NumberStyles.HexNumber); double delay2 = Int32.Parse(hexstring_delay2, System.Globalization.NumberStyles.HexNumber); double delay11 = delay1 / 10; double delay22 = delay2 / 10; int bleakbalan = Int32.Parse(hexstring_bleakbalan, System.Globalization.NumberStyles.HexNumber) / 10; int sleakbalan = Int32.Parse(hexstring_sleakbalan, System.Globalization.NumberStyles.HexNumber) / 10; int bleaktoppre = Int32.Parse(hexstring_bleaktoppre, System.Globalization.NumberStyles.HexNumber); int bleaklowpre = Int32.Parse(hexstring_bleaklowpre, System.Globalization.NumberStyles.HexNumber); // int bleaktest = Int32.Parse(hexstring_bleaktest, System.Globalization.NumberStyles.HexNumber) / 10; int lunit = Int32.Parse(hexstring_lunit, System.Globalization.NumberStyles.HexNumber); int punit = Int32.Parse(hexstring_punit, System.Globalization.NumberStyles.HexNumber); tb_FullTime.Text = full.ToString(); tb_BalanTime.Text = balan.ToString(); tb_TestTime1.Text = test.ToString(); tb_ExhaustTime.Text = exhaust.ToString(); tb_BypassTime.Text = bypass.ToString(); tb_DelayTime1.Text = delay11.ToString(); tb_DelayTime2.Text = delay22.ToString(); tb_BigBalanTime.Text = bleakbalan.ToString(); tb_SmallBalanTime.Text = sleakbalan.ToString(); // TestTime2 .Text = bleaktest.ToString(); tb_BigLeaktop.Text = bleaktoppre.ToString(); if (bleaklowpre > 32767) { string bleaklowpre2 = "FFFFFFFFFFFF" + hexstring_bleaklowpre; long bleaklowpre3 = Convert.ToInt64(bleaklowpre2, 16); tb_BigLeaklow.Text = bleaklowpre3.ToString(); } else { tb_BigLeaklow.Text = bleaklowpre.ToString(); } cb_LUnit.SelectedIndex = lunit; cb_PUnit.SelectedIndex = punit; lb_PressureUnit.Text = cb_PUnit.Text; lb_PressureUnit2.Text = cb_PUnit.Text; // label50.Text = PUnit.Text; lb_LeakUnit.Text = cb_LUnit.Text; lb_LeakUnit2.Text = cb_LUnit.Text; // label49.Text = LUnit.Text; string hexstring_fptop1 = str2.Substring(54, 4); string hexstring_fptop2 = str2.Substring(58, 4); string hexstring_fptop = hexstring_fptop2 + hexstring_fptop1; string hexstring_ltop1 = str2.Substring(78, 4); string hexstring_ltop2 = str2.Substring(82, 4); string hexstring_ltop = hexstring_ltop2 + hexstring_ltop1; string hexstring_evol1 = str2.Substring(94, 4); string hexstringg_evol2 = str2.Substring(98, 4); string hexstringg_evol = hexstringg_evol2 + hexstring_evol1; string hexstring_llow1 = str2.Substring(102, 4); string hexstring_llow2 = str2.Substring(106, 4); string hexstring_llow = hexstring_llow2 + hexstring_llow1; string hexstring_fplow1 = str2.Substring(110, 4); string hexstring_fplow2 = str2.Substring(114, 4); string hexstring_fplow = hexstring_fplow2 + hexstring_fplow1; string bleaktesttime = str2.Substring(118, 4); int testtime2 = Convert.ToInt32(bleaktesttime, 16) / 10; tb_TestTime2.Text = testtime2.ToString(); UInt32 x1 = Convert.ToUInt32(hexstring_fptop, 16);//字符串转16进制32位无符号整数 tb_FPtoplimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x1), 0).ToString();//IEEE754 字节转换float UInt32 x2 = Convert.ToUInt32(hexstring_ltop, 16);//字符串转16进制32位无符号整数 tb_Leaktoplimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x2), 0).ToString();//IEEE754 字节转换float UInt32 x3 = Convert.ToUInt32(hexstringg_evol, 16);//字符串转16进制32位无符号整数 tb_Evolume.Text = BitConverter.ToSingle(BitConverter.GetBytes(x3), 0).ToString();//IEEE754 字节转换float UInt32 x4 = Convert.ToUInt32(hexstring_llow, 16);//字符串转16进制32位无符号整数 tb_Leaklowlimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x4), 0).ToString();//IEEE754 字节转换float UInt32 x5 = Convert.ToUInt32(hexstring_fplow, 16);//字符串转16进制32位无符号整数 tb_FPlowlimit.Text = BitConverter.ToSingle(BitConverter.GetBytes(x5), 0).ToString();//IEEE754 字节转换float Timer3.Interval = 150; Timer3.Start(); break; case 3: string str3; str3 = ReceiveText.Text; string positive = str3.Substring(6, 2); int pos = Convert.ToInt32(positive, 16); if (pos == 1) { cb_TestType.SelectedIndex = 0; } else { cb_TestType.SelectedIndex = 1; } break; } } //发送过程 private void Send() { //将CRC冗余码添加进指令中 String str = SendText.Text; byte[] byt = StrtoHexbyte(str); int str2; str2 = Crc16_Modbus(byt, byt.Length); //byte byte1 =(byte)((str2 >> 8)&0xff); string str3; // str3=(( str2 >> 8) & 0xff).ToString (); str3 = Convert.ToString((str2 >> 8) & 0xff, 16); string str4; str4 = Convert.ToString(str2 & 0xff, 16); // string str4; // str4 = (str2 & 0xff).ToString(); // string str3; if (str3.Length == 1) { str3 = "0" + str3; // textBox2.Text = textBox2.Text + str4 + str3; } if (str4.Length == 1) { str4 = "0" + str4; // textBox2.Text = textBox2.Text + str4 + str3; } SendText.Text = SendText.Text + str4 + str3; String str5 = SendText.Text; if (str5.Length > 0) { byte[] byt2 = StrtoHexbyte(str5); SerialPort1.Write(byt2, 0, byt2.Length);//发送数据 } } //计算CRC冗余码 int Crc16_Modbus(byte[] modbusdata, int length) { int i, j; int crc = 0xffff;//0xffff or 0 for (i = 0; i < length; i++) { crc ^= modbusdata[i] & 0xff; for (j = 0; j < 8; j++) { if ((crc & 0x01) == 1) { crc = (crc >> 1) ^ 0xa001; } else { crc >>= 1; } } } return crc; } //将发送数据转为十六进制数据 private static byte[] StrtoHexbyte(String hexstring) { int i; hexstring = hexstring.Replace(" ", ""); byte[] returnBytes = new byte[(hexstring.Length) / 2]; try { for (i = 0; i < returnBytes.Length; i++) { returnBytes[i] = Convert.ToByte(hexstring.Substring(i * 2, 2), 16); } } catch (Exception ex) { MessageBox.Show(ex.Message); return null; } return returnBytes; } private void TestPara_FormClosing(object sender, FormClosingEventArgs e) { //try //{ // SerialPort1.Close(); // if (Form1.f1.label6.Text == "已连接") // { // Form1.f1.SerialPort1.Open(); // Form1.f1.Timer1.Interval = 500; // Form1.f1.Timer1.Start(); // } //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} } OperateResult operateResult25; OperateResult operateResult1; OperateResult operateResult2; OperateResult operateResult3; OperateResult operateResult4; private void Modify_Type_Click(object sender, EventArgs e) { if(cb_yiqi.Text == "1") { if (Form1.f1.CH1.IsOpen() == true) { if (cb_TestType.SelectedIndex == 0) // 正压 { operateResult1 = Form1.f1.CH1.WriteCoil("605", false); if (operateResult1.IsSuccess) { Form1.f1.CH1.WriteCoil("604", true); } } if (cb_TestType.SelectedIndex == 1) { operateResult2 = Form1.f1.CH1.WriteCoil("604", false); if (operateResult2.IsSuccess) { Form1.f1.CH1.WriteCoil("605", true); } } else { operateResult3 = Form1.f1.CH1.WriteCoil("605", false); if (operateResult3.IsSuccess) { Form1.f1.CH1.WriteCoil("604", true); } } } else { Form1.f1.CH1.Open(); if (cb_TestType.SelectedIndex == 0) { operateResult4 = Form1.f1.CH1.WriteCoil("605", false); if (operateResult4.IsSuccess) { Form1.f1.CH1.WriteCoil("604", true); } } if (cb_TestType.SelectedIndex == 1) { operateResult24 = Form1.f1.CH1.WriteCoil("604", false); if (operateResult24.IsSuccess) { Form1.f1.CH1.WriteCoil("605", true); } } } } if (cb_yiqi.Text == "2") { if (Form1.f1.CH2.IsOpen() == true) { if (cb_TestType.SelectedIndex == 0) // 正压 { OperateResult operateResult605 = Form1.f1.CH2.WriteCoil("605", false); if (operateResult605.IsSuccess) { Form1.f1.CH2.WriteCoil("604", true); } } if (cb_TestType.SelectedIndex == 1) { OperateResult operateResult604 = Form1.f1.CH2.WriteCoil("604", false); if (operateResult604.IsSuccess) { Form1.f1.CH2.WriteCoil("605", true); } } else { OperateResult operateResult605 = Form1.f1.CH2.WriteCoil("605", false); if (operateResult605.IsSuccess) { Form1.f1.CH2.WriteCoil("604", true); } } } else { Form1.f1.CH2.Open(); if (cb_TestType.SelectedIndex == 0) { OperateResult operateResult605 = Form1.f1.CH2.WriteCoil("605", false); if (operateResult605.IsSuccess) { Form1.f1.CH2.WriteCoil("604", true); } } if (cb_TestType.SelectedIndex == 1) { OperateResult operateResult604 = Form1.f1.CH2.WriteCoil("604", false); if (operateResult604.IsSuccess) { Form1.f1.CH2.WriteCoil("605", true); } } } } } //开启正压测试选项 private void Timer1_Tick(object sender, EventArgs e) { SendText.Text = Form1.f1.Station.Text + " 05 02 5C FF 00 "; Send(); stage = 10; Timer1.Stop(); } //开启负压测试选项 private void Timer2_Tick(object sender, EventArgs e) { SendText.Text = Form1.f1.Station.Text + " 05 02 5D FF 00 "; Send(); stage = 10; Timer2.Stop(); } //读取测试模式,点击了读取参数才可以读取测试模式 private void Timer3_Tick(object sender, EventArgs e) { SendText.Text = Form1.f1.Station.Text + " 01 02 5C 00 01"; // CrcNum(); Send(); stage = 3; Timer3.Stop(); } private void ParaNum_SelectedIndexChanged(object sender, EventArgs e) { //int i = cb_ParamID.SelectedIndex + 1; //ReadParameters(i); ReadParam(cb_yiqi.Text, cb_ParamID.Text); } /* ////写入注册表 //private void SetParameters(int i) //{ // RegistryKey regName; // regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true); // if (regName is null) // { // regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set"); // } // regName.SetValue("fulltime" + i, tb_FullTime.Text); // regName.SetValue("balantime" + i, tb_BalanTime.Text); // regName.SetValue("testtime1" + i, tb_TestTime1.Text); // regName.SetValue("testtime2" + i, tb_TestTime2.Text); // regName.SetValue("exhausttime" + i, tb_ExhaustTime.Text); // regName.SetValue("bypasstime" + i, tb_BypassTime.Text); // regName.SetValue("delaytime1" + i, tb_DelayTime1.Text); // regName.SetValue("delaytime2" + i, tb_DelayTime2.Text); // regName.SetValue("evolume" + i, tb_Evolume.Text); // regName.SetValue("paraname" + i, tb_ParaName.Text); // regName.SetValue("fptoplimit" + i, tb_FPtoplimit.Text); // regName.SetValue("fplowlimit" + i, tb_FPlowlimit.Text); // // regName.SetValue("bptoplimit" + i, BPtoplimit .Text); // // regName.SetValue("bplowlimit" + i, BPlowlimit .Text); // regName.SetValue("blptoplimit" + i, tb_BigLeaktop.Text); // regName.SetValue("blplowlimit" + i, tb_BigLeaklow.Text); // regName.SetValue("leaktoplimit" + i, tb_Leaktoplimit.Text); // regName.SetValue("leaklowlimit" + i, tb_Leaklowlimit.Text); // regName.SetValue("bbalantime" + i, tb_BigBalanTime.Text); // regName.SetValue("sbalantime" + i, tb_SmallBalanTime.Text); // regName.SetValue("punit" + i, cb_PUnit.SelectedIndex); // regName.SetValue("lunit" + i, cb_LUnit.SelectedIndex); // regName.Close(); //} */ // 保存参数在本地 private void SaveParam(string yiqi, string id) { var regName = new JsonConfig("config.json"); regName.SetValue($"{yiqi}fulltime{id}", tb_FullTime.Text); regName.SetValue($"{yiqi}balantime{id}", tb_BalanTime.Text); regName.SetValue($"{yiqi}testtime1{id}", tb_TestTime1.Text); regName.SetValue($"{yiqi}testtime2{id}", tb_TestTime2.Text); regName.SetValue($"{yiqi}exhausttime{id}", tb_ExhaustTime.Text); regName.SetValue($"{yiqi}bypasstime{id}", tb_BypassTime.Text); regName.SetValue($"{yiqi}delaytime1{id}", tb_DelayTime1.Text); regName.SetValue($"{yiqi}delaytime2{id}", tb_DelayTime2.Text); regName.SetValue($"{yiqi}evolume{id}", tb_Evolume.Text); regName.SetValue($"{yiqi}paraname{id}", tb_ParaName.Text); regName.SetValue($"{yiqi}fptoplimit{id}", tb_FPtoplimit.Text); regName.SetValue($"{yiqi}fplowlimit{id}", tb_FPlowlimit.Text); regName.SetValue($"{yiqi}blptoplimit{id}", tb_BigLeaktop.Text); regName.SetValue($"{yiqi}blplowlimit{id}", tb_BigLeaklow.Text); regName.SetValue($"{yiqi}leaktoplimit{id}", tb_Leaktoplimit.Text); regName.SetValue($"{yiqi}leaklowlimit{id}", tb_Leaklowlimit.Text); regName.SetValue($"{yiqi}bbalantime{id}", tb_BigBalanTime.Text); regName.SetValue($"{yiqi}sbalantime{id}", tb_SmallBalanTime.Text); regName.SetValue($"{yiqi}punit{id}", cb_PUnit.SelectedIndex); regName.SetValue($"{yiqi}lunit{id}", cb_LUnit.SelectedIndex); } private void ReadParam(string yiqi, string id) { var regName = new JsonConfig("config.json"); // 读取配置并赋值给控件 tb_FullTime.Text = regName.GetValue($"{yiqi}fulltime{id}", ""); tb_BalanTime.Text = regName.GetValue($"{yiqi}balantime{id}", ""); tb_TestTime1.Text = regName.GetValue($"{yiqi}testtime1{id}", ""); tb_TestTime2.Text = regName.GetValue($"{yiqi}testtime2{id}", ""); tb_ExhaustTime.Text = regName.GetValue($"{yiqi}exhausttime{id}", ""); tb_BypassTime.Text = regName.GetValue($"{yiqi}bypasstime{id}", ""); tb_DelayTime1.Text = regName.GetValue($"{yiqi}delaytime1{id}", ""); tb_DelayTime2.Text = regName.GetValue($"{yiqi}delaytime2{id}", ""); tb_Evolume.Text = regName.GetValue($"{yiqi}evolume{id}", ""); tb_ParaName.Text = regName.GetValue($"{yiqi}paraname{id}", ""); tb_FPtoplimit.Text = regName.GetValue($"{yiqi}fptoplimit{id}", ""); tb_FPlowlimit.Text = regName.GetValue($"{yiqi}fplowlimit{id}", ""); tb_BigLeaktop.Text = regName.GetValue($"{yiqi}blptoplimit{id}", ""); tb_BigLeaklow.Text = regName.GetValue($"{yiqi}blplowlimit{id}", ""); tb_Leaktoplimit.Text = regName.GetValue($"{yiqi}leaktoplimit{id}", ""); tb_Leaklowlimit.Text = regName.GetValue($"{yiqi}leaklowlimit{id}", ""); tb_BigBalanTime.Text = regName.GetValue($"{yiqi}bbalantime{id}", ""); tb_SmallBalanTime.Text = regName.GetValue($"{yiqi}sbalantime{id}", ""); // 下拉框(ComboBox)的读取(注意:SelectedIndex 是 int 类型) cb_PUnit.SelectedIndex = regName.GetValue($"{yiqi}punit{id}", 0); cb_LUnit.SelectedIndex = regName.GetValue($"{yiqi}lunit{id}", 0); } private void cb_yiqi_SelectedIndexChanged(object sender, EventArgs e) { ReadParam(cb_yiqi.Text, cb_ParamID.Text); } private void cb_PUnit_SelectedIndexChanged(object sender, EventArgs e) { lb_PressureUnit.Text = cb_PUnit.Text; lb_PressureUnit2.Text = cb_PUnit.Text; } private void cb_LUnit_SelectedIndexChanged(object sender, EventArgs e) { lb_LeakUnit.Text = cb_LUnit.Text; lb_LeakUnit2.Text = cb_LUnit.Text; } /* //读取注册表 private void ReadParameters(int i) { RegistryKey regName; regName = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set", true); if (regName is null) { regName = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\PMD\\1.0\\User-LL18-Set"); } if (regName.GetValue("fulltime" + i) is null) { tb_FullTime.Text = null; } else { regName.OpenSubKey("User"); string str1 = regName.GetValue("fulltime" + i).ToString(); tb_FullTime.Text = str1; } if (regName.GetValue("balantime" + i) is null) { tb_BalanTime.Text = null; } else { regName.OpenSubKey("User"); string str2 = regName.GetValue("balantime" + i).ToString(); tb_BalanTime.Text = str2; } if (regName.GetValue("testtime1" + i) is null) { tb_TestTime1.Text = null; } else { regName.OpenSubKey("User"); string str3 = regName.GetValue("testtime1" + i).ToString(); tb_TestTime1.Text = str3; } if (regName.GetValue("testtime2" + i) is null) { tb_TestTime2.Text = null; } else { regName.OpenSubKey("User"); string str11 = regName.GetValue("testtime2" + i).ToString(); tb_TestTime2.Text = str11; } if (regName.GetValue("exhausttime" + i) is null) { tb_ExhaustTime.Text = null; } else { regName.OpenSubKey("User"); string str4 = regName.GetValue("exhausttime" + i).ToString(); tb_ExhaustTime.Text = str4; } if (regName.GetValue("bypasstime" + i) is null) { tb_BypassTime.Text = null; } else { regName.OpenSubKey("User"); string str4 = regName.GetValue("bypasstime" + i).ToString(); tb_BypassTime.Text = str4; } if (regName.GetValue("delaytime1" + i) is null) { tb_DelayTime1.Text = null; } else { regName.OpenSubKey("User"); string str4 = regName.GetValue("delaytime1" + i).ToString(); tb_DelayTime1.Text = str4; } if (regName.GetValue("delaytime2" + i) is null) { tb_DelayTime2.Text = null; } else { regName.OpenSubKey("User"); string str4 = regName.GetValue("delaytime2" + i).ToString(); tb_DelayTime2.Text = str4; } if (regName.GetValue("evolume" + i) is null) { tb_Evolume.Text = null; } else { regName.OpenSubKey("User"); string str12 = regName.GetValue("evolume" + i).ToString(); tb_Evolume.Text = str12; } if (regName.GetValue("paraname" + i) is null) { tb_ParaName.Text = null; } else { regName.OpenSubKey("User"); string str13 = regName.GetValue("paraname" + i).ToString(); tb_ParaName.Text = str13; // regName.Close(); } if (regName.GetValue("fptoplimit" + i) is null) { tb_FPtoplimit.Text = null; } else { regName.OpenSubKey("User"); string str5 = regName.GetValue("fptoplimit" + i).ToString(); tb_FPtoplimit.Text = str5; } if (regName.GetValue("fplowlimit" + i) is null) { tb_FPlowlimit.Text = null; } else { regName.OpenSubKey("User"); string str6 = regName.GetValue("fplowlimit" + i).ToString(); tb_FPlowlimit.Text = str6; } if (regName.GetValue("blptoplimit" + i) is null) { tb_BigLeaktop.Text = null; } else { regName.OpenSubKey("User"); string str7 = regName.GetValue("blptoplimit" + i).ToString(); tb_BigLeaktop.Text = str7; } if (regName.GetValue("blplowlimit" + i) is null) { tb_BigLeaklow.Text = null; } else { regName.OpenSubKey("User"); string str8 = regName.GetValue("blplowlimit" + i).ToString(); tb_BigLeaklow.Text = str8; } if (regName.GetValue("leaktoplimit" + i) is null) { tb_Leaktoplimit.Text = null; } else { regName.OpenSubKey("User"); string str9 = regName.GetValue("leaktoplimit" + i).ToString(); tb_Leaktoplimit.Text = str9; } if (regName.GetValue("leaklowlimit" + i) is null) { tb_Leaklowlimit.Text = null; } else { regName.OpenSubKey("User"); string str10 = regName.GetValue("leaklowlimit" + i).ToString(); tb_Leaklowlimit.Text = str10; } if (regName.GetValue("bbalantime" + i) is null) { tb_BigBalanTime.Text = null; } else { regName.OpenSubKey("User"); string str10 = regName.GetValue("bbalantime" + i).ToString(); tb_BigBalanTime.Text = str10; } if (regName.GetValue("sbalantime" + i) is null) { tb_SmallBalanTime.Text = null; } else { regName.OpenSubKey("User"); string str10 = regName.GetValue("sbalantime" + i).ToString(); tb_SmallBalanTime.Text = str10; } if (regName.GetValue("punit" + i) is null) { cb_PUnit.Text = null; } else { regName.OpenSubKey("User"); string str13 = regName.GetValue("punit" + i).ToString(); cb_PUnit.SelectedIndex = Convert.ToInt32(str13); } if (regName.GetValue("lunit" + i) is null) { cb_LUnit.Text = null; } else { regName.OpenSubKey("User"); string str13 = regName.GetValue("lunit" + i).ToString(); cb_LUnit.SelectedIndex = Convert.ToInt32(str13); regName.Close(); } } */ } }