当前位置:编程学习 > C#/ASP.NET >>

关于C#中System.Timers.Timer的问题

  用 System.Timers.Timer  time  =  new System.Timers.Timer()定义了一个 time,
  然后用语句 time.Elapsed += new ElapsedEventHandler(time_Elapsed);Elapsed触发事件time_Elapsed,但time_Elapsed事件中写之前定义的函数,就会出现这样的错误:非静态的字段、方法或属性“boxing_.Form1.ReadDataOnetime()”要求对象引用请问,应该怎样解决? --------------------编程问答-------------------- 請你把程序易做图給我看看出了什麼問題 --------------------编程问答-------------------- time_Elapsed这个方法是怎么写的?

我看主要是方法的类型不统一造成,
ReadDataOnetime这个方法是static类型的嘛??

不行把你所有的代码都贴上 --------------------编程问答-------------------- 代码如下:

 public partial class Form1 : Form
    {
      System.Timers.Timer  time  =  new System.Timers.Timer();
      private void button4_Click(object sender, EventArgs e)
        {
            string ComNum = this.Ports.Text;
            int BaudRate = Convert.ToInt32(this.baudrate.Text);
            int DataBits = Convert.ToSByte(this.databit.Text);
            //保存串口号,波特率,数据位的参数
              //int  StopBits = this.TZW.Text;
            //int ParityBit = this.JOJY.Text;//保存相关参数
            if (!serialPort1.IsOpen)
            {
                //给串口通信的属性赋值
                  serialPort1.PortName = ComNum;
                serialPort1.BaudRate = BaudRate;
                //因为停止位与奇偶校验位都为枚举类型的数据,赋值时的转换需注意
                serialPort1.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof  (System.IO.Ports.Parity), this.paritybit.Text);
                serialPort1.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), this.stopbit.Text);
                //试运行TRY代码,否则运行CATCH代码                  
                try
                {
                    serialPort1.Open();
                    button4.Text = "关闭串口";
                    Ports.Enabled = false; //控件对用户不作响应
                    baudrate.Enabled = false;
                    databit.Enabled = false;
                    stopbit.Enabled = false;
                    paritybit.Enabled = false;
                   //timer1.Enabled = true; //打开串口时立即启动时钟
                     time.Interval = 1000;
                   time.AutoReset = true;
                   time.Enabled = true;
                   time.Elapsed += new ElapsedEventHandler(time_Elapsed);                   
                   
                    
                }
                catch
                {
                    MessageBox.Show("串口打开失败,串口可能已被占用", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                serialPort1.Close();
                button4.Text = "打开串口";
                Ports.Enabled = true;
                baudrate.Enabled = true;
                databit.Enabled = true;
                stopbit.Enabled = true;
                paritybit.Enabled = true;

            }

        }
#region**从串口缓存区读一次数据**
        public  void  ReadDataOnetime()
        {

            if (serialPort1.IsOpen)
            {
                int n;                            //从串口中采集的字节数
                   n = serialPort1.Read(data,0,Num); //从串口读数据,

               for (int i = 0; i < Num; i++)
                {
                    ReceiveData[i] = data[i];      //数据由Byte转换为float型
                      foreach (int i in data)           //将接收到的数据显示在文本框中
                {
                    jieshou.Text = jieshou.Text + " " + Convert.ToString(i);
                }
    #region**公有函数——将一定时间间隔收集到的数据加到集合类List<>中**
        public void AddData()
        {
                              
            float[] y = ReceiveData;
            for (int i = 0; i < Num; i++)
            {
                Time = Time + SampleTime;
                ListX.Add(Time);
                ListY.Add(y[i]);
               
            }
            
 
        }

        #endregion
private static void time_Elapsed(object source, ElapsedEventArgs e)
        {
            
            ReadDataOnetime() ;                            //每隔一定时间读一次缓存区的数据
            AddData();                                    //将数据加到集合类中
            Color color1 = Color.Red;
            Color color2 = Color.Blue;                    //设置画笔的颜色
            int width = 1;                                //画笔的粗细
            //zGraph1.f_LoadOnePix(ref ListX, ref ListY, color1, width, LineJoin.Round, LineCap.Round, ZGraph.DrawStyle.beziers);
            // zGraph1.f_AddPix(ref ListX, ref ListY, color1, width, LineJoin.Round, LineCap.ArrowAnchor, ZGraph.DrawStyle.beziers);
            zGraph1.f_AddPix(ref ListX, ref ListY, color2, width, LineJoin.Round, LineCap.Round, ZGraph.DrawStyle.Line);

            zGraph1.Refresh();                           //更新图像 
             
        } --------------------编程问答--------------------
引用 3 楼 fangshuozjjh1991 的回复:
代码如下:

 public partial class Form1 : Form
  {
  System.Timers.Timer time = new System.Timers.Timer();
  private void button4_Click(object sender, EventArgs e)
  {
  string ComNum = this.Ports.T……

红色的字体是time有关的代码,其他的是读取串口采样的数据,然后绘制实时曲线。还有一个问题是当串口接收到数据后,需要些个函数去读取串口中的数据嘛?有人说是直接有个类似on_event的函数,不用再写函数了~~望高手解答疑惑,万分感谢~~~ --------------------编程问答--------------------
引用 4 楼 fangshuozjjh1991 的回复:
引用 3 楼 fangshuozjjh1991 的回复:
代码如下:

public partial class Form1 : Form
{
System.Timers.Timer time = new System.Timers.Timer();
private void button4_Click(object sender, EventArgs e)
{
string Co……

前面还有的变量定义是
public static int Num = 15;                                                    //每次采集的数据个数
        public Byte[] data=new byte[Num] ;                                            //读取串口缓存区时用的字节数组
        public float[] ReceiveData = new float[Num];                                  //坐标轴Y的数据集合,
        public float Time = 0;                                                        //采样时间轴
        public float SampleTime = 8;                                                  //采样时间间隔
        List<float> ListX = new List<float>();                                        //用集合类存储时间集合
        List<float> ListY = new List<float>();                                       //用集合类存储采集的数据集合
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,