通过C#应用程序连接arduino

时间:2018-02-25 21:34:23

标签: c# arduino rfid

我的配置: Arduino Uno RFID Adafruit PN532 Visual Studio 2017 C#,.NET

我希望在C#中使用一个接口,在读取器和写入不同标签的arduino uno和屏蔽PN532之间进行通信。

但通过已经退出的草图。

那么,如何从C#.NET应用程序调用arduino方法来控制C#应用程序中的所有内容

我已经找到了点亮LED的例子,但我要求的更为先进和不同。

有关信息我已经知道使用串口恢复连接到我的arduino的电脑的端口。 COM

方法C#:

    private void btn_dump_Click(object sender, EventArgs e)
    {
       if(isConnected)
        {
            // and here I want to execute my methods "dump();" that are in the code of my arduino
        }
    }

Arduino方法

void dump()
{
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

if (success)
{
 Serial.println();
 Serial.print(F("ID: "));
 if (uidLength == 7)
{  
  PrintHexShort(uid, uidLength);
}
else
{
  Serial.println(F("error"));
  }
   delay(500);
  }
}

谢谢大家,

编辑:

我找到了解决方案,我发布它以帮助未来的人们到达:

Arduino代码:

String data = Serial.readString();
if(data == "string_1")
{
   // call your method
}
else if(data == "string_2")
{
   // call your method
}

C#代码:

private void Btn_Click(object sender, EventArgs e)
{
     port.Write("String_1");
}

0 个答案:

没有答案
相关问题