使用c ++通过RS232在pc和arduino之间进行串行通信 [英] Serial communication between pc and arduino via RS232 using c++

查看:28
本文介绍了使用c ++通过RS232在pc和arduino之间进行串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 RS232 线与我的 arduino Duemilanove 通信.我只是希望能够从桌面应用程序向我的 arduino 发送一个字节(或字符).Arduino 正在插入我电脑上的 USB COM5.我将 RS232 插入 COM1,然后将 RS232 另一端的引脚 2、3 和 5 分别连接到 arduino 引脚 TX、RX 和 GND.

I am trying to communicate with my arduino duemilanove via an RS232 cord. I simply want to be able to send a byte (or char) to my arduino from a desktop application. The Arduino is plugging into USB COM5 on my computer. I have the RS232 plugged into COM1, and then I have pins 2 3 and 5 on the other end of the RS232 connected to arduino pins TX, RX, and GND, respectively.

我在以下链接中找到了用于 C++ 的串行通信类:

I found a serial comm class for c++ at the following link:

http://playground.arduino.cc/Interface/CPPWindows

我已将上述示例中的 .h 和 .cpp 文件添加为 Serial.h 和 Serial.cpp(我认为该示例使用了 SerialClass.h 和 SerialClass.cpp,我只是更改了名称).

I have added the .h and .cpp files from the above example as Serial.h and Serial.cpp (i think the example uses SerialClass.h and SerialClass.cpp, I just changes the names).

在我的 arduino 上,我运行了以下代码:

On my arduino, I have the following code running:

// ARDUINO
char incomingByte = 0;

void setup() {
        Serial.begin(9600);
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, HEX);
        }
}

<小时>

我的 C++ 程序如下:


And my c++ program is the following:

// C++
#include <iostream>
#include <Windows.h>
#include "Serial.h"

using namespace std;

int main(void)
{
    Serial port("COM1");

    char* msg = "Hello Arduino!";
    int msgLen = strlen(msg);
    bool writeSuccess = port.WriteData(msg, msgLen);

    cout << "\n\n";
    system("PAUSE");
}

<小时>

当我使用 Arduino 的串行端口查看器查看打印的内容时,我得到了非常奇怪的值,这些值与我发送的内容不匹配(据我所知).


When I use the Arduino's serial port viewer to see what is bring printed, I'm getting very strange values that don't match what I'm sending (as far as I can tell).

当我发送Hello Arduino!"时,arduino 会打印以下内容:

When I send "Hello Arduino!", the arduino prints the following:

I received: FFFFFFAB
I received: 3A
I received: 3A
I received: A
I received: FFFFFFFA
I received: FFFFFFEB
I received: 6D
I received: 37
I received: 15
I received: 2D
I received: 23
I received: 21
I received: FFFFFFBD
I received: 0

这似乎不是Hello Arduino!"的正确十六进制,但我不知道为什么它不正确.有没有人知道我做错了什么?

This does not appear to be the correct hex for "Hello Arduino!", but I have no idea why it's not correct. Does anyone have any clue what I'm doing wrong?

推荐答案

Arduino 使用 TTL 逻辑进行串行连接.它期望值为 0 和 5V.RS232 使用不同的电压 -V 到 +V.您可能需要一个转换器.

Arduino used TTL logic for Serial connection. It expects values at 0 and 5V. RS232 used a different voltage -V to +V. You may need a converter.

这篇关于使用c ++通过RS232在pc和arduino之间进行串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆