Serial communication between pc and arduino via RS232 using c++

 ̄綄美尐妖づ 提交于 2019-12-11 11:41:09

问题


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.

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

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

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


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);
        }
}

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");
}

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

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

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?


回答1:


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.




回答2:


Ehm... No! pull up and pull down are not for this reason..

  • TTL = low: 0V, high: 5V

  • RS232 = low: +3:+15V, high: -3:-15V

Consequently.. You need a voltage converter (and inverter), like David Skogan correctly pointed out.

Examples:

  1. Using discrete components (has automatic echo feature, i.e. on the PC you will see the data you send): http://project.irone.org/simple-rs232-to-ttl-level-converter.html or http://circuit-diagram.hqew.net/Simple-TTL$2dRS232-Level-Converter-Using-Transistor_2757.html
  2. Common circuit with a MAX232 (or equivalent) and four capacitors
  3. Instead of using a USB-RS232 converter use a USB-UART one, using for instance a FT232 or something like that. This does not need any interface

Or.. simply use the USB port on the Arduino, which already has a FT232 on it.

Personal comment: i'd avoid solution 1...



来源:https://stackoverflow.com/questions/25020887/serial-communication-between-pc-and-arduino-via-rs232-using-c

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