Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将串行数据读取到winforms_C#_Arduino - Fatal编程技术网

C# 将串行数据读取到winforms

C# 将串行数据读取到winforms,c#,arduino,C#,Arduino,让我们从这个开始吧,我不知道我在做什么。我已经做了一些花哨的C语言,但对于arduino我一无所知。幸运的是,这是我必须完成的最后一项关于arduino的任务,所以在这之后我就完成了 我有一个Adafruit RFID/NFC屏蔽,用来读取RFIC/NFC标签。这在Adafruit示例中非常有效。现在我让它把标签的ID记录到串口,我想让我的C程序对它做出反应 我已经在网上找了很多例子,但是关于这篇文章,没有什么有用的东西可以找到。因此,我在这里发布了一个问题。到目前为止,我已尝试使用,以及其他一

让我们从这个开始吧,我不知道我在做什么。我已经做了一些花哨的C语言,但对于arduino我一无所知。幸运的是,这是我必须完成的最后一项关于arduino的任务,所以在这之后我就完成了

我有一个Adafruit RFID/NFC屏蔽,用来读取RFIC/NFC标签。这在Adafruit示例中非常有效。现在我让它把标签的ID记录到串口,我想让我的C程序对它做出反应

我已经在网上找了很多例子,但是关于这篇文章,没有什么有用的东西可以找到。因此,我在这里发布了一个问题。到目前为止,我已尝试使用,以及其他一些解决方案port.ReadByte;,和

但这一切似乎都不正常。所有的解决方案都没有发现arduino返回的任何东西。我正在为C和Arduino使用Visual Studio 2015。首先,我可以在另一个VS实例占用串行控制台时运行C。它没有导致任何类型的错误,这显然是它应该有的。是的,这台计算机上的COM端口是正确的,是的,电缆工作正常

对于arduino,我使用以下代码:

#include <Wire.h>
#include <Adafruit_PN532.h>

#define IRQ   (2)
#define RESET (3)  // Not connected by default on the NFC Shield

int ledPin = 11;
uint8_t code[] = { 153, 220, 181, 78, 0, 0, 0 };
boolean firstTime = true;
boolean correct = true;

Adafruit_PN532 nfc(IRQ, RESET);

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");
  pinMode(ledPin, OUTPUT);

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0xFF);

  // configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A card");
}

void loop(void) {
  boolean success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

  if (success) {
      if (firstTime) { Serial.println("First time. Writing shit"); }
    Serial.println("Found a card!");
    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("UID Value: ");
    for (uint8_t i=0; i < uidLength; i++) 
    {
        Serial.print(" 0x");Serial.print(uid[i], HEX);
        if (firstTime) { 
            //code[i] = uid[i], HEX;
        };
    }
    /*Serial.println("");
    for (uint8_t i = 0; i < uidLength; i++) {
        Serial.println(uid[i], HEX);
        uint8_t temp = uid[i], HEX;
        String temp = "0x" + uid[i];
    }-*/

    firstTime = false;
    Serial.println("");
    Serial.println("Let's check it: ");
    digitalWrite(ledPin, HIGH);

    for (uint8_t i = 0; i < uidLength; i++) {
        //Serial.print("Looking for: "); Serial.println(code[i], HEX);
        Serial.print("Found: "); Serial.println(uid[i], HEX);
        if (code[i] == uid[i]) {
            Serial.println("Found match!");
            correct = true;
        }
        else {
            Serial.print("Wanted: "); Serial.println(code[i], HEX);
            correct = false;
        }
    }

    if (correct) {
        Serial.println("Complete code is correct!");
    }


    // Wait 1 second before continuing
    delay(1000);
  }
  else
  {
    // PN532 probably timed out waiting for a card
    Serial.println("Timed out waiting for a card");
  }
}
是的,这与adafruit测试程序几乎相同,不,这无关紧要。我只需要在星期二之前把它修好。我仍然需要更改arduino只将ID传递给串行监视器,但这是在它可以与C通信时要做的事情。它需要以两种方式进行通信:

该ID需要由C获取 C将返回一个布尔值,arduino需要根据该布尔值采取行动
但是我无法让C与SerialPort一起工作。有人能帮助我如何处理两端的串行通信吗?

一位朋友刚刚为我解决了这个问题。它很脏,但它能完成任务

    SerialPort serial;

    public Form1()
    {
        InitializeComponent();

        serial = new SerialPort();
        serial.PortName = "COM4";
        serial.BaudRate = 9600;
        serial.DtrEnable = true;
        serial.Open();

        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += new System.EventHandler(timer_Tick);

        timer.Start();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        String dataFromArduino = serial.ReadLine().ToString();
    }

再次感谢-1:p

感谢-1,但是你能解释一下遗漏了什么吗?我讨厌你只回答一个问题,而不说明你的观点有什么错。第1步,从方程中去掉C。下载putty或Termite,看看是否可以从arduino获得终端应用程序的输出。也许可以在reddit/r/arduino中寻求更多帮助。祝你好运我不知道我必须用C来完成作业。其他团队成员已经在数据库连接和UI上工作过。我的部分是arduino和C之间的联系,因此我的问题是C。我会看看你提到的节目,也许我可以从中学到一些有用的东西: