C# 串口通信:为什么我的上位机界面有时数据刷新,有时就不刷新。望高手指点我该怎样修改?谢谢!
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.Runtime.InteropServices;
namespace SENSOR
{
public partial class Sensor : Form
{
public int cnt = 0;
public float Data_Temp1 = 0, Data_Temp2 = 0, Data_Temp3 = 0;
public float Date_Humi1 = 0, Date_Humi2 = 0, Date_Humi3 = 0;
public float Data_ADTemp1 = 0, Data_ADTemp2 = 0, Data_ADTemp3 = 0;
public double Data_Weight = 0;
public int Check_Sum = 0;
/**********************************Begin****************************************/
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct DATA_STA_STRUCT
{
//前导
public byte cLead1;
public byte cLead2;
//数据-------------------
public byte iTempH1; //
public byte iTempL1; //
public byte iTempH2; //
public byte iTempL2; //
public byte iTempH3; //
public byte iTempL3; //
public byte iHumiH1; //
public byte iHumiL1; //
public byte iHumiH2; //
public byte iHumiL2; //
public byte iHumiH3; //
public byte iHumiL3; //
public byte iADTempH1;//
public byte iADTempL1;//
public byte iADTempH2;//
public byte iADTempL2;//
public byte iADTempH3;//
public byte iADTempL3;//
public byte iWeightH1;
public byte iWeightH2;
public byte iWeightL;
public byte SD_Miniture;
public byte SD_Hour;
//校验和
public UInt16 iCheckSum;
//包尾
public byte cTail1;
public byte cTail2;
} ;
//GraphPane myPane;
public EventHandler T1LedShow;//温度
public EventHandler T2LedShow;
public EventHandler T3LedShow;
public EventHandler H1LedShow; //湿度
public EventHandler H2LedShow;
public EventHandler H3LedShow;
public EventHandler AD_T1LedShow;
public EventHandler AD_T2LedShow;
public EventHandler AD_T3LedShow;
public EventHandler WeightLedShow;
public EventHandler T1MeterShow;//温度刻度盘
private void H1LedShowData(object Obj,EventArgs e)
{
double DataHumi1 = Convert.ToDouble(Obj);
H1Led.Text = Convert.ToDouble(Obj).ToString("00.00");
H1Meter.CircularGauges["Default"].Pointers["Default"].Value = DataHumi1;
}
。。。
private void WeightLedShowData(object Obj, EventArgs e)
{
double DataWeight = Convert.ToDouble(Obj);
WeightLed.Text = Convert.ToDouble(Obj).ToString("0000.00");
WeightMeter.CircularGauges["Default"].Pointers["Default"].Value = DataWeight;
}
public Sensor()
{
InitializeComponent();
T1LedShow += T1LedShowData;
T2LedShow += T2LedShowData;
T3LedShow += T3LedShowData;
H1LedShow += H1LedShowData;
H2LedShow += H2LedShowData;
H3LedShow += H3LedShowData;
AD_T1LedShow += AD_T1LedShowData;
AD_T2LedShow += AD_T2LedShowData;
AD_T3LedShow += AD_T3LedShowData;
WeightLedShow += WeightLedShowData;
}
private void timer1_Tick(object sender, EventArgs e)
{
DateTime currentTime = DateTime.Now;
TimeLed.Caption = currentTime.ToString("HH:mm:ss").ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] portList = System.IO.Ports.SerialPort.GetPortNames();
for (int i = 0; i < portList.Length; ++i)
{
string name = portList[i];
ComboBox1.Items.Add(name);
ComboBox1.Text = name;
}
}
private void my_Sendbtn_Click(object sender, EventArgs e)
{
byte[] Temp = { 99 };
my_serialPort.Write(Temp, 0, 1);
}
private void button1_Click(object sender, EventArgs e)
{
my_serialPort.PortName = ComboBox1.Text;
my_serialPort.BaudRate = Convert.ToInt32(CommaskedTextBox.Text);
my_serialPort.Open();
ComboBox1.Enabled = false;
CommaskedTextBox.Enabled = false;
}
/// <summary>
/// byte数组转结构体
/// </summary>
/// <param name="bytes">byte数组</param>
/// <param name="type">结构体类型</param>
/// <returns>转换后的结构体</returns>
public static object BytesToStuct(byte[] bytes, Type type)
{
int size = Marshal.SizeOf(type); //得到结构体的大小
if (size > bytes.Length) //byte数组长度小于结构体的大小
{
return null; //返回空
}
IntPtr structPtr = Marshal.AllocHGlobal(size); //分配结构体大小的内存空间
Marshal.Copy(bytes, 0, structPtr, size); //将byte数组拷到分配好的内存空间
object obj = Marshal.PtrToStructure(structPtr, type); //将内存空间转换为目标结构体
Marshal.FreeHGlobal(structPtr); //释放内存空间
return obj; //返回结构体
}
unsafe
private void my_serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
DATA_STA_STRUCT ReceiveData = new DATA_STA_STRUCT(); ;
byte[] ReadBuffer = new byte[sizeof(DATA_STA_STRUCT)];
my_serialPort.Read(ReadBuffer, 0, sizeof(DATA_STA_STRUCT));
ReceiveData = (DATA_STA_STRUCT)(BytesToStuct(ReadBuffer, ReceiveData.GetType()));//数据放入结构体中
if ((ReceiveData.cLead1 == 0xaa) && (ReceiveData.cLead2 == 0x55))
{
Data_Temp1 = (float)ReceiveData.iTempH1 + ((float)ReceiveData.iTempL1) / 100f;
Data_Temp2 = (float)ReceiveData.iTempH2 + ((float)ReceiveData.iTempL2) / 100f;
Data_Temp3 = (float)ReceiveData.iTempH3 + ((float)ReceiveData.iTempL3) / 100f;
Date_Humi1 = (float)ReceiveData.iHumiH1 + ((float)ReceiveData.iHumiL1) / 100f;
Date_Humi2 = (float)ReceiveData.iHumiH2 + ((float)ReceiveData.iHumiL2) / 100f;
Date_Humi3 = (float)ReceiveData.iHumiH3 + ((float)ReceiveData.iHumiL3) / 100f;
Data_ADTemp1 = (float)ReceiveData.iADTempH1 + ((float)ReceiveData.iADTempL1) / 100f;
Data_ADTemp2 = (float)ReceiveData.iADTempH2 + ((float)ReceiveData.iADTempL2) / 100f;
Data_ADTemp3 = (float)ReceiveData.iADTempH3 + ((float)ReceiveData.iADTempL3) / 100f;
Data_Weight = (double)ReceiveData.iWeightH1 * 100d + (double)ReceiveData.iWeightH2 + ((double)ReceiveData.iWeightL) / 100d;
Check_Sum = ReceiveData.iTempH1 + ReceiveData.iTempL1 + ReceiveData.iTempH2 + ReceiveData.iTempL2 + ReceiveData.iTempH3 + ReceiveData.iTempL3 + ReceiveData.iHumiH1 + ReceiveData.iHumiL1 + ReceiveData.iHumiH2 + ReceiveData.iHumiL2 + ReceiveData.iHumiH3 + ReceiveData.iHumiL3 + ReceiveData.iADTempH1 + ReceiveData.iADTempL1 + ReceiveData.iADTempH2 + ReceiveData.iADTempL2 + ReceiveData.iADTempH3 + ReceiveData.iADTempL3 + ReceiveData.iWeightH1 + ReceiveData.iWeightH2 + ReceiveData.iWeightL;
}
if ((ReceiveData.cTail1 == 0x66) && (ReceiveData.cTail2 == 0xbb) && (ReceiveData.iCheckSum == Check_Sum ))
{
this.Invoke(T1LedShow, Data_Temp1, new EventArgs());
this.Invoke(H1LedShow, Date_Humi1, new EventArgs());
this.Invoke(T2LedShow, Data_Temp2, new EventArgs());
this.Invoke(H2LedShow, Date_Humi2, new EventArgs());
this.Invoke(T3LedShow, Data_Temp3, new EventArgs());
this.Invoke(H3LedShow, Date_Humi3, new EventArgs());
this.Invoke(AD_T1LedShow, Data_ADTemp1, new EventArgs());
this.Invoke(AD_T2LedShow, Data_ADTemp2, new EventArgs());
this.Invoke(AD_T3LedShow, Data_ADTemp3, new EventArgs());
this.Invoke(WeightLedShow, Data_Weight, new EventArgs());
}
//my_serialPort.DiscardInBuffer();
}
}
} --------------------编程问答-------------------- http://topic.csdn.net/u/20110611/16/2c5e2c5e-66e6-4daf-b404-2c46e751dc09.html --------------------编程问答-------------------- “有时数据刷新,有时就不刷新”,这个不难解释:有些数据上次跟这次有改变,有些没改变! --------------------编程问答-------------------- 举一个例子,函数 H1LedShowData
将GaugePointer集合里的Pointer的DampeningSweep的值=0
private void H1LedShowData(object Obj,EventArgs e)
{
double DataHumi1 = Convert.ToDouble(Obj);
H1Led.Text = Convert.ToDouble(Obj).ToString("00.00");
H1Meter.CircularGauges["Default"].Pointers["Default"].Value = DataHumi1;
Application.DoEvents();
}
--------------------编程问答-------------------- 有可能串口的通讯速度太快,UI还没来得及的刷新, GaugePointer如果不设置指针的扇扫时间它就会一直处于待加速状态,所以会看到一直不刷新 --------------------编程问答-------------------- 谢谢指点,我修改试试~
补充:.NET技术 , C#