How to poll the serial port to see if there is data and if not go on

I am working on a senior project that involves a RFID card reader that sends info through the serial port only when a RFID tag is in the readers range. I am wanting to constantly check the serial port for this info but I can only get it to check it and it waits until there is a card. In this period i cannot do anything else. It just sits there and waits. I want it to check it and if no data then go on. Below is what i am doing.

serialPort1.Open();

string names, decoded;

serialPort1.ReadTo("8");

names = serialPort1.ReadTo("31");

names = names.Remove(0, 16);

//names = "802A546573745F45453438305F463031";

StringBuilder sb = new StringBuilder();

for (int i = 0; i <= names.Length - 2; i += 2)

{

sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(names.Substring(i, 2),

System.Globalization.NumberStyles.HexNumber))));

}

decoded = sb.ToString();

decoded += "\t\t";

decoded += DateTime.Now.ToString();

decoded += "\r\n";

textBox1.Text += decoded;

serialPort1.Close();

This code will open the serial port and read in my hex numbers from the card and change them to ascii text. I just want it not to wait there. My friends said i should use a timer to check it every second or so but i do not know how to do that either. Very new and very green. Appreciate all the help i can get.

Thanks



Answer this question

How to poll the serial port to see if there is data and if not go on

  • Claudio Perrone

    Hi

    Using a timer, I think you should embed your code where I marked with red.

    //

    // timer1

    //

    this.timer1.Enabled = true;

    this.timer1.Interval = 1000;

    this.timer1.Tick += new System.EventHandler(this.Clock_Tick);

    private System.Windows.Forms.Timer timer1;

    public string GetTime()

    {

    string TimeInString = "";

    int day = DateTime.Now.Day;

    int month = DateTime.Now.Month;

    int year = DateTime.Now.Year;

    int hour = DateTime.Now.Hour;

    int min = DateTime.Now.Minute;

    int sec = DateTime.Now.Second;

    string AM = "AM";

    string PM = "PM";

    TimeInString = (day < 10) "0" + day.ToString() : day.ToString();

    TimeInString += "-" + ((month < 10) "0" + month.ToString() : month.ToString());

    TimeInString += "-" + ((year < 10) "0" + year.ToString() : year.ToString());

    TimeInString += " " + ((hour < 10) "0" + hour.ToString() : hour.ToString());

    TimeInString += ":" + ((min < 10) "0" + min.ToString() : min.ToString());

    TimeInString += ":" + ((sec < 10) "0" + sec.ToString() : sec.ToString());

    TimeInString += " " + ((hour < 12) "" + AM : PM);

    return TimeInString;

    }

    private void Clock_Tick(object sender, EventArgs e)

    {

    if (sender == timer1)

    {

    lbTime.Text = GetTime();

    schedulerThread(); //this means schedulerThread will run every second OR

    int ss = DateTime.Now.Second;
    if (ss == 0) //this means schedulerThread will run every minute
    {
    schedulerThread();

    }

    }

    }
    private void schedulerThread()
    {
    //Do the operation

    }

    Hope this helps



  • How to poll the serial port to see if there is data and if not go on