title: Arduino does not send data to WinForms app
body: <p>I have an Arduino Leonardo with a program that is listening on a serial port and executes commands sent to it. The Arduino device is an RFID reader.</p>
  <p>I wrote a WinForms app that communicates with the RFID reader to authenticate users. The Arduino receives data fine, but does not send anything. Even the TX diode on the board does not light up.</p>
  <p>I tested my reader with an other PC serial monitor, and it works fine, so the problem is in my app. The Baud rate is 9600, data bits is 8 and parity is none. Handshaking probably does not matter, because the serial monitor worked with any handshaking.</p>
  <p>What am I doing wrong? Another strange thing is, that when I connected the PC with my app via RS232 to an other PC with a serial monitor, and I simulated my reader, everything works. So why are only my app and the Arduino not working, when all other combinations are?</p>
  <p>Arduino device is only Arduino + MFRC522 reader.</p>
  <p>App code:</p>
  <pre class="lang-cs prettyprint-override"><code>        public static Status Authenticate(Key key, Passsword passwd, UID uid, string port)
          {
              SerialPort serial = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
              serial.Handshake = Handshake.None;
  
              serial.Open();
  
              byte[] cmd = { 1 };
  
              serial.Write(cmd, 0, 1); //Send command
  
              if (serial.ReadLine() != &quot;START&quot;) //Device is ready
              { serial.Close(); return Status.Err; }
  
              if (serial.ReadLine() != &quot;KEY&quot;) //Device requires key
              { serial.Close(); return Status.Err; }
  
              serial.Write(key.ToArray(), 0, 6); //Send key
  
              if (serial.ReadLine() != &quot;PASSWD&quot;) //Device requires password
              { serial.Close(); return Status.Err; }
  
              serial.Write(passwd.ToArray(), 0, 16); //Send password
  
              if (serial.ReadLine() != &quot;UID&quot;) //Device requires card UID
              { serial.Close(); return Status.Err; }
  
              serial.Write(uid.ToArray(), 0, 4); //Send UID
  
              if (serial.ReadLine() != &quot;WAIT-CARD&quot;) //Wait
              { serial.Close(); return Status.Err; }
  
              if (serial.ReadLine() != &quot;CARD-PRESENT&quot;) //User presented card
              { serial.Close(); return Status.Err; }
  
              string stat = serial.ReadLine(); //Receive authorization status
  
              serial.Close();
  
              switch (stat) //Check status
              {
                  case &quot;OK&quot;: return Status.OK;
                  case &quot;ERR-PASS&quot;: return Status.Inc_passwd;
                  case &quot;ERR-UID&quot;: return Status.Inc_uid;
                  case &quot;ERR-KEY&quot;: return Status.Inc_key;
                  default: return Status.Err;
              }
  </code></pre>
  <p>Full Arduino code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;MFRC522.h&gt;
  #include &lt;SPI.h&gt;
  #include &lt;EEPROM.h&gt;
  
  #define SECTORS_14
  
  // ----------------------------------------------------------------------------------------------------
  
  #ifdef SECTORS_15
  const byte permittedBlocks[] = {8, 9, 10, 12, 13, 14, 16, 17, 18, 20, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33,
    34, 36, 37, 38, 40, 41, 42, 44, 45, 46, 48, 49, 50, 52, 53, 54, 56, 57, 58, 60, 61, 62};
  #endif
  
  #ifdef SECTORS_14
  const byte permittedBlocks[] = {8, 9, 10, 12, 13, 14, 16, 17, 18, 20, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33,
    34, 36, 37, 38, 40, 41, 42, 44, 45, 46, 48, 49, 50, 52, 53, 54, 56, 57, 58};
  #endif
  
  typedef struct codes
  {
    byte uid[4];
    byte passwd[16];
    MFRC522::MIFARE_Key key;
  } personalData;
  
  void setup() 
  {
    Serial.begin(9600);
    SPI.begin();
    delay(1000);
  }
  
  void loop() 
  {
    byte cmd;
    
    while (!Serial.available());
    cmd = Serial.read();
  
    switch (cmd)
    {
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 1:
      {
        digitalWrite(13, HIGH);
        Serial.println(&quot;START&quot;);
        
        personalData personal = RFID_GetPersonalData();
        
        MFRC522 rfid(10, 9);
        rfid.PCD_Init();
  
        if (RFID_WaitForCard(&amp;rfid, personal)) return;
  
        Serial.println(&quot;OK&quot;);
        rfid.PICC_HaltA();
        rfid.PCD_StopCrypto1();
        
        break;
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 2:
      {
        Serial.println(&quot;START&quot;);
        
        personalData personal = RFID_GetPersonalData();
        
        Serial.println(&quot;NEW-PASSWD&quot;);
  
        byte newPasswd[16];
        getArray(newPasswd, 16);
  
        MFRC522 rfid(10, 9);
        rfid.PCD_Init();
  
        if (RFID_WaitForCard(&amp;rfid, personal)) return;
  
        Serial.println(&quot;CHANGING&quot;);
  
        byte ok = RFID_ChangePasswd(newPasswd, &amp;rfid, personal.key);
  
        if (ok == 1)      {Serial.println(&quot;ERR-KEY&quot;);    return;}
        else if (ok == 2) {Serial.println(&quot;ERR-WRITE&quot;);    return;}
  
        Serial.println(&quot;OK&quot;);
  
        rfid.PICC_HaltA();
        rfid.PCD_StopCrypto1();
        
        break;
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 3:
      {
        Serial.println(&quot;START&quot;);
        
        personalData personal = RFID_GetPersonalData();
  
        Serial.println(&quot;TO-WRITE&quot;);
  
        byte count[1];
        getArray(count, 1);
  
        byte blocks[count[0]][1];
        byte data[count[0]][16];
  
        for (byte i = 0; i &lt; count[0]; i++)
        {
          Serial.println(&quot;BLOCK&quot;);
  
          getArray(blocks[i], 1);
  
          if (!isBlockPermitted(blocks[i][0])) {Serial.println(&quot;BLOCK-INVALID&quot;); return;}
  
          Serial.println(&quot;DATA&quot;);
  
          getArray(data[i], 16);
        }
        
  
        MFRC522 rfid(10, 9);
        rfid.PCD_Init();
  
        if (RFID_WaitForCard(&amp;rfid, personal)) return;
  
        Serial.println(&quot;WRITING&quot;);
  
        for (byte i = 0; i &lt; count[0]; i++)
        {
          byte ok = RFID_WriteBlock(data[i], &amp;rfid, personal.key, blocks[i][0]);
  
          if (ok == 1)      {Serial.println(&quot;ERR-KEY&quot;);    return;}
          else if (ok == 2) {Serial.println(&quot;ERR-WRITE&quot;);    return;}
        }
        
        Serial.println(&quot;OK&quot;);
  
        rfid.PICC_HaltA();
        rfid.PCD_StopCrypto1();
        
        break;
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 4:
      {
        Serial.println(&quot;START&quot;);
        
        personalData personal = RFID_GetPersonalData();
  
        MFRC522 rfid(10, 9);
        rfid.PCD_Init();
  
        if (RFID_WaitForCard(&amp;rfid, personal)) return;
        
        Serial.println(&quot;FORMATTING&quot;);
  
        byte ok = RFID_FormatCard(&amp;rfid, personal.key);
  
        if (ok == 1)      {Serial.println(&quot;ERR-KEY&quot;);    return;}
        else if (ok == 2) {Serial.println(&quot;ERR-WRITE&quot;);    return;}
  
        Serial.println(&quot;OK&quot;);
  
        rfid.PICC_HaltA();
        rfid.PCD_StopCrypto1();
        
        break;
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 5:
      {
        Serial.println(&quot;START&quot;);
        
        personalData personal = RFID_GetPersonalData();
  
        Serial.println(&quot;TO-READ&quot;);
  
        byte count[1];
        getArray(count, 1);
  
        byte blocks[count[0]][1];
  
        for (byte i = 0; i &lt; count[0]; i++)
        {
          Serial.println(&quot;BLOCK&quot;);
  
          getArray(blocks[i], 1);
  
          if (!isBlockPermitted(blocks[i][0])) {Serial.println(&quot;BLOCK-INVALID&quot;); return;}
        }
  
        MFRC522 rfid(10, 9);
        rfid.PCD_Init();
  
        if (RFID_WaitForCard(&amp;rfid, personal)) return;
  
        Serial.println(&quot;READING&quot;);
  
        byte data[count[0]][16];
        for (byte i = 0; i &lt; count[0]; i++)
        {
          byte ok = RFID_ReadBlock(data[i], &amp;rfid, personal.key, blocks[i][0]);
  
          if (ok == 1)      {Serial.println(&quot;ERR-KEY&quot;);    return;}
          else if (ok == 2) {Serial.println(&quot;ERR-READ&quot;);    return;}
        }
        
        for (byte i = 0; i &lt; count[0]; i++)
        {
          Serial.println(&quot;BLOCK-&quot; + blocks[i][0]);
          for (byte i2 = 0; i2 &lt; 16; i2++)
            Serial.println(data[i][i2]);
        }
  
        Serial.println(&quot;OK&quot;);
  
        rfid.PICC_HaltA();
        rfid.PCD_StopCrypto1();
        
        break;
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 6:
      {
        Serial.println(&quot;START&quot;);
        
        personalData personal = RFID_GetPersonalData();
  
        Serial.println(&quot;TO-READ&quot;);
  
        byte countR[1];
        getArray(countR, 1);
  
        byte blocksR[countR[0]][1];
  
        for (byte i = 0; i &lt; countR[0]; i++)
        {
          Serial.println(&quot;BLOCK&quot;);
  
          getArray(blocksR[i], 1);
  
          if (!isBlockPermitted(blocksR[i][0])) {Serial.println(&quot;BLOCK-INVALID&quot;); return;}
        }
  
        Serial.println(&quot;TO-WRITE&quot;);
  
        byte countW[1];
        getArray(countW, 1);
  
        byte blocksW[countW[0]][1];
        byte dataW[countW[0]][16];
  
        for (byte i = 0; i &lt; countW[0]; i++)
        {
          Serial.println(&quot;BLOCK&quot;);
  
          getArray(blocksW[i], 1);
  
          if (!isBlockPermitted(blocksW[i][0])) {Serial.println(&quot;BLOCK-INVALID&quot;); return;}
  
          Serial.println(&quot;DATA&quot;);
  
          getArray(dataW[i], 16);
        }
  
        MFRC522 rfid(10, 9);
        rfid.PCD_Init();
  
        if (RFID_WaitForCard(&amp;rfid, personal)) return;
  
        Serial.println(&quot;READING&quot;);
  
        byte dataR[countR[0]][16];
        for (byte i = 0; i &lt; countR[0]; i++)
        {
          byte ok = RFID_ReadBlock(dataR[i], &amp;rfid, personal.key, blocksR[i][0]);
  
          if (ok == 1)      {Serial.println(&quot;ERR-KEY&quot;);    return;}
          else if (ok == 2) {Serial.println(&quot;ERR-READ&quot;);    return;}
        }
  
        for (byte i = 0; i &lt; countR[0]; i++)
        {
          Serial.println(&quot;BLOCK-&quot; + blocksR[i][0]);
          for (byte i2 = 0; i2 &lt; 16; i2++)
            Serial.println(dataR[i][i2]);
        }
  
        Serial.println(&quot;WRITING&quot;);
  
        for (byte i = 0; i &lt; countW[0]; i++)
        {
          byte ok = RFID_WriteBlock(dataW[i], &amp;rfid, personal.key, blocksW[i][0]);
  
          if (ok == 1)      {Serial.println(&quot;ERR-KEY&quot;);    return;}
          else if (ok == 2) {Serial.println(&quot;ERR-WRITE&quot;);    return;}
        }
  
        Serial.println(&quot;OK&quot;);
  
        rfid.PICC_HaltA();
        rfid.PCD_StopCrypto1();
        
        break;
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 7:
      {
        Serial.println(&quot;START&quot;);
        Serial.println(&quot;TO-WRITE&quot;);
  
        byte count[1];
        getArray(count, 1);
        byte addr[count[0]][1];
        byte val[count[0]][1];
  
        for (byte i = 0; i &lt; count[0]; i++)
        {
          Serial.println(&quot;ADDR&quot;);
          getArray(addr[i], 1);
  
          Serial.println(&quot;VAL&quot;);
          getArray(val[i], 1);
        }
  
        for (byte i = 0; i &lt;count[0]; i++)
          EEPROM.put(addr[i][0], val[i][0]);
  
        Serial.println(&quot;OK&quot;);
      }
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      case 8:
      {
        Serial.println(&quot;START&quot;);
        Serial.println(&quot;TO-READ&quot;);
  
        byte count[1];
        getArray(count, 1);
        byte addr[count[0]][1];
        byte val[count[0]][1];
  
        for (byte i = 0; i &lt; count[0]; i++)
        {
          Serial.println(&quot;ADDR&quot;);
          getArray(addr[i], 1);
        }
  
        for (byte i = 0; i &lt;count[0]; i++)
        {
          EEPROM.get(addr[i][0], val[i][0]);
          Serial.print(&quot;READ-&quot;); Serial.println(addr[i][0]);
          Serial.println(val[i][0]);
        }
        
        Serial.println(&quot;OK&quot;);
      }
  
      case 9:
      {
        Serial.println(&quot;START&quot;);
        Serial.println(&quot;TO-READ&quot;);
  
        byte countR[1];
        getArray(countR, 1);
        byte addrR[countR[0]][1];
        byte valR[countR[0]][1];
  
        for (byte i = 0; i &lt; countR[0]; i++)
        {
          Serial.println(&quot;ADDR&quot;);
          getArray(addrR[i], 1);
        }
  
        Serial.println(&quot;TO-WRITE&quot;);
  
        byte countW[1];
        getArray(countW, 1);
        byte addrW[countW[0]][1];
        byte valW[countW[0]][1];
  
        for (byte i = 0; i &lt; countW[0]; i++)
        {
          Serial.println(&quot;ADDR&quot;);
          getArray(addrW[i], 1);
  
          Serial.println(&quot;VAL&quot;);
          getArray(valW[i], 1);
        }
  
        for (byte i = 0; i &lt;countR[0]; i++)
        {
          EEPROM.get(addrR[i][0], valR[i][0]);
          Serial.print(&quot;READ-&quot;); Serial.println(addrR[i][0]);
          Serial.println(valR[i][0]);
        }
  
        for (byte i = 0; i &lt;countW[0]; i++)
          EEPROM.put(addrW[i][0], valW[i][0]);
        
        Serial.println(&quot;OK&quot;);
      }
    }
  }
  // -----------------------------------------------------------------------------------------------------------
  inline bool compareArray(byte a1[], byte a2[], byte aSize)
  {
    for (byte i = 0; i &lt; aSize; i++)
      if (a1[i] != a2[i]) return false;
    return true;
  }
  // -----------------------------------------------------------------------------------------------------------
  inline void copyArray(byte a1[], byte a2[], byte aSize)
  {
    for (byte i = 0; i &lt; aSize; i++)
      a1[i] = a2[i];
  }
  // -----------------------------------------------------------------------------------------------------------
  inline void getArray(byte a[], byte len)
  {
    for (byte i = 0; i &lt; len; i++)
    {
      while (!Serial.available());
      a[i] = Serial.read();
    }
  }
  // -----------------------------------------------------------------------------------------------------------
  inline MFRC522::MIFARE_Key RFID_GetKey()
  {
    MFRC522::MIFARE_Key key;
    byte rawKey[6];
    for (byte i = 0; i &lt; 6; i++)
    {
      while (!Serial.available());
      rawKey[i] = Serial.read();
    }
  
    for (byte i = 0; i &lt; 6; i++)
      key.keyByte[i] = rawKey[i];
    return key;
  }
  // -----------------------------------------------------------------------------------------------------------
  byte RFID_Auth(personalData data, MFRC522* rfid)
  {
        if (!compareArray(data.uid, rfid-&gt;uid.uidByte, 4))
        {
          return 1;
        }
  
        MFRC522::StatusCode status;
        status = rfid-&gt;PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &amp;data.key, &amp;(rfid-&gt;uid));
        if (status != MFRC522::STATUS_OK) 
        {
          return 2;
        }
  
        byte buff[18];
        byte len = 18;
        status = rfid-&gt;MIFARE_Read(1, buff, &amp;len);
        if (status != MFRC522::STATUS_OK) 
        {
          return 3;
        }
  
        if (!compareArray(buff, data.passwd, 16))
        {
          return 4;
        }
  
        return 0;
  }
  // -----------------------------------------------------------------------------------------------------------
  inline byte RFID_ChangePasswd(byte newPasswd[], MFRC522* rfid, MFRC522::MIFARE_Key key)
  {
    return RFID_WriteBlock(newPasswd, rfid, key, 1);
  }
  
  bool isBlockPermitted(byte block)
  {
  #ifdef SECTORS_15
    if (block &gt; 62) return false;
  #endif
  
  #ifdef SECTORS_14
    if (block &gt; 58) return false;
  #endif
  
    if (block &lt; 8) return false;
  
    bool is = false;
    
    for (byte i = 0; i &lt; 
  #ifdef SECTORS_15
    42;
  #endif
  #ifdef SECTORS_14
    39;
  #endif
    i++)
      if (block == permittedBlocks[i]) is = true;
  
    return is;
  }
  // -----------------------------------------------------------------------------------------------------------
  byte RFID_WriteBlock(byte data[], MFRC522* rfid, MFRC522::MIFARE_Key key, byte block)
  {
    MFRC522::StatusCode status;
    status = rfid-&gt;PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &amp;key, &amp;(rfid-&gt;uid));
    if (status != MFRC522::STATUS_OK) 
    {
      return 1;
    }
  
    byte data2[18];
    for (byte i = 0; i &lt; 16; i++)
      data2[i] = data[i];
    
    status = rfid-&gt;MIFARE_Write(block, data2, 16);
    if (status != MFRC522::STATUS_OK) 
    {
      return 2;
    }
  
    return 0;
  }
  // -----------------------------------------------------------------------------------------------------------
  byte RFID_ReadBlock(byte data[], MFRC522* rfid, MFRC522::MIFARE_Key key, byte block)
  {
    MFRC522::StatusCode status;
    status = rfid-&gt;PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &amp;key, &amp;(rfid-&gt;uid));
    if (status != MFRC522::STATUS_OK) 
    {
      return 1;
    }
  
    byte buff[18];
    byte len = 18;
    
    status = rfid-&gt;MIFARE_Read(block, buff, &amp;len);
    if (status != MFRC522::STATUS_OK) 
    {
      return 2;
    }
  
    copyArray(data, buff, 16);
  
    return 0;
  }
  // -----------------------------------------------------------------------------------------------------------
  byte RFID_FormatCard(MFRC522* rfid, MFRC522::MIFARE_Key key)
  {
    const byte data[18] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    
    for (byte i = 0; i &lt; 
  #ifdef SECTORS_15
    42;
  #endif
  #ifdef SECTORS_14
    39;
  #endif
    i++)
    {
      byte block = permittedBlocks[i];
  
      MFRC522::StatusCode status;
      status = rfid-&gt;PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &amp;key, &amp;(rfid-&gt;uid));
      if (status != MFRC522::STATUS_OK) 
      {
        return 1;
      }
  
      status = rfid-&gt;MIFARE_Write(block, const_cast&lt;byte*&gt;(data), 16);
      if (status != MFRC522::STATUS_OK) 
      {
        return 2;
      }
    }
  
    return 0;
  }
  // -----------------------------------------------------------------------------------------------------------
  inline personalData RFID_GetPersonalData()
  {
    personalData data;
    
    Serial.println(&quot;KEY&quot;);
        
    data.key = RFID_GetKey();
  
    Serial.println(&quot;PASSWD&quot;);
  
    getArray(data.passwd, 16);
  
    Serial.println(&quot;UID&quot;);
  
    getArray(data.uid, 4);
  
    return data;
  }
  
  // -----------------------------------------------------------------------------------------------------------
  inline bool RFID_WaitForCard(MFRC522* rfid, personalData personal)
  {
    Serial.println(&quot;WAIT-CARD&quot;);
    
    while (!rfid-&gt;PICC_IsNewCardPresent()) { if (Serial.available()) {byte cmd = Serial.read(); if (cmd == 66) {Serial.println(&quot;CANCELED&quot;); return true;}}}
    while (!rfid-&gt;PICC_ReadCardSerial());
    digitalWrite(13, LOW);
    Serial.println(&quot;CARD-PRESENT&quot;);
  
    byte ok = RFID_Auth(personal, rfid);
        
    if (ok == 1)      {Serial.println(&quot;ERR-UID&quot;);    return true;}
    else if (ok == 2) {Serial.println(&quot;ERR-KEY&quot;);    return true;}
    else if (ok == 3) {Serial.println(&quot;ERR-READ&quot;);   return true;}
    else if (ok == 4) {Serial.println(&quot;ERR-PASSWD&quot;); return true;}
  
    return false;
  }
  </code></pre>
  <p>Minimal reproducible example</p>
  <p>Arduino:</p>
  <pre class="lang-cpp prettyprint-override"><code>void setup() 
  {
    Serial.begin(9600);
  }
  
  void loop() 
  {
    byte command;
    while (!Serial.available());
    command = Serial.read();
    // ...
    Serial.println(&quot;START&quot;);
    // Some operations
    byte someData;
    while (!Serial.available());
    someData = Serial.read();
    // Some operations
    Serial.println(&quot;OK&quot;);
  
    // Arduino receives, but not send
  }
  </code></pre>
  <p>App:</p>
  <pre><code>public static Status Authenticate(Key key, Passsword passwd, UID uid, string port)
          {
              SerialPort serial = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
              serial.Handshake = Handshake.None;
  
              serial.Open();
  
              byte[] cmd = { 1 };
  
              serial.Write(cmd, 0, 1); //Send command
  
              if (serial.ReadLine() != &quot;START&quot;) //Device is ready. Here program is stuck, becuse it's waiting for START command that never comes
              { serial.Close(); return Status.Err; }
  
              serial.Write(cmd, 0, 1); //Send &quot;someData&quot;
  
              string stat = serial.ReadLine(); //Receive status
  
              serial.Close();
  
              switch (stat) //Check status
              {
                  case &quot;OK&quot;: return Status.OK;
                  case &quot;ERR-PASS&quot;: return Status.Inc_passwd;
                  case &quot;ERR-UID&quot;: return Status.Inc_uid;
                  case &quot;ERR-KEY&quot;: return Status.Inc_key;
                  default: return Status.Err;
              }
  </code></pre>