Alex Gordon
Alex Gordon

Reputation: 60871

serialport error

i have the following code:

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")));
        }
    }
}

and for all the serialport1 lines i am getting this error:

Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.serialPort1' C:\Users\alexluvsdanielle\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 23 17 WindowsFormsApplication1

what am i doing wrong?

Upvotes: 0

Views: 810

Answers (2)

Matt DeKrey
Matt DeKrey

Reputation: 11942

Personally, I'd remove the class ThreadWork and move the DoWork method out to the parent class and remove the static modifier from it. That is, do the following:

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")));
        }
    }
}

This allows you to leave serialPort1 on your Forms designer and still use the thread model.

Upvotes: 1

egrunin
egrunin

Reputation: 25083

It's saying that serialPort1 is not static, and therefore cannot be referenced from DoWork(), a static function.

You have to make serialPort1 a static for this design to work. That probably means taking it off the form and declaring it in the codebehind instead. Then you have to instantiate it when the Form is first constructed.

Upvotes: 2

Related Questions