how to make RFID mfrc522 work just when serial is estblished

Hi guys

I have RFID connected with Arduino uno and I want every time I am scan the card will send the rfid number to windows VB.NET with serial connection. I can do this and i achieved it but I don't want every time scan send i want just when serial is established will scan and send.

Ho can I achieve this case

my code is:

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance. 
#include <Servo.h> 
 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;
Servo myservo3;
void setup() 
{ 
  myservo1.attach(1);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(2); 
  myservo3.attach(3); 
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  
  Serial.begin(9600); //begins serial communication
} 
  
void loop() 
{ 

   // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("");

  byte letter;
     
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     
     
       }
      delay(2000);
}

The Arduino has no idea if there is anything listening to the serial port, unless that listener tells it that it is listening.

So, your VB app will need to send "Hey, I care about RFID data", or something easier to parse, and the Arduino will need to read what the app sends, and take appropriate action.

But can't do something like

if (Serial.available()){
    delay(100);

I tried it but RFID didn't sense any card!

I used this code and worked with Arduino serial but still not with VB ?

while(Serial.available()>0);
 Serial.begin(9600); //begins serial communication

  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init(); // Initiate MFRC522
while(Serial.available()>0);
 Serial.begin(9600); //begins serial communication

Do you REALLY think that available() returns something useful BEFORE you call begin()?

Do you KNOW what available() is telling you? It does NOT appear that you do.

@PaulS

So what I have to do what is serial.available()

Serial.available() returns the number of bytes in the incoming serial data buffer that can be read().

It tells you nothing about whether there is anything on the other end of the serial port.

What you are asking for is impossible. A radio station broadcasts. The operators have no way of knowing whether anyone is listening. Unless they solicit input AND the listener(s) care to respond.

The Arduino sends data out the serial port, or not, without caring whether anyone is listening, or not.

So why when I added it in the code the arduino serial didn't receive the rfid number until I opened the serial window!!

but didn't work with VB serial