2014-03-28 61 views
0

我有這樣的代碼:字符串比較返回false eventhough它們是同一個

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; 
using System.Text.RegularExpressions; 

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

    public class ThreadExample 
    { 
     FlowDocument mcFlowDoc = new FlowDocument(); 
     Paragraph para = new Paragraph(); 

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

       int count = 0; 
       //richtextbox2.Document.Blocks.Clear(); 
       List<string> mystring = new List<string>(); 
       while (reader.Read()) 
       { 
        count++; 
        string Text = (String.Format("{0}", Object.Equals(Variables.buffering, reader.GetString(0)))); 

        Console.WriteLine(Text); 

        mystring.Add(Text); 
        if (Convert.ToBoolean(Text)) 
        { 
         mystring.Add("1"); 
         //string myText = new TextRange(mainWindow.richtextbox2.Document.ContentStart, mainWindow.richtextbox2.Document.ContentEnd).Text; 

         //replace two or more consecutive spaces with a single space, and 
         //replace two or more consecutive newlines with a single newline. 
         //var str = Regex.Replace(myText, @"(|\r?\n)\1+", "$1", RegexOptions.Multiline); 
         //Int64 id = reader.GetInt64(0); 
         string fname = reader.GetString(1); 
         string sname = reader.GetString(2); 
         DateTime saveNow = DateTime.Now; 

         mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run(saveNow.ToString()))))); 
         mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(": "))); 
         mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(fname))); 
         mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(" "))); 
         mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(sname))); 
         mainWindow.Dispatcher.Invoke(new Action(() => mainWindow.richtextbox2.AppendText(" granted access to gate 1")));      
         string text = "s"; 
         mainWindow.WriteSerial(text); 
         //Console.WriteLine(Text); 

         //Console.WriteLine("ID = {0}, Name = {1}, Surname = {2}, Age = {3}", id, fname, sname, age); 
         //Console.WriteLine("{0}\t{1}\t{2}\t{3}", reader.GetInt64(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)); 

        } 
       } 
sqliteCon.Close(); 

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

    } 

public partial class MainWindow : Window 
    { 
     SerialPort serial = new SerialPort(); 
     //string received_data; 
     //Thread readThread = new Thread(Read); 
     FlowDocument mcFlowDoc = new FlowDocument(); 
     Paragraph para = new Paragraph(); 


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

      } 
     } 
     string dBConnectionString = @"Data Source = C:\Users\Documents\Visual Studio 2012\Projects\SerialTrial\SerialTrial\bin\Debug\CompanyList.sqlite;"; 

     static int count = 0; 

private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      count++; 
      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]); 
       } 

      } 

     } 
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
     { 
      int bytes = serial.BytesToRead; 
      Console.WriteLine(bytes); 
      if (bytes == 14) 
      { 
       byte[] buffer = new byte[bytes]; 
       serial.Read(buffer, 0, bytes); 
       System.String ID = System.Text.Encoding.Default.GetString(buffer); 
       //System.String ID = new String(System.Text.Encoding.UTF8.GetChars(buffer)); 
       Console.WriteLine(ID); 
       foreach (var item in ID) 
       { 
        Console.WriteLine(item.ToString()); 
       } 
       /* 
       Console.WriteLine(); 
       string s2 = BitConverter.ToString(buffer); 
       string tempAry = s2.Replace("-", ""); 
       Console.WriteLine(tempAry); 

       Variables.buffering = BitConverter.ToInt64(buffer, 0); 
       */ 
       Variables.buffering = ID; 
       Console.WriteLine(); 
       //Console.WriteLine(Variables.buffering); 
       Thread thread = new Thread(new ThreadStart(() => ThreadExample.ThreadJob(this))); 
       thread.Start(); 
       thread.Join(); 
      } 

     } 

我送0x02,0x37,0x30,0x30,0x30,0x32,0x42,0x34,0x30,0x38,0x45,0x39 ,相當於70002B408E95的串口中的0x35,0x03。此ID代碼也是我的數據庫中的70002B408E95。爲什麼我的compare.Equals返回false事件,儘管這兩個字符串都是真的?

Variables.buffering在我的課堂上使緩衝全局。

class Variables 
    { 
     public static System.String buffering; 

    } 

請幫忙。由於

+0

凡在你的代碼是錯誤出現?你提供了很多代碼......你有沒有嘗試調試檢查員告訴你什麼? – Zache

+0

這是一個很多代碼來支持你相對簡單的問題。你有沒有試過一個顯示相同問題的小型控制檯應用程序? – Steve

+1

在調用Object.Equals時,** Variables.buffering'和'reader.GetString(0)'的內容究竟是什麼?有什麼'.ToCharArray()'輸出?他們持有什麼?另外:你應該在這裏使用'==':'Variables.buffering == reader.GetString(0)' –

回答

1

您收到0x02,0x37,0x30,0x30,0x30,0x32,0x42,0x34,0x30,0x38,0x45,0x39,0x35,0x03作爲您的數據。我想0x020x03是種開始停止字節。它們之間有什麼是你的ASCII字符串。因此,你需要忽略這些開始停止字節,通過更換代碼:

System.String ID = System.Text.Encoding.Default.GetString(buffer); 

System.String ID = System.Text.Encoding.Default.GetString(buffer, 1, 12); // start reading from byte at 1st position and take 12 of them 
0

的Object.Equals比較地址

Object.Equals(Variables.buffering, reader.GetString(0)) 

與String.Equals嘗試比較字符串

編輯:由於在下面的評論中指出,這不僅是不行的,但也錯了,所以關閉我的答案

+0

我嘗試改變爲這一個:string Text =(String.Format(「{0}」,String.Equals(Variables.buffering,reader.GetString(0)))); ----但它仍然返回false – jbat21

+1

如果您使用Object.Equals和字符串它將調用string.Equals – Zache

+0

所以我將如何檢查兩個字符串的相似性?一個來自串口,另一個來自我的sqlite數據庫 – jbat21

0

通常我們比較值類型變量或引用類型變量。如果你比較int,將int類型的變量與int類型串起來,那麼你應該使用類型值1 = 1或者「a」=「b」,但是當你和對象比較時,你應該使用引用類型如1.equal (1)或a.equal(b)

相關問題