簡體   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