2

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: Arduino and C++ (for Windows)

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?

6
  • 2
    You cannot connect RS232 cable pins directly to microcontroller pins for two reasons. First, the excessive voltage will likely destroy the inputs. Second, the signals must be inverted in sense when moving between RS232 standards and logic level signals used by the ATMEGA's UART. You need a level translator, or to replace the true serial connection with a logic-level USB-serial one. Is there a reason you can't use the Arduino's existing USB serial interface? Jul 29, 2014 at 18:31
  • 2
    Additionally the "FFFFFFFA" most likely comes from sign exstension when a mistakenly-signed byte value is implicitly converted to a larger int type. Declare your variable unsigned char instead. Jul 29, 2014 at 18:32
  • @ChrisStratton The only problem with working with the existing USB serial interface is I cannot figure out how to send data through USB using C++. It was trivial to learn how to send data through RS232, but I can't find any info on doing it through USB. Additionally, wouldn't there be a conflict if I tried to have one program writing COM5 while the Arduino's terminal was viewing COM5?
    – user50333
    Jul 29, 2014 at 19:41
  • The existing USB interface emulates a serial port, so you would use the exact same serial APIs, and simply a different port name (whichever one you use for downloading sketches). Also make sure to close the Arduino serial monitor before you try talking to it from your program. Jul 29, 2014 at 19:42
  • You don't send it through USB, you send it through the virtual COM port. Jul 29, 2014 at 19:43

2 Answers 2

2

It because that in your c++ application you haven't explicitly mentioned serial port speed. Mention the same baudrate which you have used in arduino here(c++).

Assuming that you have used rs232 to ttl converter, as you are getting some output, Which in other case wouldn't as you would have already burned your mcu.

0

You need some voltage converter between arduino and pc. PC uses "real" RS232 and arduino uses TTL level RS232. Check more in this site. You need something like this RS232 to TTL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.