简体   繁体   English

Arduino和Visual C ++串行通信

[英]Arduino and Visual C++ Serial Communication

So I found this tutorial for Arduino Visual Studio Communication: http://playground.arduino.cc/Interfacing/CPPWindows 因此,我找到了用于Arduino Visual Studio Communication的本教程: http : //playground.arduino.cc/Interface/CPPWindows

After reading up on it I wrote a small program on the Arduino which reads a character's ASCII code and returns that incremented value. 阅读完之后,我在Arduino上编写了一个小程序,该程序读取字符的ASCII代码并返回该递增值。 I already tested it with the serial monitor. 我已经使用串行监视器对其进行了测试。 However, when I wrote the program below I not only receive the answer, but I also receive "garbage" as well. 但是,当我在下面编写程序时,不仅会得到答案,而且还会收到“垃圾”。

#include <iostream>
#include <string>
#include "Serial.h"

using namespace std;

int main(int argc, char* argv[])
{
    Serial * Arduino = new Serial("COM8");
    cout << "Communicating with COM7 enter data to be sent\n";
    char data[256] = "";
    int nchar = 256;
    char incomingData[256] = "";
    while (Arduino->IsConnected())
    {
        cin >> data;
        Arduino->WriteData(data, nchar);
        Arduino->ReadData(incomingData, nchar);
        cout << incomingData << endl;
    }
    return 0;
}

The ouput looks as follows: 输出看起来如下:

F:\Serial\Debug>Serial.exe
Communicating with COM7 enter data to be sent
1
2☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
a
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺b☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺╠╠╠╠╠╠╠╠
^C
F:\Serial\Debug>

Can anyone shed light into how I should go about changing this code so that I can send and recieve a specific number of characters or in this case one character. 任何人都可以阐明我应该如何更改此代码,以便我可以发送和接收特定数量的字符,在这种情况下为一个字符。 I have already tried changing nchar to 1 so that it would only send a single character; 我已经尝试过将nchar更改为1,以便只发送一个字符。 however, this causes the output to display out of sync. 但是,这会导致输出显示不同步。 Any help is appreciated thanks. 任何帮助表示赞赏,谢谢。

First of all, you always send nchar bytes, no matter how much (or little) you read from the user. 首先,无论您从用户那里读取了多少(或很少),都始终发送nchar字节。 And also the data you read from the serial port, you don't terminate it like a string. 而且,您从串行端口读取的数据也不会像字符串一样终止。

For the first it's simple, stop using an array for that, and use std::string (to avoid possible buffer overflows), and send size bytes. 首先,很简单,停止使用数组,然后使用std::string (以避免可能的缓冲区溢出),然后发送size字节。

Then when receiving, you need to find out how many bytes you received, and then terminate the array like a string (or use std::string there too, there's a constructor that lets you pass in a pointer and a size). 然后在接收时,您需要找出接收到的字节数,然后像字符串一样终止数组(或者也可以在其中使用std::string ,有一个构造函数可以让您传递指针和大小)。

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

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