arduino 和 visual studio c++,2 路串行通信
arduino and visual studio c++, 2 way serial communication
我正在使用 Arduino 和 Visual studio c++ 并尝试构建双向实时串行通信。
我用的是win 10(in VMware Fusion), 32位系统, visual studio 2013, Arduino IDE 1.8.0 and Arduino board Uno.
我使用了 http://playground.arduino.cc/Interfacing/CPPWindows 中的库文件,它们是两个文件:SerialClass.h 和 Serial.cpp. 而我在我的 main 中使用 readData() 和 WriteData() 函数。
我想再 运行 几次,用户可以在控制台中输入,Arduino 会相应地生成输出。但是当我添加 while 循环时,它不能正常工作。
下面是我的 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;
}
和我的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");
}
}
如果我 运行 我的代码没有 while 循环,我可以得到我想要的:
Connection established!!!
Enter your command: 1
arduino: 10
但是当添加 while 循环时,它会跳过输入请求,我的输出变为:
Enter your command: 1
arduino: 10
Enter your command: arduino:
Enter your command: arduino:
Enter your command: arduino:
Enter your command: arduino:
...
尝试了一些解决方案后,我认为可能是缓冲区数据[]和命令[],我在下一个运行之前没有清空它。但我试过
memset(data,0,4);
或
data[4]='[=16=]';
但它仍然不起作用并跳过询问输入。有什么建议我该如何解决?谢谢!
根据 post "How do I flush the cin buffer?" 的建议,问题位于您的 std::cin.get(command, 2);
代码之后。额外的字符保留在 std::cin
中,并在下次调用时直接重用。第一个额外字符是 '\n'
(回车键),std::cin.get()
将 return 0.
最好的解决办法是在获取命令后忽略多余的字符。
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++ 并尝试构建双向实时串行通信。 我用的是win 10(in VMware Fusion), 32位系统, visual studio 2013, Arduino IDE 1.8.0 and Arduino board Uno.
我使用了 http://playground.arduino.cc/Interfacing/CPPWindows 中的库文件,它们是两个文件:SerialClass.h 和 Serial.cpp. 而我在我的 main 中使用 readData() 和 WriteData() 函数。
我想再 运行 几次,用户可以在控制台中输入,Arduino 会相应地生成输出。但是当我添加 while 循环时,它不能正常工作。
下面是我的 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;
}
和我的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");
}
}
如果我 运行 我的代码没有 while 循环,我可以得到我想要的:
Connection established!!!
Enter your command: 1
arduino: 10
但是当添加 while 循环时,它会跳过输入请求,我的输出变为:
Enter your command: 1
arduino: 10
Enter your command: arduino:
Enter your command: arduino:
Enter your command: arduino:
Enter your command: arduino:
...
尝试了一些解决方案后,我认为可能是缓冲区数据[]和命令[],我在下一个运行之前没有清空它。但我试过
memset(data,0,4);
或
data[4]='[=16=]';
但它仍然不起作用并跳过询问输入。有什么建议我该如何解决?谢谢!
根据 post "How do I flush the cin buffer?" 的建议,问题位于您的 std::cin.get(command, 2);
代码之后。额外的字符保留在 std::cin
中,并在下次调用时直接重用。第一个额外字符是 '\n'
(回车键),std::cin.get()
将 return 0.
最好的解决办法是在获取命令后忽略多余的字符。
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);