找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1311|回复: 0
打印 上一主题 下一主题
收起左侧

基于c#的配气仪源程序

[复制链接]
跳转到指定楼层
楼主
ID:611900 发表于 2019-9-16 09:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于C#的配气仪源码,个人练习做的

单片机源程序如下:
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Windows.Forms;

  13. namespace GCC
  14. {
  15.     public partial class MainForm : Form
  16.     {
  17.         public static bool isRun = false;
  18.         public static string keyBoardValue;
  19.         //public readonly Thread timerThread = new Thread(SetTimer);
  20.         //public System.Timers.Timer showTimer = new System.Timers.Timer();

  21.         public MainForm()
  22.         {
  23.             InitializeComponent();
  24.             Control.CheckForIllegalCrossThreadCalls = false;   //防止跨线程访问出错
  25.         }

  26.         private void Cb_gas1_SelectedIndexChanged(object sender, EventArgs e)
  27.         {
  28.             tb_coefficient1.Text = DataProcessing.GetCoefficient(cb_gas1.SelectedItem.ToString());
  29.             tb_concentration1.Text = "";
  30.             tb_target_concentration1.Text = "";
  31.         }

  32.         private void Cb_gas2_SelectedIndexChanged(object sender, EventArgs e)
  33.         {
  34.             tb_coefficient2.Text = DataProcessing.GetCoefficient(cb_gas2.SelectedItem.ToString());
  35.             tb_concentration2.Text = "";
  36.             tb_target_concentration2.Text = "";
  37.         }

  38.         private void Cb_gas3_SelectedIndexChanged(object sender, EventArgs e)
  39.         {
  40.             tb_coefficient3.Text = DataProcessing.GetCoefficient(cb_gas3.SelectedItem.ToString());
  41.             tb_concentration3.Text = "";
  42.             tb_target_concentration3.Text = "";
  43.         }

  44.         private void Cb_gas4_SelectedIndexChanged(object sender, EventArgs e)
  45.         {
  46.             tb_coefficient4.Text = DataProcessing.GetCoefficient(cb_gas4.SelectedItem.ToString());
  47.             tb_concentration4.Text = "";
  48.             tb_target_concentration4.Text = "";
  49.         }

  50.         private void MainForm_Load(object sender, EventArgs e)
  51.         {
  52.             try
  53.             {
  54.                 SerialPortHandle.LoadSerialConfig(sp, "COM7", "9600", "8", "2", "NONE");

  55.                 MachineInitialization(sp);
  56.                 tb_states1.Text = "关闭";
  57.                 tb_states2.Text = "关闭";
  58.                 tb_states3.Text = "关闭";
  59.                 tb_states4.Text = "关闭";

  60.                 DataProcessing.LogRecord("系统初始化完成");
  61.                 //showTimer.Interval = 1000;
  62.                 //showTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerExecute);
  63.             }
  64.             catch
  65.             { }
  66.         }
  67.         
  68.         private void Bt_start_Click(object sender, EventArgs e)
  69.         {
  70.             ComboBox[] comboBoxes = { cb_gas1, cb_gas2, cb_gas3, cb_gas4 };
  71.             TextBox[,] aisle = {
  72.                 { tb_concentration1, tb_target_concentration1, tb_coefficient1, tb_flow1, tb_test_concentration1, tb_states1 },
  73.                 { tb_concentration2, tb_target_concentration2, tb_coefficient2, tb_flow2, tb_test_concentration2, tb_states2 },
  74.                 { tb_concentration3, tb_target_concentration3, tb_coefficient3, tb_flow3, tb_test_concentration3, tb_states3 },
  75.                 { tb_concentration4, tb_target_concentration4, tb_coefficient4, tb_flow4, tb_test_concentration4, tb_states4 }
  76.             };

  77.             if (!sp.IsOpen)
  78.             {
  79.                 SerialPortHandle.LoadSerialConfig(sp, "COM7", "9600", "8", "2", "NONE");
  80.             }
  81.             DataProcessing.DataCounting(comboBoxes, aisle);
  82.             WriteSetflowValue();
  83.             bt_start.Enabled = false;
  84.             cb_gas1.Enabled = false;
  85.             cb_gas2.Enabled = false;
  86.             cb_gas3.Enabled = false;
  87.             cb_gas4.Enabled = false;
  88.             tb_concentration1.ReadOnly = true;
  89.             tb_concentration2.ReadOnly = true;
  90.             tb_concentration3.ReadOnly = true;
  91.             tb_concentration4.ReadOnly = true;
  92.             tb_target_concentration1.ReadOnly = true;
  93.             tb_target_concentration2.ReadOnly = true;
  94.             tb_target_concentration3.ReadOnly = true;
  95.             tb_target_concentration4.ReadOnly = true;
  96.             tb_concentration1.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration1_MouseDown);
  97.             tb_concentration2.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration2_MouseDown);
  98.             tb_concentration3.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration3_MouseDown);
  99.             tb_concentration4.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration4_MouseDown);
  100.             tb_target_concentration1.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration1_MouseDown);
  101.             tb_target_concentration2.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration2_MouseDown);
  102.             tb_target_concentration3.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration3_MouseDown);
  103.             tb_target_concentration4.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration4_MouseDown);
  104.         }

  105.         string aisle1_setFlow = "";
  106.         string aisle2_setFlow = "";
  107.         string aisle3_setFlow = "";
  108.         string aisle4_setFlow = "";
  109.         /// <summary>
  110.         /// 写入设定流量值
  111.         /// </summary>
  112.         public void WriteSetflowValue()
  113.         {
  114.             timer.Enabled = false;
  115.             //showTimer.Enabled = false;
  116.             string[] setFlowValue = new string[4];
  117.             ComboBox[] comboBoxes = { cb_gas1, cb_gas2, cb_gas3, cb_gas4 };
  118.             
  119.             if (sp.IsOpen)
  120.             {
  121.                 for (int i = 0; i < comboBoxes.Length; i++)
  122.                 {
  123.                     if (comboBoxes[i].Text != "")
  124.                     {
  125.                         if (comboBoxes[i].Text != " ")
  126.                         {
  127.                             try
  128.                             {
  129.                                 //string a = tb_setOutputFlow.Text;
  130.                                 setFlowValue[i] = DataProcessing.GetGasSetFlow(DataProcessing.gasR[i], Convert.ToDouble(tb_setOutputFlow.Text)).ToString();
  131.                                 //MessageBox.Show(setFlowValue[i]);
  132.                                 //Thread.Sleep(50);
  133.                                 if (isRun)
  134.                                 {
  135.                                     int index = i + 1;
  136.                                     if (ValveIsClose("通道" + index, "读关闭", "", index))
  137.                                     {
  138.                                         while (true)    //等待消息接收完成
  139.                                         {
  140.                                             if (isReceived)
  141.                                             {
  142.                                                 isReceived = false;
  143.                                                 SerialPortHandle.SendSerialData(sp, DataProcessing.SendDataformat("通道" + index, "阀控控制", ""));
  144.                                                 Thread.Sleep(100);
  145.                                                 break;
  146.                                             }
  147.                                         }
  148.                                         //MessageBox.Show("通道" + index + "阀控打开");
  149.                                     }
  150.                                     DataProcessing.LogRecord("通道"+index+ "写入设定流量值");
  151.                                     byte[] sendData = DataProcessing.SendDataformat("通道" + index, "写入设定流量值", setFlowValue[i]);
  152.                                     while (true)    //等待消息接收完成
  153.                                     {
  154.                                         if (isReceived)
  155.                                         {
  156.                                             isReceived = false;
  157.                                             SerialPortHandle.SendSerialData(sp, sendData);
  158.                                             Thread.Sleep(100);
  159.                                             break;
  160.                                         }
  161.                                     }
  162.                                     //MessageBox.Show("通道" + index + "写入设定流量值成功");
  163.                                     switch (i)
  164.                                     {
  165.                                         case 0:
  166.                                             aisle1_setFlow = setFlowValue[i];
  167.                                             break;
  168.                                         case 1:
  169.                                             aisle2_setFlow = setFlowValue[i];
  170.                                             break;
  171.                                         case 2:
  172.                                             aisle3_setFlow = setFlowValue[i];
  173.                                             break;
  174.                                         case 3:
  175.                                             aisle4_setFlow = setFlowValue[i];
  176.                                             break;
  177.                                     }
  178.                                 }
  179.                                 isRun = false;
  180.                             }
  181.                             catch (System.Exception ex)
  182.                             {
  183.                                 MessageBox.Show("WriteSetflowValue函数:" + ex.Message);
  184.                             }
  185.                         }
  186.                     }
  187.                 }
  188.                 timer.Enabled = true;
  189.                 DataProcessing.LogRecord("定时器打开");
  190.                 //showTimer.Enabled = true;
  191.             }
  192.             //showTimer.Enabled = true;
  193.         }

  194.         /// <summary>
  195.         /// 设置定时器
  196.         /// </summary>
  197.         //private void SetTimer()
  198.         //{
  199.         //    //showTimer.Interval = 1000;
  200.         //    //showTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerExecute);
  201.         //    //showTimer.Enabled = true;
  202.         //}

  203.         ///// <summary>
  204.         ///// 定时器执行函数
  205.         ///// </summary>
  206.         ///// <param name="source"></param>
  207.         ///// <param name="e"></param>
  208.         //private void TimerExecute(object source, System.Timers.ElapsedEventArgs e)
  209.         //{
  210.         //    //showTimer.Enabled = false;
  211.         //    if (sp.IsOpen)
  212.         //    {
  213.         //        ShowFlowAndConcentration();
  214.         //        sp.DiscardInBuffer();
  215.         //        sp.DiscardOutBuffer();
  216.         //    }
  217.         //    else
  218.         //    {
  219.         //    }
  220.         //    //showTimer.Enabled = true;
  221.         //}

  222.         /// <summary>
  223.         /// 判断通道阀门状态
  224.         /// </summary>
  225.         /// <param name="aisleName">通道名称</param>
  226.         /// <param name="strFun">功能码</param>
  227.         /// <param name="data">数据值</param>
  228.         /// <param name="index">索引</param>
  229.         /// <returns>返回值为true,阀门打开;返回值为false,阀门关闭</returns>
  230.         public bool ValveIsClose(string aisleName, string strFun, string data, int index)
  231.         {
  232.             DataProcessing.LogRecord(aisleName + strFun);
  233.             while (true)    //等待消息接收完成
  234.             {
  235.                 if (isReceived)
  236.                 {
  237.                     isReceived = false;
  238.                     SerialPortHandle.SendSerialData(sp, DataProcessing.SendDataformat(aisleName, strFun, data));
  239.                     Thread.Sleep(100);
  240.                     break;
  241.                 }
  242.             }
  243.             if (index == 1)
  244.             {

  245.                 //string A = p;
  246.                 if (DataProcessing.aisle1[1] == 0x01)
  247.                 {
  248.                     return true;
  249.                 }
  250.                 else
  251.                 {
  252.                     return false;
  253.                 }
  254.             }
  255.             else if (index == 2)
  256.             {
  257.                 //string B = p;
  258.                 if (DataProcessing.aisle2[1] == 0x01)
  259.                 {
  260.                     return true;
  261.                 }
  262.                 else
  263.                 {
  264.                     return false;
  265.                 }
  266.             }
  267.             else if (index == 3)
  268.             {
  269.                 //string C = p;
  270.                 if (DataProcessing.aisle3[1] == 0x01)
  271.                 {
  272.                     return true;
  273.                 }
  274.                 else
  275.                 {
  276.                     return false;
  277.                 }
  278.             }
  279.             else
  280.             {
  281.                 //string D = p;
  282.                 if (DataProcessing.aisle4[1] == 0x01)
  283.                 {
  284.                     return true;
  285.                 }
  286.                 else
  287.                 {
  288.                     return false;
  289.                 }
  290.             }
  291.         }

  292.         /// <summary>
  293.         /// 显示测量流量和测量浓度
  294.         /// </summary>
  295.         public void ShowFlowAndConcentration()
  296.         {
  297.             ComboBox[] comboBoxes = { cb_gas1, cb_gas2, cb_gas3, cb_gas4 };
  298.             TextBox[] tb_concentration = { tb_concentration1, tb_concentration2, tb_concentration3, tb_concentration4 };
  299.             TextBox[] tb_test_concentration = { tb_test_concentration1, tb_test_concentration2, tb_test_concentration3, tb_test_concentration4 };
  300.             double[] result = { 0, 0, 0, 0 };
  301.             double temp;
  302.             if (sp.IsOpen)
  303.             {
  304.                 try
  305.                 {
  306.                     for (int i = 1; i <= 4; i++)
  307.                     {
  308.                         if (comboBoxes[i - 1].Text != "")
  309.                         {
  310.                             if (comboBoxes[i - 1].Text != " ")
  311.                             {
  312.                                 DataProcessing.LogRecord("通道" + i + "读显示流量");
  313.                                 byte[] sendData = DataProcessing.SendDataformat("通道" + i, "读显示流量", "");
  314.                                 while (true)    //等待消息接收完成
  315.                                 {
  316.                                     if (isReceived)
  317.                                     {
  318.                                         isReceived = false;
  319.                                         SerialPortHandle.SendSerialData(sp, sendData);
  320.                                         Thread.Sleep(100);
  321.                                         break;
  322.                                     }
  323.                                 }
  324.                                 switch (i)
  325.                                 {
  326.                                     case 1:
  327.                                         temp = CountFlowAndConcentration(DataProcessing.aisle1);
  328.                                         result[0] = Convert.ToInt32(temp / 4095 * 2000);
  329.                                         tb_flow1.Text = result[0].ToString();
  330.                                         //tb_test_concentration1.Text = (result[0] * double.Parse(tb_coefficient1.Text)).ToString();
  331.                                         tb_states1.Text = MeasuringCondition(aisle1_setFlow, temp);
  332.                                         break;
  333.                                     case 2:
  334.                                         temp = CountFlowAndConcentration(DataProcessing.aisle2);
  335.                                         result[1] = Convert.ToInt32(temp / 4095 * 2000);
  336.                                         tb_flow2.Text = result[1].ToString();
  337.                                         //tb_test_concentration2.Text = (result[1] * double.Parse(tb_coefficient2.Text)).ToString();
  338.                                         tb_states2.Text = MeasuringCondition(aisle2_setFlow, temp);
  339.                                         break;
  340.                                     case 3:
  341.                                         temp = CountFlowAndConcentration(DataProcessing.aisle3);
  342.                                         result[2] = Convert.ToInt32(temp / 4095 * 2000);
  343.                                         tb_flow3.Text = result[2].ToString();
  344.                                         //tb_test_concentration3.Text = (result[2] * double.Parse(tb_coefficient3.Text)).ToString();
  345.                                         tb_states3.Text = MeasuringCondition(aisle3_setFlow, temp);
  346.                                         break;
  347.                                     case 4:
  348.                                         temp = CountFlowAndConcentration(DataProcessing.aisle4);
  349.                                         result[3] = Convert.ToInt32(temp / 4095 * 2000);
  350.                                         tb_flow4.Text = result[3].ToString();
  351.                                         //tb_test_concentration4.Text = (result[3] * double.Parse(tb_coefficient4.Text)).ToString();
  352.                                         tb_states4.Text = MeasuringCondition(aisle4_setFlow, temp);
  353.                                         break;
  354.                                 }
  355.                             }
  356.                         }
  357.                     }
  358.                     DataProcessing.GetTestGasCoefficient(result);
  359.                     for (int j = 0; j < comboBoxes.Length; j++)
  360.                     {
  361.                         if (comboBoxes[j].Text != "")
  362.                         {
  363.                             if (comboBoxes[j].Text != " ")
  364.                             {
  365.                                 if (comboBoxes[j].Text == "N2")
  366.                                 {
  367.                                     //tb_test_concentration[j].Text = (double.Parse(tb_concentration[j].Text) * 1).ToString("#0.00");
  368.                                 }
  369.                                 else
  370.                                 {
  371.                                     tb_test_concentration[j].Text = (double.Parse(tb_concentration[j].Text) * DataProcessing.testGasR[j]).ToString("#0.00");
  372.                                 }
  373.                                     
  374.                             }
  375.                         }
  376.                     }
  377.                 }
  378.                 catch (System.Exception ex)
  379.                 {
  380.                     MessageBox.Show("ShowFlowAndConcentration函数:" + ex.Message);
  381.                 }
  382.             }
  383.         }

  384.         /// <summary>
  385.         ///
  386.         /// </summary>
  387.         /// <param name="aisle"></param>
  388.         /// <returns></returns>
  389.         private static double CountFlowAndConcentration(byte[] aisle)
  390.         {
  391.             double temp = double.Parse(Convert.ToInt32(aisle[1] * 256 + aisle[2]).ToString());
  392.             if (temp > 4095)
  393.             {
  394.                 temp = 0;
  395.             }
  396.             DataProcessing.LogRecord("读取的流量值为:" + temp);
  397.             return temp;
  398.         }

  399.         private void Bt_back_Click(object sender, EventArgs e)
  400.         {
  401.             //showTimer.Enabled = false;
  402.             //timerThread.Suspend();
  403.             bt_start.Enabled = true;
  404.             cb_gas1.Enabled = true;
  405.             cb_gas2.Enabled = true;
  406.             cb_gas3.Enabled = true;
  407.             cb_gas4.Enabled = true;
  408.             tb_concentration1.ReadOnly = false;
  409.             tb_concentration2.ReadOnly = false;
  410.             tb_concentration3.ReadOnly = false;
  411.             tb_concentration4.ReadOnly = false;
  412.             tb_target_concentration1.ReadOnly = false;
  413.             tb_target_concentration2.ReadOnly = false;
  414.             tb_target_concentration3.ReadOnly = false;
  415.             tb_target_concentration4.ReadOnly = false;
  416.             tb_maxOutputFlow.ReadOnly = false;
  417.             tb_setOutputFlow.ReadOnly = false;
  418.             tb_flow1.Text = "";
  419.             tb_flow2.Text = "";
  420.             tb_flow3.Text = "";
  421.             tb_flow4.Text = "";
  422.             tb_test_concentration1.Text = "";
  423.             tb_test_concentration2.Text = "";
  424.             tb_test_concentration3.Text = "";
  425.             tb_test_concentration4.Text = "";
  426.             tb_concentration1.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration1_MouseDown);
  427.             tb_concentration2.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration2_MouseDown);
  428.             tb_concentration3.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration3_MouseDown);
  429.             tb_concentration4.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration4_MouseDown);
  430.             tb_target_concentration1.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration1_MouseDown);
  431.             tb_target_concentration2.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration2_MouseDown);
  432.             tb_target_concentration3.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration3_MouseDown);
  433.             tb_target_concentration4.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration4_MouseDown);
  434.             if (sp.IsOpen)
  435.             {
  436.                 timer.Enabled = false;
  437.                 for (int i = 1; i <= 4; i++)
  438.                 {
  439.                     while (true)    //等待消息接收完成
  440.                     {
  441.                         if (isReceived)
  442.                         {
  443.                             isReceived = false;
  444.                             SerialPortHandle.SendSerialData(sp, DataProcessing.SendDataformat("通道" + i, "阀控关闭", ""));
  445.                             Thread.Sleep(100);
  446.                             break;
  447.                         }
  448.                     }
  449.                 }
  450.                 tb_states1.Text = "关闭";
  451.                 tb_states2.Text = "关闭";
  452.                 tb_states3.Text = "关闭";
  453.                 tb_states4.Text = "关闭";
  454.                 //tb_states1.ForeColor = System.Drawing.SystemColors.WindowText;
  455.                 //tb_states2.ForeColor = System.Drawing.SystemColors.WindowText;
  456.                 //tb_states3.ForeColor = System.Drawing.SystemColors.WindowText;
  457.                 //tb_states4.ForeColor = System.Drawing.SystemColors.WindowText;
  458.                 //sp.Close();
  459.             }
  460.         }

  461.         public static bool isReceived = false;

  462.         /// <summary>
  463.         /// 串口数据接收
  464.         /// </summary>
  465.         /// <param name="sender"></param>
  466.         /// <param name="e"></param>
  467.         private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
  468.         {
  469.             Thread.Sleep(50);
  470.             int bufferlen = sp.BytesToRead;
  471.             string readStr = "";
  472.             if (bufferlen > 0)
  473.             {
  474.                 byte[] readbuffer = new byte[bufferlen];
  475.                 sp.Read(readbuffer, 0, readbuffer.Length);
  476.                 for (int i = 0; i < readbuffer.Length; i++)
  477.                 {
  478.                     readStr += readbuffer[i] + "/";
  479.                 }
  480.                 DataProcessing.LogRecord("串口接收到的数据:" + readStr);
  481.                 Thread dataAnalysisThread = new Thread(() => DataProcessing.DataAnalysis(readbuffer));
  482.                 dataAnalysisThread.IsBackground = true;
  483.                 dataAnalysisThread.Start();
  484.                 //DataProcessing.DataAnalysis(readbuffer);
  485.                 isReceived = true;
  486.             }
  487.         }
  488.         
  489.         /// <summary>
  490.         /// 机器初始化设置
  491.         /// </summary>
  492.         /// <param name="sp">通信的串口</param>
  493.         private static void MachineInitialization(SerialPort sp)
  494.         {
  495.             Queue initializtionQueue = new Queue();
  496.             initializtionQueue.Clear();

  497.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道1", "阀控关闭", ""));   //0  
  498.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道1", "读关闭", ""));    //1  
  499.             

  500.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道2", "阀控关闭", ""));   //2
  501.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道2", "读关闭", ""));    //3


  502.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道3", "阀控关闭", ""));   //4
  503.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道3", "读关闭", ""));    //5


  504.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道4", "阀控关闭", ""));   //6
  505.             initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道4", "读关闭", ""));    //7

  506.             SendQueueMessage(sp,initializtionQueue);
  507.         }

  508.         /// <summary>
  509.         /// 发送队列消息
  510.         /// </summary>
  511.         /// <param name="sp">串口对象</param>
  512.         /// <param name="sendQueue">需要发送的消息队列</param>
  513.         private static void SendQueueMessage(SerialPort sp,Queue sendQueue)
  514.         {
  515.             if (sp.IsOpen)
  516.             {
  517.                 isReceived = true;
  518.                 foreach (byte[] sendData in sendQueue)
  519.                 {
  520.                     int index = GetIndexFromQueue(sendData, sendQueue);
  521.                     //send:  SerialPortHandle.SendSerialData(sp, sendData);

  522.                     send: while (true)    //等待消息接收完成
  523.                     {
  524.                         if (isReceived)
  525.                         {
  526.                             isReceived = false;
  527.                             SerialPortHandle.SendSerialData(sp, sendData);
  528.                             Thread.Sleep(50);
  529.                             break;
  530.                         }
  531.                     }

  532.                     if (index == 1)
  533.                     {
  534.                         if (DataProcessing.aisle1[1] != 0x01)
  535.                         {
  536.                             goto send;
  537.                         }
  538.                     }
  539.                     else if (index == 3)
  540.                     {
  541.                         if (DataProcessing.aisle2[1] != 0x01)
  542.                         {
  543.                             goto send;
  544.                         }
  545.                     }
  546.                     else if (index == 5)
  547.                     {
  548.                         if (DataProcessing.aisle3[1] != 0x01)
  549.                         {
  550.                             goto send;
  551.                         }
  552.                     }
  553.                     else if (index == 7)
  554.                     {
  555.                         if (DataProcessing.aisle4[1] != 0x01)
  556.                         {
  557.                             goto send;
  558.                         }
  559.                     }
  560.                     else
  561.                     {
  562.                         //continue;
  563.                     }
  564.                     //isReceived = false;
  565.                 }
  566.                 sendQueue.Clear();
  567.             }
  568.         }

  569.         /// <summary>
  570.         /// 获取队列索引值
  571.         /// </summary>
  572.         /// <param name="element">被索引的元素</param>
  573.         /// <param name="queue">索引队列</param>
  574.         /// <returns>索引值</returns>
  575.         private static int GetIndexFromQueue(byte[] element, Queue queue)
  576.         {
  577.             int index = 0;
  578.             if (queue.Count > 0)
  579.             {
  580.                 for (int i = 0; i < queue.ToArray().Length; i++)
  581.                 {
  582.                     if (element == queue.ToArray()[i])
  583.                     {
  584.                         index = i;
  585.                         break;
  586.                     }
  587.                 }
  588.             }
  589.             return index;
  590.         }

  591.         private void Timer_Tick(object sender, EventArgs e)
  592.         {
  593.             if (sp.IsOpen)
  594.             {
  595.                 timer.Enabled = false;
  596.                 ShowFlowAndConcentration();
  597.                 sp.DiscardInBuffer();
  598.                 sp.DiscardOutBuffer();
  599.                 timer.Enabled = true;
  600.             }
  601.         }

  602.         /// <summary>
  603.         /// 获取测量状态
  604.         /// </summary>
  605.         /// <param name="setFlow">设定的流量值</param>
  606.         /// <param name="testFlow">测试的流量值</param>
  607.         /// <param name="textBox"></param>
  608.         /// <returns></returns>
  609.         private static string MeasuringCondition(string setFlow, double testFlow)
  610.         {
  611.             string status;
  612.             //textBox.ReadOnly = false;
  613.             double difference = System.Math.Abs(testFlow - double.Parse(setFlow));
  614.             if (double.Parse(setFlow) > 100)
  615.             {
  616.                 if (difference > double.Parse(setFlow) * 0.05)
  617.                 {
  618.                     status = "异常";
  619.                     //textBox.ForeColor = System.Drawing.Color.Red;
  620.                 }
  621.                 else
  622.                 {
  623.                     status = "正常";
  624.                     //textBox.ForeColor = System.Drawing.Color.Chartreuse;
  625.                 }
  626.             }
  627.             else
  628.             {
  629.                 if (difference > 5)
  630.                 {
  631.                     status = "异常";
  632.                     //textBox.ForeColor = System.Drawing.Color.Red;
  633.                 }
  634.                 else
  635.                 {
  636.                     status = "正常";
  637.                     //textBox.ForeColor = System.Drawing.Color.Chartreuse;
  638.                 }
  639.             }
  640.             //textBox.ReadOnly = true;
  641.             return status;
  642.         }

  643.         /// <summary>
  644.         /// 获取键盘窗口输入值
  645.         /// </summary>
  646.         /// <param name="textBox"></param>
  647.         private void GetKeyBoardValue(TextBox textBox)
  648.         {

  649.             KeyBoardForm keyBoard = new KeyBoardForm();
  650.             keyBoard.ShowDialog();
  651.             textBox.Text = keyBoardValue;
  652.             keyBoard.Dispose();
  653.         }

  654.         private void Bt_clear_Click(object sender, EventArgs e)
  655.         {
  656.             
  657.         }

  658.         private void Tb_setOutputFlow_MouseDown(object sender, MouseEventArgs e)
  659.         {
  660.             GetKeyBoardValue(tb_setOutputFlow);
  661.         }

  662.         private void Tb_concentration1_MouseDown(object sender, MouseEventArgs e)
  663.         {
  664.             GetKeyBoardValue(tb_concentration1);
  665.         }

  666.         private void Tb_concentration2_MouseDown(object sender, MouseEventArgs e)
  667.         {
  668.             GetKeyBoardValue(tb_concentration2);
  669.         }

  670.         private void Tb_concentration3_MouseDown(object sender, MouseEventArgs e)
  671.         {
  672.             GetKeyBoardValue(tb_concentration3);
  673.         }

  674.         private void Tb_concentration4_MouseDown(object sender, MouseEventArgs e)
  675.         {
  676.             GetKeyBoardValue(tb_concentration4);
  677.         }

  678.         private void Tb_target_concentration1_MouseDown(object sender, MouseEventArgs e)
  679.         {
  680.             GetKeyBoardValue(tb_target_concentration1);
  681.         }

  682.         private void Tb_target_concentration2_MouseDown(object sender, MouseEventArgs e)
  683.         {
  684.             GetKeyBoardValue(tb_target_concentration2);
  685.         }

  686.         private void Tb_target_concentration3_MouseDown(object sender, MouseEventArgs e)
  687.         {
  688.             GetKeyBoardValue(tb_target_concentration3);
  689.         }

  690.         private void Tb_target_concentration4_MouseDown(object sender, MouseEventArgs e)
  691.         {
  692.             GetKeyBoardValue(tb_target_concentration4);
  693.         }

  694.         private void Tb_setOutputFlow_TextChanged(object sender, EventArgs e)
  695.         {
  696.             if (sp.IsOpen)
  697.             {
  698.                 if (!bt_start.Enabled)
  699.                 {
  700.                     WriteSetflowValue();
  701.                 }
  702.             }
  703.         }

  704.         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  705.         {
  706.             if (sp.IsOpen)
  707.             {
  708.                 sp.Close();
  709.             }
  710.         }

  711.     }
  712. }
复制代码

所有资料51hei提供下载:
GCC.rar (326.54 KB, 下载次数: 22)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表