serialport错误

时间:2010-06-13 22:49:07

标签: c#

我有以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class ThreadWork
        {
            public static void DoWork()
            {
serialPort1 = new SerialPort();
                serialPort1.Open();
                serialPort1.Write("AT+CMGF=1\r\n");
                //Thread.Sleep(500);
                serialPort1.Write("AT+CNMI=2,2\r\n");
                //Thread.Sleep(500);
                serialPort1.Write("AT+CSCA=\"+4790002100\"\r\n");
                //Thread.Sleep(500);
                serialPort1.DataReceived += serialPort1_DataReceived_1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
            Thread myThread = new Thread(myThreadDelegate);
            myThread.Start();
        }

        private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string response = serialPort1.ReadLine();
            this.BeginInvoke(new MethodInvoker(() => textBox1.AppendText(response + "\r\n")));
        }
    }
}

并且对于所有serialport1行,我收到此错误:

错误1非静态字段,方法或属性'WindowsFormsApplication1.Form1.serialPort1'需要对象引用C:\ Users \ alexluvsdanielle \ AppData \ Local \ Temporary Projects \ WindowsFormsApplication1 \ Form1.cs 23 17 WindowsFormsApplication1

我做错了什么?

2 个答案:

答案 0 :(得分:2)

它说serialPort1不是静态的,因此无法从静态函数DoWork()引用。

您必须使serialPort1成为静态才能使此设计正常工作。这可能意味着将其从表单中删除并在代码隐藏中声明它。然后,您必须在首次构建表单时对其进行实例化。

答案 1 :(得分:1)

就个人而言,我会移除班级ThreadWork并将DoWork方法移至父类,并从中删除static修饰符。也就是说,执行以下操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void DoWork()
        {
serialPort1 = new SerialPort();
            serialPort1.Open();
            serialPort1.Write("AT+CMGF=1\r\n");
            //Thread.Sleep(500);
            serialPort1.Write("AT+CNMI=2,2\r\n");
            //Thread.Sleep(500);
            serialPort1.Write("AT+CSCA=\"+4790002100\"\r\n");
            //Thread.Sleep(500);
            serialPort1.DataReceived += serialPort1_DataReceived_1;
        }           

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadStart myThreadDelegate = new ThreadStart(DoWork);
            Thread myThread = new Thread(myThreadDelegate);
            myThread.Start();
        }

        private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string response = serialPort1.ReadLine();
            this.BeginInvoke(new MethodInvoker(() => textBox1.AppendText(response + "\r\n")));
        }
    }
}

这允许您在表单设计器上保留serialPort1并仍然使用线程模型。