将serialport变量设置为可在线程中访问的全局变量

时间:2014-02-26 16:12:52

标签: c#

    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()
        {
            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))
                    {
                        Console.WriteLine(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));
            thread.Start();
        }
        #endregion
    }
}

您好。我有这个代码,我想把serial.Write()放在这个函数中:

if (Convert.ToBoolean(Text))
                {
                    Console.WriteLine(Text);
                    //serial.Write();
                }

你能帮我把这个串行全局化,以便不存在这个错误:当前上下文中不存在'serial'这个名字吗?

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是向调用该方法的MainWindow类添加方法,并将窗口实例传递给ThreadJob

即。 修改ThreadJob以接受实例作为参数:

public static void ThreadJob(MainWindow mainWindow)

将其放在MainWindow某处:

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

修改您的Thread创建以传递当前窗口实例:

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

现在Console.WriteLine(text)之后你应该能够写下:

mainWindow.SerialWrite(text);

我没有测试过这个解决方案,因此可能需要进行一些清理。这可能不是最好的方法,因为serial是您的窗口实例的一部分(您可以将它们分开)。