【问题标题】:C++ console application to turn on and off an Arduino LED用于打开和关闭 Arduino LED 的 C++ 控制台应用程序
【发布时间】:2015-07-30 20:48:06
【问题描述】:

我正在尝试编写 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");




    }
    }
}

【问题讨论】:

    标签: c++ arduino serial-port


    【解决方案1】:

    cin 是行缓冲的。这意味着您只有在按 Enter 时才能获取数据。并且enter也是一个字符。通常0x0a'\n'。这不是o,所以你的 LED 会关闭。

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

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

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

    【讨论】:

      【解决方案2】:

      感谢大卫对我的问题的评论。 我自己找到了答案。我用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;
      }
      }
      

      【讨论】:

      • 我在尝试编译时收到此错误:“错误 LNK2019:未解析的外部符号“public:__thiscall Serial::Serial(char const *)”(??0Serial@@QAE@PBD @Z) 在函数 _wmain" 中引用。我在 VS 2019 中创建了一个空项目。 Configuration Properties > Advanced 将“Character Set”设置为“Use Unicode Character Set”,Linker > System 将“SubSystem”设置为“Console (/SUBSYSTEM:CONSOLE)”。我还需要仔细检查其他项目设置吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多