我有这段代码:
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();
}
}
我在相当于70002B408E95的串口中发送0x02,0x37,0x30,0x30,0x30,0x32,0x42,0x34,0x30,0x38,0x45,0x39,0x35,0x03。此ID代码在我的数据库中也是70002B408E95。为什么我的compare.Equals返回false,两个字符串都是真的?
Variables.buffering在我的班级中使缓冲全局化。
class Variables
{
public static System.String buffering;
}
请帮忙。感谢
答案 0 :(得分:1)
您收到0x02,0x37,0x30,0x30,0x30,0x32,0x42,0x34,0x30,0x38,0x45,0x39,0x35,0x03
作为您的数据。我猜 0x02 和 0x03 是开始和停止字节。它们之间是你的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
答案 1 :(得分:0)
Object.Equals比较地址
Object.Equals(Variables.buffering, reader.GetString(0))
尝试使用String.Equals来比较字符串
编辑:正如下面的评论所指出的,这不仅不起作用,而且也是错误的,所以不要回答我的答案
答案 2 :(得分:0)
通常我们比较值类型变量或引用类型变量。如果比较int,string类型的变量与int,string那么你应该选择值类型,如1 = 1或&#34; a&#34; =&#34; b&#34; ,但是当你与对象比较时,你应该使用引用类型,如1.equal(1)或a.equal(b)