首页 文章

C#中的Arduino数字引脚映射

提问于
浏览
1

我正在编写一个与Arduino Mega 2560交互的C#应用程序 . 但是我使用Arduino的主板很新 .

所以我的问题是可以映射C#应用程序中的引脚,以监控我的数字输入的状态 .

例:

我像这样制作Arduino设置

pinMode(22, INPUT)
(...)
pinMode(53, INPUT)

然后在C#中我可以读取一个特定的引脚来获得它的状态 . 惠特之类的东西:

serialPort1.Read("pin44"); //and with that read I can see if its in HIGH or LOW.

要么

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int pinNumber = serialPort1.**ReadPinNumber()**; //I would store the number of which pin is generating the data and do something with it.
}

我不知道我是否足够清楚,但我需要的是不断从arduino接收数据并知道来自哪个引脚,所以我可以根据我的需要做一些事情 .

我已经成功完成了第一部分 . 我可以收到数据并做一些事情 . 但是当涉及多个引脚时是棘手的部分 .

先感谢您 .

1 回答

  • 0

    不确定你是否可以这样做,但你能在Arduino中做这样的事情:

    int pin7val = 0;     // variable to store the read value
    
    void setup()
    {
      Serial.begin(9600);
      pinMode(7, INPUT);      // sets the digital pin 7 as input
    }
    
    void loop()
    {  
      pin7val = digitalRead(7);   // read the input pin 7
      Serial.write(pin7val + " Pin44");
    }
    

    然后在视觉工作室做一些简单的事情

    string Serial = serialPort1.ReadLine();
    
    if (Serial contains "44")
    {
    int pin44val = Serial();
    }
    

    C#语法可能都不正确,但你明白了 .

相关问题