Click here to Skip to main content
15,305,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to read RFID tags and display it my inventory c# application. I have accessed it through Arduino application which gives me the right output on control monitor when I pass RFID Keychain. But when I tried to access it through visual studio, it works only once and then gives me access is denied!and also stopped working on Arduino IDE saying the port is busy!

What I have tried:

public products()
    {
        InitializeComponent();
        initializeRFIDPort();
    }



//Function  to initialize the serial port

 SerialPort RFIDPort    
 public SerialPort initializeRFIDPort()
    {

        try
        {
            RFIDPort = new SerialPort("COM4",9600,Parity.None,8, StopBits.One);
          
            if (RFIDPort.IsOpen)
                RFIDPort.Close();

            if(!RFIDPort.IsOpen)
                    RFIDPort.Open();
            }
        catch (UnauthorizedAccessException ex) {MessageBox.Show( ex.Message); }
        catch (Exception)
        {
            RFIDPort = null;
        }

        return RFIDPort;
    }


I have two scan button in my class:

 private void ScanButton_Click(object sender, EventArgs e)
    {
        try
        {

            scanButtonIsClicked = true;
            if (RFIDPort.IsOpen)
            {
                RFIDPort.DataReceived += serialPort1_DataReceived;
                textBox1.Text = "";

            }
            else
                MessageBox.Show("RFID Reader is not connected!");
        }
        catch (IOException) { MessageBox.Show("Please reconnect your device "); }
        catch (System.Exception)
        {
            MessageBox.Show("Please Try Again");
        }
     }

  private void Searchbutton_Click(object sender, EventArgs e)
    {
        scansearchbtn = true;
        scanButtonIsClicked = false;

        try
        {
            if (RFIDPort.IsOpen)
            {
                 RFIDPort.DataReceived += serialPort1_DataReceived;

                textBox2.Text = "";

            }
            else { MessageBox.Show("RFID Reader is not connected!"); }
        }
        catch (IOException) { MessageBox.Show("Please reconnect your device ");}
        }

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        try
        {

            string line = RFIDPort.ReadLine();

            if (scanButtonIsClicked == true)
                this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
            else
                this.BeginInvoke(new LineReceivedEvent(Line2Received), line);
        }
        catch (Exception){ MessageBox.Show("Can't Read from RFID Device.Please try again"); }

    }

    private delegate void LineReceivedEvent(string line);

    private void LineReceived(string line)
    {
        textBox2.Text = line;

    }
    private void Line2Received(string line)
    {
        textBox1.Text = line;

    }


and then I closed serial port in Forum-Closing. when I close the program which contains RFIDPort.Close(), also an exception that I/O is aborted.
please If anyone can lead me to the right way. I have try many times!!
Posted
Updated 27-Jun-18 23:20pm
v2
Comments
Richard MacCutchan 28-Jun-18 5:32am
   
Make sure the Arduino IDE is not running, or it will be holding the port open.

1 solution

Only one app can "open" a serial port at a time.

Any app that is "hung" with an open serial port needs to be killed to release the port.

And when you "create" the port, is starts as "closed"; all your "checking" is redundant.

And all your "empty" exception blocks are useless. Don't use "empty" blocks because it defeats the "default"; which is to report via the (last) "Exception" (base) class.
   
Comments
Member 13858107 27-Jun-18 17:07pm
   
so how can I upload the sketch to Arduino hardware if I don't open it on Arduino software and then read it by my own program(Vs)? Thanks for the advice, honestly I am a beginner :)
Gerry Schmitz 27-Jun-18 17:40pm
   
I pointed out some of the things you need to "clean up" first to get towards "something" that can be fixed.

You may then get some more useful "diagnostic" info.
Member 13858107 28-Jun-18 5:31am
   
Ok, but you said you can open the serial port just in one app. How can I open it just in Visual Studio and without opening it Arduino and uploading the sketch in order to read data? I don't get it!
Gerry Schmitz 28-Jun-18 13:56pm
   
When "I" upload firmware to a device, I run a "loader".

When I have "finished" loading, I "quit" the loader (which "closes" its "handle" to the port), and then I (re)open the port on the client machine (with my "app") and then send and receive.

i.e. Only one "app" opens the "same" serial port at any one time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900