在同一解决方案中从winforms项目运行arduino项目

时间:2017-03-21 17:19:52

标签: c# winforms visual-studio arduino

我已经在visual studio中做了一个解决方案,其中一个是winform应用项目和另一个arduino项目。在我的winform中,我添加了一个按钮。所以我希望每当我在winform中单击按钮时,arduino代码就会执行。请告诉我这样做的程序。

1 个答案:

答案 0 :(得分:1)

最简单的命令结构是单个字符。在C#端,打开一个串口并写下char:

// In the button handler write the command to the port
// Change port # to whatever
SerialPort serialPort = new SerialPort("COM3", 9600);
serialPort.Open();
serialPort.Write("1");
serialPort.Close();

在Arduino方面:

void setup () 
{ 
    // Enable serial port
    Serial.begin(9600); 
} 

char cmd;
void loop () 
{ 
    if (Serial.available()) {
        // Read byte from serial port 
        cmd = Serial.read(); 
        switch (cmd) { 
            case '1':
            // Command from PC rcvd - do something here
            break; 
        }
    } 
}