问题描述
我正在使用Arduino和Visual Studio c ++,并试图建立双向实时串行通信.我正在使用的是win 10(在VMware Fusion中),32位系统,Visual Studio 2013,Arduino IDE 1.8.0和Arduino开发板Uno.
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.
我想再运行几次,用户可以在控制台中输入,而Arduino将相应地生成输出.但是,当我添加while循环时,它无法正常工作.
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;
}
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");
}
}
如果我在不使用while循环的情况下运行代码,我可以得到想要的东西:
And if I run my code without while loop, I can get what I want:
Connection established!!!
Enter your command: 1
arduino: 10
但是,当添加while循环时,它会跳过要求输入的内容,而我的输出将变成:
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:
...
尝试了一些解决方案后,我认为它可能是buffer data []和command [],在下一次运行之前我没有将其清空.但是我已经尝试过
memset(data,0,4);
data[4]='\0';
但是它仍然不起作用,并跳过要求输入的内容.有什么建议可以解决吗?谢谢!
But it still doesn't work and skip asking for input. Any suggestion how can I solve it? Thanks!
推荐答案
建议在如何冲洗cin缓冲区?" ,问题出在您的std::cin.get(command, 2);
代码之后.多余的字符保留在std::cin
中,并在下一次调用时直接重用.第一个额外的字符是'\n'
(Enter键),std::cin.get()
将返回0.
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);
代替
std::cout << "Enter your command: ";
std::cin.get(command, 2); //input command
int msglen = strlen(command);
这篇关于arduino和Visual Studio C ++,2路串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!