首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >串口错误

串口错误
EN

Stack Overflow用户
提问于 2010-06-14 06:49:07
回答 2查看 686关注 0票数 0

我有以下代码:

代码语言:javascript
复制
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需要对象引用

我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-06-14 07:07:26

这说明serialPort1不是静态的,因此不能从静态函数DoWork()中引用。

您必须将serialPort1设置为static才能使此设计生效。这可能意味着将其从表单中删除,并在代码后台中声明它。然后,您必须在第一次构造表单时将其实例化。

票数 2
EN

Stack Overflow用户

发布于 2010-06-14 08:41:31

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

代码语言:javascript
复制
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保留在窗体设计器上,并仍然使用线程模型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3034197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档