2014-02-27 34 views
-1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.IO.Ports; 
using System.Threading; 
using System.Windows.Threading; 
using System.Data.SQLite; 

namespace Datalogging 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 

    public class ThreadExample 
    { 

     public static void ThreadJob(MainWindow mainWindow) 
     { 
      string dBConnectionString = @"Data Source = C:\Users\johnmark\Documents\Visual Studio 2012\Projects\SerialTrial\SerialTrial\bin\Debug\employee.sqlite;"; 
      SQLiteConnection sqliteCon = new SQLiteConnection(dBConnectionString); 
      //open connection to database 
      try 
      { 
       sqliteCon.Open(); 
       SQLiteCommand createCommand = new SQLiteCommand("Select empID from EmployeeList", sqliteCon); 
       SQLiteDataReader reader; 
       reader = createCommand.ExecuteReader(); 


       //richtextbox2.Document.Blocks.Clear(); 
       while (reader.Read()) 
       { 

        string Text = (String.Format("{0}", Object.Equals(definition.buffering, reader.GetValue(0)))); 
        if (Convert.ToBoolean(Text)) 
        { 
         mainWindow.SerialWrite('s'); 
         Console.WriteLine(Text); 
         //richtextbox1.Document.Blocks.Add(new Paragraph(new Run(Text))); 
        } 
       } 
       sqliteCon.Close(); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 


    } 


    public partial class MainWindow : Window 
    { 

     //string received_data; 
     //Thread readThread = new Thread(Read); 
     FlowDocument mcFlowDoc = new FlowDocument(); 
     Paragraph para = new Paragraph(); 
     SerialPort serial = new SerialPort(); 

     public MainWindow() 
     { 
      InitializeComponent(); 


      combobox1.Items.Insert(0, "Select Port"); 
      combobox1.SelectedIndex = 0; 
      string[] ports = null; 
      ports = SerialPort.GetPortNames(); 

      // Display each port name to the console. 
      int c = ports.Count(); 
      for (int i = 1; i <= c; i++) 
      { 
       if (!combobox1.Items.Contains(ports[i - 1])) 
       { 
        combobox1.Items.Add(ports[i - 1]); 
       } 

      } 


     } 


     private void combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 

     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       if ((string)button2.Content == "Connect") 
       { 
        string myItem = combobox1.SelectedItem.ToString(); 
        if (myItem == "Select Port") 
        { 
         MessageBox.Show("Select Port"); 
        } 
        else 
        { 
         serial.PortName = myItem; 
         serial.Open(); 
         button2.Content = "Disconnect"; 
         textbox2.Text = "Serial Port Opened"; 
         serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived); 

        } 
       } 

       else 
       { 
        serial.Close(); 
        button2.Content = "Connect"; 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 


     #region Receiving 


     public void port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
     { 
      int bytes = serial.BytesToRead; 
      byte[] buffer = new byte[bytes]; 
      serial.Read(buffer, 0, bytes); 
      foreach (var item in buffer) 
      { 
       Console.Write(item.ToString()); 
      } 
      definition.buffering = BitConverter.ToInt64(buffer, 0); 
      Console.WriteLine(); 
      Console.WriteLine(definition.buffering); 
      Console.WriteLine(); 
      Thread thread = new Thread(new ThreadStart(ThreadExample.ThreadJob(this))); 
      thread.Start(); 
      thread.Join(); 
     } 
     #endregion 

     public void WriteSerial(string text) 
     { 
      serial.Write(text); 
     } 


    } 
} 

嗨,大家好。任何人都可以幫助我在這段代碼中出了什麼問題?它顯示此錯誤: 錯誤2'Datalogging.MainWindow'沒有包含'SerialWrite'的定義,也沒有找到接受類型'Datalogging.MainWindow'的第一個參數的擴展方法'SerialWrite'(你是否缺少一個using指令或程序集引用?)C#將串行傳遞給類

錯誤3方法名稱預計

我該怎麼解決呢?請編輯代碼並將其張貼在這裏作爲您的答案,謝謝。

回答

0

你的方法調用mainWindow.SerialWrite('s');更改爲mainWindow.WriteSerial('s');,以適應方法名在此聲明:

public void WriteSerial(string text) 

你倒這兩個詞。


對於您的「方法名稱預計」,我想它在port_DataReceived。您需要將委託傳遞給該線程,但是您沒有正確執行。

代替

Thread thread = new Thread(new ThreadStart(ThreadExample.ThreadJob(this))); 

(你不能直接傳遞的方法作爲參數,可以只使用函數指針),可以使用此語法來傳遞代表:

Thread thread = new Thread(new ThreadStart(() => ThreadExample.ThreadJob(this))); 

請注意,new TreadStart是多餘的,new Thread(() => ThreadExample.ThreadJob(this));將完成這項工作。

+0

哦。抱歉。我沒看到它。哈哈。現在感謝它的修復。這個怎麼樣?錯誤方法名稱預計 此錯誤指向此處:線程線程=新線程(新線程啓動(ThreadExample.ThreadJob(this))); – iaskyou

+0

@iaskyou編輯我的答案,沒有確切的線很難說,但我想它應該在那裏。 –

+0

阿羅哈。非常感謝。它現在正在工作。 (y)的 – iaskyou