标题:
基于c#的配气仪源程序
[打印本页]
作者:
勒维奥萨
时间:
2019-9-16 09:37
标题:
基于c#的配气仪源程序
基于C#的配气仪源码,个人练习做的
单片机源程序如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace GCC
{
public partial class MainForm : Form
{
public static bool isRun = false;
public static string keyBoardValue;
//public readonly Thread timerThread = new Thread(SetTimer);
//public System.Timers.Timer showTimer = new System.Timers.Timer();
public MainForm()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //防止跨线程访问出错
}
private void Cb_gas1_SelectedIndexChanged(object sender, EventArgs e)
{
tb_coefficient1.Text = DataProcessing.GetCoefficient(cb_gas1.SelectedItem.ToString());
tb_concentration1.Text = "";
tb_target_concentration1.Text = "";
}
private void Cb_gas2_SelectedIndexChanged(object sender, EventArgs e)
{
tb_coefficient2.Text = DataProcessing.GetCoefficient(cb_gas2.SelectedItem.ToString());
tb_concentration2.Text = "";
tb_target_concentration2.Text = "";
}
private void Cb_gas3_SelectedIndexChanged(object sender, EventArgs e)
{
tb_coefficient3.Text = DataProcessing.GetCoefficient(cb_gas3.SelectedItem.ToString());
tb_concentration3.Text = "";
tb_target_concentration3.Text = "";
}
private void Cb_gas4_SelectedIndexChanged(object sender, EventArgs e)
{
tb_coefficient4.Text = DataProcessing.GetCoefficient(cb_gas4.SelectedItem.ToString());
tb_concentration4.Text = "";
tb_target_concentration4.Text = "";
}
private void MainForm_Load(object sender, EventArgs e)
{
try
{
SerialPortHandle.LoadSerialConfig(sp, "COM7", "9600", "8", "2", "NONE");
MachineInitialization(sp);
tb_states1.Text = "关闭";
tb_states2.Text = "关闭";
tb_states3.Text = "关闭";
tb_states4.Text = "关闭";
DataProcessing.LogRecord("系统初始化完成");
//showTimer.Interval = 1000;
//showTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerExecute);
}
catch
{ }
}
private void Bt_start_Click(object sender, EventArgs e)
{
ComboBox[] comboBoxes = { cb_gas1, cb_gas2, cb_gas3, cb_gas4 };
TextBox[,] aisle = {
{ tb_concentration1, tb_target_concentration1, tb_coefficient1, tb_flow1, tb_test_concentration1, tb_states1 },
{ tb_concentration2, tb_target_concentration2, tb_coefficient2, tb_flow2, tb_test_concentration2, tb_states2 },
{ tb_concentration3, tb_target_concentration3, tb_coefficient3, tb_flow3, tb_test_concentration3, tb_states3 },
{ tb_concentration4, tb_target_concentration4, tb_coefficient4, tb_flow4, tb_test_concentration4, tb_states4 }
};
if (!sp.IsOpen)
{
SerialPortHandle.LoadSerialConfig(sp, "COM7", "9600", "8", "2", "NONE");
}
DataProcessing.DataCounting(comboBoxes, aisle);
WriteSetflowValue();
bt_start.Enabled = false;
cb_gas1.Enabled = false;
cb_gas2.Enabled = false;
cb_gas3.Enabled = false;
cb_gas4.Enabled = false;
tb_concentration1.ReadOnly = true;
tb_concentration2.ReadOnly = true;
tb_concentration3.ReadOnly = true;
tb_concentration4.ReadOnly = true;
tb_target_concentration1.ReadOnly = true;
tb_target_concentration2.ReadOnly = true;
tb_target_concentration3.ReadOnly = true;
tb_target_concentration4.ReadOnly = true;
tb_concentration1.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration1_MouseDown);
tb_concentration2.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration2_MouseDown);
tb_concentration3.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration3_MouseDown);
tb_concentration4.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_concentration4_MouseDown);
tb_target_concentration1.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration1_MouseDown);
tb_target_concentration2.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration2_MouseDown);
tb_target_concentration3.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration3_MouseDown);
tb_target_concentration4.MouseDown -= new System.Windows.Forms.MouseEventHandler(Tb_target_concentration4_MouseDown);
}
string aisle1_setFlow = "";
string aisle2_setFlow = "";
string aisle3_setFlow = "";
string aisle4_setFlow = "";
/// <summary>
/// 写入设定流量值
/// </summary>
public void WriteSetflowValue()
{
timer.Enabled = false;
//showTimer.Enabled = false;
string[] setFlowValue = new string[4];
ComboBox[] comboBoxes = { cb_gas1, cb_gas2, cb_gas3, cb_gas4 };
if (sp.IsOpen)
{
for (int i = 0; i < comboBoxes.Length; i++)
{
if (comboBoxes[i].Text != "")
{
if (comboBoxes[i].Text != " ")
{
try
{
//string a = tb_setOutputFlow.Text;
setFlowValue[i] = DataProcessing.GetGasSetFlow(DataProcessing.gasR[i], Convert.ToDouble(tb_setOutputFlow.Text)).ToString();
//MessageBox.Show(setFlowValue[i]);
//Thread.Sleep(50);
if (isRun)
{
int index = i + 1;
if (ValveIsClose("通道" + index, "读关闭", "", index))
{
while (true) //等待消息接收完成
{
if (isReceived)
{
isReceived = false;
SerialPortHandle.SendSerialData(sp, DataProcessing.SendDataformat("通道" + index, "阀控控制", ""));
Thread.Sleep(100);
break;
}
}
//MessageBox.Show("通道" + index + "阀控打开");
}
DataProcessing.LogRecord("通道"+index+ "写入设定流量值");
byte[] sendData = DataProcessing.SendDataformat("通道" + index, "写入设定流量值", setFlowValue[i]);
while (true) //等待消息接收完成
{
if (isReceived)
{
isReceived = false;
SerialPortHandle.SendSerialData(sp, sendData);
Thread.Sleep(100);
break;
}
}
//MessageBox.Show("通道" + index + "写入设定流量值成功");
switch (i)
{
case 0:
aisle1_setFlow = setFlowValue[i];
break;
case 1:
aisle2_setFlow = setFlowValue[i];
break;
case 2:
aisle3_setFlow = setFlowValue[i];
break;
case 3:
aisle4_setFlow = setFlowValue[i];
break;
}
}
isRun = false;
}
catch (System.Exception ex)
{
MessageBox.Show("WriteSetflowValue函数:" + ex.Message);
}
}
}
}
timer.Enabled = true;
DataProcessing.LogRecord("定时器打开");
//showTimer.Enabled = true;
}
//showTimer.Enabled = true;
}
/// <summary>
/// 设置定时器
/// </summary>
//private void SetTimer()
//{
// //showTimer.Interval = 1000;
// //showTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerExecute);
// //showTimer.Enabled = true;
//}
///// <summary>
///// 定时器执行函数
///// </summary>
///// <param name="source"></param>
///// <param name="e"></param>
//private void TimerExecute(object source, System.Timers.ElapsedEventArgs e)
//{
// //showTimer.Enabled = false;
// if (sp.IsOpen)
// {
// ShowFlowAndConcentration();
// sp.DiscardInBuffer();
// sp.DiscardOutBuffer();
// }
// else
// {
// }
// //showTimer.Enabled = true;
//}
/// <summary>
/// 判断通道阀门状态
/// </summary>
/// <param name="aisleName">通道名称</param>
/// <param name="strFun">功能码</param>
/// <param name="data">数据值</param>
/// <param name="index">索引</param>
/// <returns>返回值为true,阀门打开;返回值为false,阀门关闭</returns>
public bool ValveIsClose(string aisleName, string strFun, string data, int index)
{
DataProcessing.LogRecord(aisleName + strFun);
while (true) //等待消息接收完成
{
if (isReceived)
{
isReceived = false;
SerialPortHandle.SendSerialData(sp, DataProcessing.SendDataformat(aisleName, strFun, data));
Thread.Sleep(100);
break;
}
}
if (index == 1)
{
//string A = p;
if (DataProcessing.aisle1[1] == 0x01)
{
return true;
}
else
{
return false;
}
}
else if (index == 2)
{
//string B = p;
if (DataProcessing.aisle2[1] == 0x01)
{
return true;
}
else
{
return false;
}
}
else if (index == 3)
{
//string C = p;
if (DataProcessing.aisle3[1] == 0x01)
{
return true;
}
else
{
return false;
}
}
else
{
//string D = p;
if (DataProcessing.aisle4[1] == 0x01)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 显示测量流量和测量浓度
/// </summary>
public void ShowFlowAndConcentration()
{
ComboBox[] comboBoxes = { cb_gas1, cb_gas2, cb_gas3, cb_gas4 };
TextBox[] tb_concentration = { tb_concentration1, tb_concentration2, tb_concentration3, tb_concentration4 };
TextBox[] tb_test_concentration = { tb_test_concentration1, tb_test_concentration2, tb_test_concentration3, tb_test_concentration4 };
double[] result = { 0, 0, 0, 0 };
double temp;
if (sp.IsOpen)
{
try
{
for (int i = 1; i <= 4; i++)
{
if (comboBoxes[i - 1].Text != "")
{
if (comboBoxes[i - 1].Text != " ")
{
DataProcessing.LogRecord("通道" + i + "读显示流量");
byte[] sendData = DataProcessing.SendDataformat("通道" + i, "读显示流量", "");
while (true) //等待消息接收完成
{
if (isReceived)
{
isReceived = false;
SerialPortHandle.SendSerialData(sp, sendData);
Thread.Sleep(100);
break;
}
}
switch (i)
{
case 1:
temp = CountFlowAndConcentration(DataProcessing.aisle1);
result[0] = Convert.ToInt32(temp / 4095 * 2000);
tb_flow1.Text = result[0].ToString();
//tb_test_concentration1.Text = (result[0] * double.Parse(tb_coefficient1.Text)).ToString();
tb_states1.Text = MeasuringCondition(aisle1_setFlow, temp);
break;
case 2:
temp = CountFlowAndConcentration(DataProcessing.aisle2);
result[1] = Convert.ToInt32(temp / 4095 * 2000);
tb_flow2.Text = result[1].ToString();
//tb_test_concentration2.Text = (result[1] * double.Parse(tb_coefficient2.Text)).ToString();
tb_states2.Text = MeasuringCondition(aisle2_setFlow, temp);
break;
case 3:
temp = CountFlowAndConcentration(DataProcessing.aisle3);
result[2] = Convert.ToInt32(temp / 4095 * 2000);
tb_flow3.Text = result[2].ToString();
//tb_test_concentration3.Text = (result[2] * double.Parse(tb_coefficient3.Text)).ToString();
tb_states3.Text = MeasuringCondition(aisle3_setFlow, temp);
break;
case 4:
temp = CountFlowAndConcentration(DataProcessing.aisle4);
result[3] = Convert.ToInt32(temp / 4095 * 2000);
tb_flow4.Text = result[3].ToString();
//tb_test_concentration4.Text = (result[3] * double.Parse(tb_coefficient4.Text)).ToString();
tb_states4.Text = MeasuringCondition(aisle4_setFlow, temp);
break;
}
}
}
}
DataProcessing.GetTestGasCoefficient(result);
for (int j = 0; j < comboBoxes.Length; j++)
{
if (comboBoxes[j].Text != "")
{
if (comboBoxes[j].Text != " ")
{
if (comboBoxes[j].Text == "N2")
{
//tb_test_concentration[j].Text = (double.Parse(tb_concentration[j].Text) * 1).ToString("#0.00");
}
else
{
tb_test_concentration[j].Text = (double.Parse(tb_concentration[j].Text) * DataProcessing.testGasR[j]).ToString("#0.00");
}
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show("ShowFlowAndConcentration函数:" + ex.Message);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="aisle"></param>
/// <returns></returns>
private static double CountFlowAndConcentration(byte[] aisle)
{
double temp = double.Parse(Convert.ToInt32(aisle[1] * 256 + aisle[2]).ToString());
if (temp > 4095)
{
temp = 0;
}
DataProcessing.LogRecord("读取的流量值为:" + temp);
return temp;
}
private void Bt_back_Click(object sender, EventArgs e)
{
//showTimer.Enabled = false;
//timerThread.Suspend();
bt_start.Enabled = true;
cb_gas1.Enabled = true;
cb_gas2.Enabled = true;
cb_gas3.Enabled = true;
cb_gas4.Enabled = true;
tb_concentration1.ReadOnly = false;
tb_concentration2.ReadOnly = false;
tb_concentration3.ReadOnly = false;
tb_concentration4.ReadOnly = false;
tb_target_concentration1.ReadOnly = false;
tb_target_concentration2.ReadOnly = false;
tb_target_concentration3.ReadOnly = false;
tb_target_concentration4.ReadOnly = false;
tb_maxOutputFlow.ReadOnly = false;
tb_setOutputFlow.ReadOnly = false;
tb_flow1.Text = "";
tb_flow2.Text = "";
tb_flow3.Text = "";
tb_flow4.Text = "";
tb_test_concentration1.Text = "";
tb_test_concentration2.Text = "";
tb_test_concentration3.Text = "";
tb_test_concentration4.Text = "";
tb_concentration1.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration1_MouseDown);
tb_concentration2.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration2_MouseDown);
tb_concentration3.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration3_MouseDown);
tb_concentration4.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_concentration4_MouseDown);
tb_target_concentration1.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration1_MouseDown);
tb_target_concentration2.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration2_MouseDown);
tb_target_concentration3.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration3_MouseDown);
tb_target_concentration4.MouseDown += new System.Windows.Forms.MouseEventHandler(Tb_target_concentration4_MouseDown);
if (sp.IsOpen)
{
timer.Enabled = false;
for (int i = 1; i <= 4; i++)
{
while (true) //等待消息接收完成
{
if (isReceived)
{
isReceived = false;
SerialPortHandle.SendSerialData(sp, DataProcessing.SendDataformat("通道" + i, "阀控关闭", ""));
Thread.Sleep(100);
break;
}
}
}
tb_states1.Text = "关闭";
tb_states2.Text = "关闭";
tb_states3.Text = "关闭";
tb_states4.Text = "关闭";
//tb_states1.ForeColor = System.Drawing.SystemColors.WindowText;
//tb_states2.ForeColor = System.Drawing.SystemColors.WindowText;
//tb_states3.ForeColor = System.Drawing.SystemColors.WindowText;
//tb_states4.ForeColor = System.Drawing.SystemColors.WindowText;
//sp.Close();
}
}
public static bool isReceived = false;
/// <summary>
/// 串口数据接收
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(50);
int bufferlen = sp.BytesToRead;
string readStr = "";
if (bufferlen > 0)
{
byte[] readbuffer = new byte[bufferlen];
sp.Read(readbuffer, 0, readbuffer.Length);
for (int i = 0; i < readbuffer.Length; i++)
{
readStr += readbuffer[i] + "/";
}
DataProcessing.LogRecord("串口接收到的数据:" + readStr);
Thread dataAnalysisThread = new Thread(() => DataProcessing.DataAnalysis(readbuffer));
dataAnalysisThread.IsBackground = true;
dataAnalysisThread.Start();
//DataProcessing.DataAnalysis(readbuffer);
isReceived = true;
}
}
/// <summary>
/// 机器初始化设置
/// </summary>
/// <param name="sp">通信的串口</param>
private static void MachineInitialization(SerialPort sp)
{
Queue initializtionQueue = new Queue();
initializtionQueue.Clear();
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道1", "阀控关闭", "")); //0
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道1", "读关闭", "")); //1
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道2", "阀控关闭", "")); //2
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道2", "读关闭", "")); //3
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道3", "阀控关闭", "")); //4
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道3", "读关闭", "")); //5
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道4", "阀控关闭", "")); //6
initializtionQueue.Enqueue(DataProcessing.SendDataformat("通道4", "读关闭", "")); //7
SendQueueMessage(sp,initializtionQueue);
}
/// <summary>
/// 发送队列消息
/// </summary>
/// <param name="sp">串口对象</param>
/// <param name="sendQueue">需要发送的消息队列</param>
private static void SendQueueMessage(SerialPort sp,Queue sendQueue)
{
if (sp.IsOpen)
{
isReceived = true;
foreach (byte[] sendData in sendQueue)
{
int index = GetIndexFromQueue(sendData, sendQueue);
//send: SerialPortHandle.SendSerialData(sp, sendData);
send: while (true) //等待消息接收完成
{
if (isReceived)
{
isReceived = false;
SerialPortHandle.SendSerialData(sp, sendData);
Thread.Sleep(50);
break;
}
}
if (index == 1)
{
if (DataProcessing.aisle1[1] != 0x01)
{
goto send;
}
}
else if (index == 3)
{
if (DataProcessing.aisle2[1] != 0x01)
{
goto send;
}
}
else if (index == 5)
{
if (DataProcessing.aisle3[1] != 0x01)
{
goto send;
}
}
else if (index == 7)
{
if (DataProcessing.aisle4[1] != 0x01)
{
goto send;
}
}
else
{
//continue;
}
//isReceived = false;
}
sendQueue.Clear();
}
}
/// <summary>
/// 获取队列索引值
/// </summary>
/// <param name="element">被索引的元素</param>
/// <param name="queue">索引队列</param>
/// <returns>索引值</returns>
private static int GetIndexFromQueue(byte[] element, Queue queue)
{
int index = 0;
if (queue.Count > 0)
{
for (int i = 0; i < queue.ToArray().Length; i++)
{
if (element == queue.ToArray()[i])
{
index = i;
break;
}
}
}
return index;
}
private void Timer_Tick(object sender, EventArgs e)
{
if (sp.IsOpen)
{
timer.Enabled = false;
ShowFlowAndConcentration();
sp.DiscardInBuffer();
sp.DiscardOutBuffer();
timer.Enabled = true;
}
}
/// <summary>
/// 获取测量状态
/// </summary>
/// <param name="setFlow">设定的流量值</param>
/// <param name="testFlow">测试的流量值</param>
/// <param name="textBox"></param>
/// <returns></returns>
private static string MeasuringCondition(string setFlow, double testFlow)
{
string status;
//textBox.ReadOnly = false;
double difference = System.Math.Abs(testFlow - double.Parse(setFlow));
if (double.Parse(setFlow) > 100)
{
if (difference > double.Parse(setFlow) * 0.05)
{
status = "异常";
//textBox.ForeColor = System.Drawing.Color.Red;
}
else
{
status = "正常";
//textBox.ForeColor = System.Drawing.Color.Chartreuse;
}
}
else
{
if (difference > 5)
{
status = "异常";
//textBox.ForeColor = System.Drawing.Color.Red;
}
else
{
status = "正常";
//textBox.ForeColor = System.Drawing.Color.Chartreuse;
}
}
//textBox.ReadOnly = true;
return status;
}
/// <summary>
/// 获取键盘窗口输入值
/// </summary>
/// <param name="textBox"></param>
private void GetKeyBoardValue(TextBox textBox)
{
KeyBoardForm keyBoard = new KeyBoardForm();
keyBoard.ShowDialog();
textBox.Text = keyBoardValue;
keyBoard.Dispose();
}
private void Bt_clear_Click(object sender, EventArgs e)
{
}
private void Tb_setOutputFlow_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_setOutputFlow);
}
private void Tb_concentration1_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_concentration1);
}
private void Tb_concentration2_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_concentration2);
}
private void Tb_concentration3_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_concentration3);
}
private void Tb_concentration4_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_concentration4);
}
private void Tb_target_concentration1_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_target_concentration1);
}
private void Tb_target_concentration2_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_target_concentration2);
}
private void Tb_target_concentration3_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_target_concentration3);
}
private void Tb_target_concentration4_MouseDown(object sender, MouseEventArgs e)
{
GetKeyBoardValue(tb_target_concentration4);
}
private void Tb_setOutputFlow_TextChanged(object sender, EventArgs e)
{
if (sp.IsOpen)
{
if (!bt_start.Enabled)
{
WriteSetflowValue();
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (sp.IsOpen)
{
sp.Close();
}
}
}
}
复制代码
所有资料51hei提供下载:
GCC.rar
(326.54 KB, 下载次数: 22)
2019-9-16 09:36 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1