更新
我正在阅读RFID Card
,并将其价值签入我的textbox.text
.
它在我第一次通过我的卡时工作RFID Reader
,但在我第二次通过卡时,它会在我的卡上显示整个卡的 ID textbox.text
,它只显示卡 ID 的最后一个字母。有时您会看到整个数字在文本框中出现和消失的速度非常快,但是从您第二次通过卡片开始,文本框中只剩下最后一个字母。
这可能是什么原因造成的?
这是我当前的代码:
using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text;
using System.Text.RegularExpressions;
namespace RFID_Reader
{
public partial class PortaSerial : Form
{
private SerialPort porta_serial = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
public PortaSerial()
{
InitializeComponent();
}
private void PortaSerial_Load(object sender, EventArgs e)
{
try
{
if (porta_serial.IsOpen)
{
porta_serial.Close();
}
porta_serial.Open();
porta_serial.DtrEnable = true;
porta_serial.DataReceived += new SerialDataReceivedEventHandler(Recebe_Data);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
void Recebe_Data(object sender, SerialDataReceivedEventArgs e)
{
try
{
string oi = porta_serial.ReadExisting().ToString();
SetLabel(oi);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
void SetLabel(String s)
{
try
{
if (this.InvokeRequired)
{
this.Invoke(new Action<String>(SetLabel), s);
return;
}
textBox1.Text = RemoveSpecialCharacters(s);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
{
sb.Append(c);
}
}
return sb.ToString();
}
}
}