2013-03-05 92 views
1

我正在写一个C#应用程序,它将与Arduino Mega 2560进行交互。 但我使用Arduino的主板很新颖。Arduino数字引脚映射C#

所以我的问题是有可能映射在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的接收数据,并知道从哪个引脚是从哪里来的,所以我可以根据自己的需要做些事情。

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

预先感谢您。

回答

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"); 
} 

然后在Visual Studio中做一些简单的说像

string Serial = serialPort1.ReadLine(); 

if (Serial contains "44") 
{ 
int pin44val = Serial(); 
} 

的C#语法ISN大概都是正确的,但你明白了。

+0

我以为这样的东西......但我有点觉得它是错误的方式来做到这一点......我想要的东西就像:我读了每一个引脚,像我建立一个字典,并更新其状态..它会像:读取引脚22 - >接收LOW - >存储当前状态。读引脚23 - >接收高 - >存储状态...这从C#而不是从arduino – 2013-03-06 04:09:01