用于打开和关闭Arduino LED的C ++控制台应用程序

时间:2015-07-30 20:48:06

标签: c++ arduino serial-port

我正在尝试编写c ++控制台和Arduino uno之间的接口。我想通过串口发送一个字符或整数到arduino。我写了一些代码(见下文),LED闪烁很短的时间。我希望它能继续存在,直到我输入另一个字母或数字。如何才能做到这一点?我使用了Arduino playground中的SerialClass.h和SerialClass.cpp。几个小时的google-fu无济于事。

这是c ++代码(Arduino代码如下):

#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h"    // Library described above
#include <string>
#include <iostream>

using namespace std;

bool weiter = true;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Welcome to the serial test app!\n\n" << endl;

    Serial* SP = new Serial("COM4");    // adjust as needed

    if (SP->IsConnected())
    //printf("We are connected\n");
    cout << "We are connected!\n" << endl;
    char sendingData[256] = "";
    int dataLength = 256;
    int writeResult = 0;

while (weiter == true) {
if (SP->IsConnected()) {


    cout << "Press o to turn LED on! Press anything else to turn LED off! " << endl;
    cin >> sendingData;
    writeResult = SP->WriteData(sendingData, dataLength);
    cout << "Data send: " << sendingData << endl;
}

}
return 0;
}

这是Arduino代码:

int incomingByte = 0;   // for incoming serial data
int led = 13;
void setup() {
    Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
    pinMode(led, OUTPUT);
    Serial.println("This is my LED program; press o to turn on the LED");
}

void loop() {

    // send data only when you receive data:
    if (Serial.available() > 0) {
            // read the incoming byte:
            incomingByte = Serial.read();

            // say what you got:
            //Serial.print("I received: ");
            //Serial.println(incomingByte, DEC);

            if (incomingByte == 111) //111 entspricht dem Buchstaben o
            {
              digitalWrite(led, HIGH);
              Serial.print("I received: ");
              Serial.println(incomingByte, DEC);
              Serial.println("LED is turned on");
                            }
            if (incomingByte != 111)
            { digitalWrite(led, LOW);
              Serial.print("I received: ");
              Serial.println(incomingByte, DEC);
              Serial.println("LED is turned off");




    }
    }
}

2 个答案:

答案 0 :(得分:0)

cin是行缓冲的。这意味着您只能在按Enter键时获取数据。而且输入也是一个角色。通常为0x0a'\n'。这不是o所以你的领导关闭了。

我要做的是让你的arduino忽略空白:

if(incomingByte == `\n` || incomingByte == `\r` || incomingbyte == ' ')
  return;

还会发生每次发送256个字符的原因,因为dataLength永远不会更改。所以做一些聪明的事情srtlen;)。

答案 1 :(得分:0)

Thanks David for your comment on my question. I found the answer by myself. I used serial.WriteData("o",1);

Here is the complete code of my main.cpp. Just in case it might be useful for others:

#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h"    // Library described above
#include <string>
#include <iostream>

using namespace std;


bool weiter = true;
int dummy1 = 0;


int _tmain(int argc, _TCHAR* argv[]) {


cout << "*** This is my Arduino LED app! ***\n" << endl;
//Serial* SP = new Serial("COM4");
//Serial serial("COM4");
Serial serial("COM4");

if (serial.IsConnected())
//printf("We are connected\n");
cout << "We are connected!\n" << endl;



while (weiter == true) {

cout << "Press 1 for LED on; press 0 for LED off!" << endl;
cin >> dummy1;
if (dummy1 == 1) {



if (serial.IsConnected()){
    serial.WriteData("o",1);
    cout << "LED is on!" << endl;
    cout << "Do you want to continue? 1 for continue, 0 for exit!" << endl;
    //printf("\nData sent successfully!\n");
     cin >> weiter;
 }
 }


else {

    serial.WriteData("p", 1 );
    cout << "LED is off!" << endl;
    cout << "Do you want to continue? 1 for continue, 0 for exit!" << endl;
    cin >> weiter;
  }
  }

if (weiter == 1)
{
    weiter = true;
}

if (weiter == 0) {
    weiter = false;
return 0;
}
}