0

I want to add texts / descriptions after the RFID reader has scanned some tags. I tried using the IF statements but it did nothing.

Its output are the tag numbers but the texts I wanted to add didnt show or display. Please help, thanks!

I want the output to be 00000919BEAE = Milk

Here's our code:

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

      }





    }
 }
Ram Aquino
  • 341
  • 3
  • 8

2 Answers2

0

Try making RxString and the value you are comparing it to both uppercase, like this:

      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);
      }
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • it still just displays the RFID number from the tags :( – Ram Aquino Aug 29 '13 at 03:01
  • Have you debugged what `RxString` is from the reader, before it calls `DisplayText`? Is it removing leading zeroes or anything like that, which would cause the string comparison to fail? – Karl Anderson Aug 29 '13 at 03:04
  • We didnt debug anything. And no it didnt remove any zeroes. The values declared on the tagids are the ones being sent by the reader from the tags itself. – Ram Aquino Aug 29 '13 at 03:25
  • Why have you not debugged your program? – Karl Anderson Aug 29 '13 at 03:32
  • What do you mean debugged exactly? Im sorry Im still not used to technical terms but we run the programs and got no errors but we didnt get the desired output. – Ram Aquino Aug 29 '13 at 03:38
  • I have no idea. Im new in c# sooo.. If its not too much bother can you tell me? – Ram Aquino Aug 29 '13 at 03:58
  • Are you using Visual Studio? – Karl Anderson Aug 29 '13 at 04:02
  • yup i know how to set up breakpoints but what does it do in my code? – Ram Aquino Aug 29 '13 at 04:13
  • It allows you to stop at a line and then inspect the values in variables, either by mousing over the variable itself, adding variables to a watch window or using the immediate variables window. – Karl Anderson Aug 29 '13 at 04:13
  • Put a break point on these two lines: `RxString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText));`. This will let you know what value is being read by your RFID scanner. – Karl Anderson Aug 29 '13 at 04:39
0

I want the output to be 000A1234D980 = Milk

but you have condition

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

tagid1 is equal to "00000919BEAE", so your condition will fail.

debug and see which value exactly you get for RxString you can find the answer why your condition fail

Update :

You can use Dictionary for this

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

in your DisplayText method you can check for RxString key exist in the tag dictionary and if exist you can print

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

Sample code :

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]); 
        }
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Sorry the above one one was supposedly an example but I forgot I declared the value for tagid1 xD As for the RxString, its value changes depending on what the RFID reader will read. We want to make it as when the reader read a tag itll be saved on RxString then if that tag matches one of the tagids, it will describe whether that tag is either milk, soda, fruits or meat. – Ram Aquino Aug 29 '13 at 02:44
  • where should we insert the dictionary? Inside the displayText ot anywhere on the program? Im sorry, Im just really a newbie in c# – Ram Aquino Aug 29 '13 at 03:37
  • We tried the above codes but had the following errors: Error 2 Invalid token '(' in class, struct, or interface member declaration C:\Users\Ram\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs 19 9 WindowsFormsApplication3 and Error 7 'WindowsFormsApplication3.Form1.tags' is a 'field' but is used like a 'type' C:\Users\Ram\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs 20 10 WindowsFormsApplication3 – Ram Aquino Aug 29 '13 at 03:45
  • I tried the update but it still didnt display the milk.. only the 00000919BEAE. There was no error whatsoever but it didnt display the desired output. I have no idea what to do anymore and I apreciate you guys helping me with. Do you have any idea what might be wrong or missing from the codes? Ill update the whole thing with your latest update on our work. Thanks again! – Ram Aquino Aug 29 '13 at 03:54
  • change the line `Console.Write(tags[RxString])` to `textBox1.AppendText(RxString + ":" + tags[RxString])` – Damith Aug 29 '13 at 03:59
  • i just change it but still get the same hex data that the rfid have – Ram Aquino Aug 29 '13 at 04:22