繁体   English   中英

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

[英]C++ console application to turn on and off an Arduino LED

我正在尝试对c ++控制台和Arduino uno之间的接口进行编程。 我想通过串行端口向arduino发送一个char或一个整数。 我已经编写了一些代码(请参阅下文),并且LED闪烁了很短的时间。 我希望它保持打开状态,直到我输入另一个字母或数字。 如何才能做到这一点? 我使用了来自Arduino运动场的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");




    }
    }
}

cin是行缓冲的。 这意味着您仅在按Enter键时才能获取数据。 输入也是字符。 通常为0x0a'\\n' 而且这不是o因此您的led灯会关闭。

我要做的是通过以下方法使您的arduino忽略空格:

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

而且还会发生一次,因为您的dataLength从未更改,所以每次发送256个字符。 所以用srtlen ;)做一些聪明的事。

感谢大卫对我的问题的评论。 我自己找到了答案。 我用了serial.WriteData("o",1);

这是我的main.cpp的完整代码。 以防万一它可能对其他人有用:

#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;
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM