简体   繁体   English

arduino和Visual Studio C ++,2路串行通信

[英]arduino and visual studio c++, 2 way serial communication

I am using Arduino and Visual studio c++ and trying to build a two-way real time serial communication. 我正在使用Arduino和Visual Studio c ++,并试图建立双向实时串行通信。 What I am using is win 10(in VMware Fusion), 32-bit system, visual studio 2013, Arduino IDE 1.8.0 and Arduino board Uno. 我正在使用的是win 10(在VMware Fusion中),32位系统,Visual Studio 2013,Arduino IDE 1.8.0和Arduino开发板Uno。

I used the library files from http://playground.arduino.cc/Interfacing/CPPWindows , which are two files: SerialClass.h and Serial.cpp. 我使用了来自http://playground.arduino.cc/Interface/CPPWindows的库文件,它们是两个文件: SerialClass.hSerial.cpp。 And I am using readData() and WriteData() function in my main. 我在主程序中使用readData()WriteData()函数。

I want to run it for few more times, user can give an input in console and Arduino will generate an output accordingly. 我想再运行几次,用户可以在控制台中输入,而Arduino将相应地生成输出。 But when I added the while loop, it doesn't work properly. 但是,当我添加while循环时,它无法正常工作。

Below is my main.cpp:(with while loop in comment line) 下面是我的main.cpp :(在注释行中带有while循环)

int main() {
    Serial* port = new Serial("COM3");
    if (port->IsConnected()) cout << "Connected!" << endl;

    char data[4] = "";
    char command[2] = "";
    int datalength = 4;  //length of the data,
    int readResult = 0;
    int n;

        for (int i = 0; i < 4; ++i) { data[i] = 0; } //initial the data array

        //read from user input 
        //this is where I added while loop 
      // while(1){
        std::cout << "Enter your command: ";
        std::cin.get(command, 2);     //input command 
        int msglen = strlen(command);
        if (port->WriteData(command, msglen));   //write to arduino
        printf("\n(writing success)\n");

        //delay
        Sleep(10);

        //read from arduino output
        n = port->ReadData(data, 4);
        if (n != -1){
            data[n] = 0;
            cout <<"arduino: " data << endl;
        }
     // } 

    system("pause");
    return 0;
}

and my arduino code: 和我的arduino代码:

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    }


void loop() {
    // put your main code here, to run repeatedly:
    if (Serial.available() > 0) {
        char c = Serial.read();
        if (c == '1') 
          Serial.write("10");
        else if (c == '2') 
          Serial.write("20");
        else if (c == '3') 
          Serial.write("30");
        else
        Serial.write("Invalid");
    }

}

And if I run my code without while loop, I can get what I want: 如果我在不使用while循环的情况下运行代码,则可以得到想要的结果:

Connection established!!!
Enter your command: 1
arduino: 10

But when add while loop, it skips asking for an input, and my output becomes: 但是当添加while循环时,它会跳过要求输入的内容,而我的输出将变成:

Enter your command: 1
arduino: 10
Enter your command: arduino:
Enter your command: arduino:
Enter your command: arduino:
Enter your command: arduino:
...

After trying some solutions, I think it could be buffer data[] and command[] , I didn't empty it before the next run. 在尝试了一些解决方案后,我认为它可能是buffer data []和command [],在下一次运行之前我没有将其清空。 But I have tried with 但是我尝试过

memset(data,0,4); 

or 要么

data[4]='\0';

But it still doesn't work and skip asking for input. 但是它仍然不起作用,并跳过要求输入的内容。 Any suggestion how can I solve it? 有什么建议可以解决吗? Thanks! 谢谢!

As suggest that post "How do I flush the cin buffer?" 正如建议“如何冲洗cin缓冲液?” , the problem is located after your std::cin.get(command, 2); ,问题出在您的std::cin.get(command, 2); code. 码。 Extra characters stay in the std::cin and be reused directly at the next call. 多余的字符保留在std::cin并在下一次调用时直接重用。 And the first extra character is '\\n' (Enter Key) and the std::cin.get() will return 0. 第一个额外的字符是'\\n' (Enter键),并且std::cin.get()将返回0。

The best solution is to ignore extra characters after getting the command. 最好的解决方案是在获取命令后忽略多余的字符。

std::cout << "Enter your command: ";
std::cin.get(command, 2);     //input command
std::cin.clear(); // to reset the stream state
std::cin.ignore(INT_MAX,'\n'); // to read and ignore all characters except 'EOF' 
int msglen = strlen(command);

Instead of 代替

std::cout << "Enter your command: ";
std::cin.get(command, 2);     //input command 
int msglen = strlen(command);

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

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