使用C#清理RFID阅读器中的数据

时间:2017-05-24 03:20:40

标签: c# rfid

我能够使用下面的代码从RFID阅读器读取数据,但我得到的输出有一个特殊的字符,第二个问题是我有一个来自我公司的现有SQL数据库,里面的数据与什么不匹配我正进入(状态。我的RFID阅读器品牌是SMARTCODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace RFID
{
    public partial class Form1 : Form
    {


        public string Output;
        SerialPort mySerialPort = new SerialPort("COM1");
        private delegate void SetTextDeleg(string data);
        public Form1()
        {
            InitializeComponent();

            SettingR232();

        }


        private void SettingR232()
        {
            try
            {
                mySerialPort.BaudRate = 9600;
                mySerialPort.Parity = Parity.None;
                mySerialPort.StopBits = StopBits.One;
                mySerialPort.DataBits = 8;
                mySerialPort.Handshake = Handshake.None;
                mySerialPort.ReadTimeout = 2000;
                mySerialPort.WriteTimeout = 500;
                mySerialPort.DtrEnable = true;
                mySerialPort.RtsEnable = true;

                mySerialPort.Open();

                mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                mySerialPort.DataReceived += DataReceivedHandler;

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


        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {   

            SerialPort sp = (SerialPort)sender;
            System.Threading.Thread.Sleep(500);
            string indata = sp.ReadExisting();
            this.BeginInvoke(new SetTextDeleg(DisplayOutput), new object[] { indata });

        }


        public void DisplayOutput(string displayData)
        {
            string compdata = "";

            if (compdata != displayData)
            {
               textBox1.Clear();
                String first = displayData;

                String s = first;


            }


            String New = textBox1.Text;


            textBox1.Text = New;

            string[] lines = { New };
            System.IO.File.WriteAllLines(@"C:\Users\DarkVader\Desktop\output.txt", lines);


        }


        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

            String ID = textBox1.Text;
            String Fname = "";



            try
            {

                using (SqlConnection connection = new SqlConnection("Data Source=DTSQL03;Initial Catalog=TableBadge;Integrated Security=True"))
                {
                    SqlCommand command =
                    new SqlCommand("select * from CBadge WHERE cardno LIKE '%" + ID + "%'", connection);
                    connection.Open();

                    SqlDataReader read = command.ExecuteReader();

                    while (read.Read())
                    {
                        Fname = (read["firstname"].ToString());


                        fname.Text = Regex.Replace(Fname, @"\s+", " ");

                    }
                    read.Close();
                }


            }
            catch
            {

                MessageBox.Show("NO Connection");

            }


        }


    }
}

这是我得到的数据

enter image description here

这是我必须匹配的SQL数据库中的数据。

0006F350

任何帮助都会很棒。谢谢!

更新

好的,我可以使用下面的代码清除特殊字符。

String New = textBox1.Text;

textBox1.Text = Regex.Replace(New, @"[^0-9a-zA-Z]+", "");

现在我的问题是在当前的SQL服务器上获得正确的格式化。

000B06F3500006F350

0 个答案:

没有答案