PlayHardGoPro
PlayHardGoPro

Reputation: 2923

Reading data from a RS232 port with RFID

UPDATE

I'm reading an RFID Card, and signing it's value into my textbox.text.
It's working in the first time I pass the card in my RFID Reader, but form the second time I pass the card, instead it shows the whole card's id on my textbox.text, it's showing only the LAST letter of the card's id. Sometimes you can see the whole number appear and vanish very fast on the textbox, but from the second time you pass the card, only the last letter remain in the textbox.
What may be causing this ?

Here's my current code:

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

Upvotes: 0

Views: 4666

Answers (2)

JeffRSon
JeffRSon

Reputation: 11176

In order to access elements of the GUI (TextBox, Label, ...) you need to be running in the UI thread. DataReceived is running in another thread. You can change into the UI thread by Invoke like this

void SetLabel(String s)
{
   if (this.InvokeRequired) {
      this.Invoke (new Action<String>(SetLabel), s);
      return;
   }

   Label1.Text = s;
}

But beware - if you need to access different parts of the GUI (e.g. Label and TextBox), you should "collect" those Invoke's because every call takes some time. Also you may consider BeginInvoke instead of Invoke for not blocking the receive thread. However, you should read about those details in MSDN or google for more difficult examples.

Upvotes: 4

Steve
Steve

Reputation: 6424

In your Form1_Load method, you use port to refer to your SerialPort object, but in your event handler, you use porta_serial.

Upvotes: 0

Related Questions