2010-06-13 96 views
0

我有以下代碼:的serialport錯誤

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

我在做什麼錯?

+0

你有新的串口嗎? – kenny 2010-06-13 22:51:10

+1

聲明serialPort1在哪裏? – 2010-06-13 22:51:26

+0

我在我的表格上有串行端口 – 2010-06-13 22:59:08

回答

2

這就是說serialPort1不是靜態的,因此不能從DoWork()這個靜態函數中引用。

您必須使serialPort1成爲靜態設計才能運行。這可能意味着將其從表單中取出並在代碼隱藏中聲明它。然後,你必須在Form首次構建時實例化它。

+0

egrunin im抱歉,你能告訴我如何做到這一點 – 2010-06-14 00:19:19

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你的窗體設計,仍然使用的線程模型。