arduino and visual studio c++, 2 way serial communication

纵饮孤独 提交于 2019-12-08 10:32:32

问题


I am using Arduino and Visual studio c++ and trying to build a two-way real time serial communication. 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.

I used the library files from http://playground.arduino.cc/Interfacing/CPPWindows, which are two files: SerialClass.h and Serial.cpp. And I am using readData() and WriteData() function in my main.

I want to run it for few more times, user can give an input in console and Arduino will generate an output accordingly. But when I added the while loop, it doesn't work properly.

Below is my main.cpp:(with while loop in comment line)

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:

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:

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

But when add while loop, it skips asking for an input, and my output becomes:

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. 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!


回答1:


As suggest that post "How do I flush the cin buffer?", the problem is located after your std::cin.get(command, 2); code. Extra characters stay in the std::cin and be reused directly at the next call. And the first extra character is '\n' (Enter Key) and the std::cin.get() will return 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);


来源:https://stackoverflow.com/questions/41393918/arduino-and-visual-studio-c-2-way-serial-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!