0

我想在 RFID 阅读器扫描一些标签后添加文本/描述。我尝试使用 IF 语句,但它什么也没做。

它的输出是标签号,但我想添加的文本没有显示或显示。请帮忙,谢谢!

我希望输出为 00000919BEAE = Milk

这是我们的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
    Dictionary<string, string> tags = new Dictionary<string, string>() { 
{ "00000919BEAE", "Milk" } ,
{"0000092A1132", "Fruits"}};
    public string RxString;

      public Form1()
      {
          InitializeComponent();
      }

      private void buttonStart_Click(object sender, EventArgs e)
      {
          serialPort1.PortName = "COM15";
          serialPort1.BaudRate = 9600;

          serialPort1.Open();
          if (serialPort1.IsOpen)
          {
              buttonStart.Enabled = false;
              buttonStop.Enabled = true;
              textBox1.ReadOnly = false;
          }
      }

      private void buttonStop_Click(object sender, EventArgs e)
      {
          if (serialPort1.IsOpen)
          {
              serialPort1.Close();
              buttonStart.Enabled = true;
              buttonStop.Enabled = false;
              textBox1.ReadOnly = true;
          }

      }

      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
          if (serialPort1.IsOpen) serialPort1.Close();
      }

      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          // If the port is closed, don't try to send a character.

          if(!serialPort1.IsOpen) return;

          // If the port is Open, declare a char[] array with one element.
          char[] buff = new char[1];

          // Load element 0 with the key character.

          buff[0] = e.KeyChar;

          // Send the one character buffer.
          serialPort1.Write(buff, 0, 1);

          // Set the KeyPress event as handled so the character won't
          // display locally. If you want it to display, omit the next line.
          e.Handled = true;


      }



      private void DisplayText(object sender, EventArgs e)
      {
          textBox1.AppendText(RxString);
          if (tags.ContainsKey(RxString))
          {
              Console.Write(tags[RxString]); // this will print Milk
          }

      }

      private void serialPort1_DataReceived
        (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
      {
          RxString = serialPort1.ReadExisting();
          this.Invoke(new EventHandler(DisplayText));

      }





    }
 }
4

2 回答 2

0

尝试制作RxString并将其与两个大写进行比较的值,如下所示:

      if (RxString.ToUpper() == tagid1.ToUpper())
      {
          string milk = "Milk";
          Console.Write(milk);
      }
      if (RxString.ToUpper() == tagid2.ToUpper())
      {
          string fruits = "Fruits";
          Console.Write(fruits);
      }
      if (RxString.ToUpper() == tagid3.ToUpper())
      {
          string soda = "Soda";
          Console.Write(soda);
      }
      if (RxString.ToUpper() == tagid4.ToUpper())
      {
          string meat = "Meat";
          Console.Write(meat);
      }
于 2013-08-29T02:23:18.917 回答
0

我希望输出为 000A1234D980 = Milk

但你有条件

      if (RxString == tagid1)
      {
          string milk = "Milk";
          Console.Write(milk);
      }

tagid1 等于"00000919BEAE",因此您的条件将失败。

调试并查看您获得的确切值RxString 可以找到您的条件失败的答案

更新 :

您可以为此使用字典

Dictionary<string, string> tags = new Dictionary<string, string>();
tags.Add("00000919BEAE", "Milk");
tags.Add("0000092A1132", "Fruits");

在您的DisplayText方法中,您可以检查RxString标签字典中是否存在键,如果存在,您可以打印

if (tags.ContainsKey(RxString))
{
    Console.Write(tags[RxString]); // this will print Milk
}

示例代码:

public partial class Form1 : Form
{
    Dictionary<string, string> tags = new Dictionary<string, string>() { 
    { "00000919BEAE", "Milk" } ,
    {"0000092A1132", "Fruits"}};

    public string RxString { get; set; }

    public Form1()
    {
        InitializeComponent();
    }

    private void DisplayText(object sender, EventArgs e)
    {
        textBox1.AppendText(RxString);
        if (tags.ContainsKey(RxString))
        {
            textBox1.AppendText(RxString + ":" + tags[RxString]); 
        }
    }
}
于 2013-08-29T02:32:50.697 回答