HELP! - RS232 Serial Stuff

I'm not sure why I'm having a heck of a time getting RS232 working with the Arduino (this is the new 328 chipset/board).

RS232 device requires: -19200 -1 start bit -1 stop bit -No flow -N parity

When I connect to pin 0 and 1 I can see data come in... however, it doesn't look correct. Not sure if this is because I'm also using the serial to monitor and program the Arduino? I tried using the SoftwareSerial and also get no results -- both sending and receiving to this device.

What I'm trying to achieve is sending out a hex message EXACTLY like this through the Arduino serial:

F0 00 00 7F 00 00 71 05 02 02 00 00 F1 23 00 01 00 00 00 01 13 F7

How do you guys recommend I do this!?

Any help would appreciated!

How about showing us the Arduino sketch that you are using to send the data? Maybe someone can help you debug it.

Regards,

Dave

Well, it's in such disarray I don't think it will help much. I'm thinking that my problem may be that the Arduino outputs TTL and I need true RS232. So that may be my problem.

Here is how I'm sending out hardcoded for now:

void sendToSerial(){

// F0 00 00 7F 00 00 71 05 02 02 00 00 F1 23 00 01 00 00 00 01 13 F7 mySerial.print(0xF0, HEX); mySerial.print(0x00, HEX); mySerial.print(0x00, HEX); mySerial.print(0x7F, HEX); mySerial.print(0x00, HEX); mySerial.print(0x00, HEX); mySerial.print(0x71, HEX); mySerial.print(0x05, HEX); mySerial.print(0x02, HEX); mySerial.print(0x02, HEX); mySerial.print(0x00, HEX); mySerial.print(0x00, HEX); mySerial.print(0xF1, HEX); mySerial.print(0x23, HEX); mySerial.print(0x00, HEX); mySerial.print(0x01, HEX); mySerial.print(0x00, HEX); mySerial.print(0x00, HEX); mySerial.print(0x00, HEX); mySerial.print(0x01, HEX); mySerial.print(0x13, HEX); mySerial.print(0xF7, HEX);

}

That is not sending it as true: F0 00 00 7F 00 00 71 05 02 02 00 00 F1 23 00 01 00 00 00 01 13 F7

But as: F0 0 0 7F 0 0 71 5 2 2 0 0 F1 23 0 1 0 0 0 1 13 F7

Which is incorrect if you notice that its not 00 but 0. I think this will cause the device to not respond to the command. Not sure yet as I can't really interface with it.

What's the story on TTL vs RS232?

not 00 but 0.

That’s because the HEX option of Serial.print does not print leading zeros. (It’s a “feature,” not a “bug.”) Since the numerical values appear to be correct, we can conclude that it has nothing to do with TTL or RS-232.

One way to making the hex output numbers show leading zeros is to use the sprintf() function with “%02X” format specifier. (I"ll also put the values in an array so that I don’t have to do the same thing 22 times, because I’m so lazy:

//
// Demo of sprintf to print hex bytes with leading zeros
//
// davekw7x
//
void setup()
{
    Serial.begin(9600);
}

void loop()
{
    byte byte_array[22] = {
        0xF0, 0x00, 0x00, 0x7F, 0x00, 0x00,
        0x71, 0x05, 0x02, 0x02, 0x00, 0x00,
        0xF1, 0x23, 0x00, 0x01, 0x00, 0x00,
        0x00, 0x01, 0x13, 0xF7
    };
    // Two hex digits, a space, and the terminating zero byte are all we need here
    char buffer[4];
    
    for (int i = 0; i < sizeof(byte_array); i++) {
        sprintf(buffer, "%02X ", byte_array[i]);
        Serial.print(buffer);
    }
    Serial.println();
    delay(5000);  // Or put an infinite loop here if you only want to do it once.
}

Output


F0 00 00 7F 00 00 71 05 02 02 00 00 F1 23 00 01 00 00 00 01 13 F7

So the formatting works great… thanks!

However, the device is not responding to the command. I get feedback from the device but the values are not what they should be. I seem to get the correct amount of value back, just wrong values. Is this a TTL vs RS232 issue?

[edit]Ohhhh. I thought you were getting correct numerical values and just needed to format them. Oh, well...[/edit]

What is the device? Is it a TTL (logic level) device or an RS-232 signal level device? How is it connected to your Arduino? What values are you expecting?

I mean, the output that I showed was what the ATmega sent to the Serial Monitor window in Arduino through the USB/Serial connection.

In other words: Describe what your project is and what it is supposed to do.

Regards,

Dave

Let me restate the issue because my last post didn’t make much sense (been working on this all day… tired).

I have pin 0 and pin 1 connected from the Arduino to the serial port of the device. Pins 2 & 3 to the DB9 RS232 pins.

This a whole house audio system with RS232 control. I’m trying to get the Arduino to control it. Can I use TTL direct? Or do I need a RS232?

Also, should I be using pin 0 and pin 1 or SoftwareSerial? Seems weird to be using SerialMonitor and it connected to a device… thought you couldn’t “share” RS232.

Also, when I press a button on the device it sends via RS232 to the Arduino... but the values I get (via the Arduino Serial Monitor) are not the values the device should be sending, thus, my thinking I need a RS232 to TLL driver.

This a whole house audio system with RS232 control. I'm trying to get the Arduino to control it. Can I use TTL direct? Or do I need a RS232?

Unless the manual says you can use TTL level RS-232, then you probably can't - likely it is expecting standard RS-232 voltage levels, which is why it isn't working as you're expecting. In order to supply these, you'll need to do some level conversion.

One way (probably the simplest way) is to wire up something like a MAX232 level converter chip:

http://www.maxim-ic.com/datasheet/index.mvp/id/1798

Another option might be to try the "discrete component" approach, like was used on the original Arduino for RS-232 communication (which the USB and FTDI chipset replaced) - see:

http://arduino.cc/en/Main/ArduinoBoardSerial (see version 2 - link to schematic below)

http://webzone.k3.mah.se/k3dacu/arduino/releases/serial_v2/arduino_rs232_v2.png

If you open up the PNG format schematic - you'll see the DB9 RS-232 interface; you could hook that up to Pins 0/1 - or to your choice of pins if you want to use software serial control.

Hope this helps...

:)

It specifically needs RS232 not TTL...

So, how does one debug using serial monitor if they use a Serial port on Pin 0 and Pin 1? Would I remove the USB cable program and monitor from the RS232 port? Then when I want to test with the device, plug the RS232 cable into the device?

I have a couple of these units but I can't seem to figure out how to make it work with the arduino:

http://www.kronosrobotics.com/downloads/EZ232.pdf

You need an RS-232 to TTL adapter. I homebrewed some from MAX2232 chips and little boxes, but you can (almost surely) get assembled units from any of the Arduino or robotics vendors that you use and trust.

If you want true reliability, then using a real and dedicated level shifter chip like the MAX232 is the best option; they aren't expensive, and can take the headache out of things.

I have seen various kludges that use transistors and resistors and diodes instead of real guaranteed-to-work level shifters, but I can't recommend any.

Well - I recommended the one the old Arduino uses to him because, well - I figured if it was good enough for an Arduino...

:D

I'm sure that people on this forum will cheerfully give advice on which are likely to work. Just remember that what works for one PC might not work with another.

This is especially true with the cobbled-together discrete component level shifters; some RS-232 ports on PCs and other devices will take it OK, others expect "true" levels (+/- 12V):

http://en.wikipedia.org/wiki/RS-232

I would say if the device was made in the last 10 years, it will probably be ok with lower voltage swings, likely +/- 5V (but maybe not a +/- 3V). That Arduino circuit I believe is around +/- 5V, so it should be OK if you want to try it.

Before I got into the Arduino, I was using a Basic Stamp 2 board that was assembled from a kit, that used a similar discrete component level shifter circuit for RS-232 communications with the host PC, to upload data to the device; it worked pretty well with the PC I was using at the time.

YMMV, of course...

:)

Since the Arduino only have one true serial, which is shared with the USB. How do I "share" it or disable it so I can use a Serial Shield or a MAX232? Can the USB and Pins 0 and 1 be used with USB at the same time?

How will I program and debug if I don't have USB anymore? Would I use a RS232 to USB to program?

So, how does one debug using serial monitor if they use a Serial port on Pin 0 and Pin 1? Would I remove the USB cable program and monitor from the RS232 port? Then when I want to test with the device, plug the RS232 cable into the device?

That's one way; or you could try to communicate to the level shifter using a software serial port instead, and leave the hardware one alone to monitor via USB.

There's also a way (but I can't remember how) to temporarily disable the FTDI chipset (using a jumper to ground), and then use the TX/RX pins for whatever you want (typically if you want/need more I/O pins and not communications). So, you could put in that jumper whenever you needed to use the pins for your own level shifter to the RS-232 port.

But I can't remember what the pin was you tied LOW...hopefully someone else will.

:(

Ok. Thanks for all the info... I will try to get a MAX232 working and follow up!

Ok.. got my MAX232 driver working!! I'm sniffing the data coming from the Arduino using Docklight... the correct values are coming in as ASCII, but they are suppose to be coming in as HEX for the device to process.

How do I force it to send out as true HEX not ASCII... since the ASCII (which is HEX) when it actually gets converted to HEX is way off value wise!

the correct values are coming in as ASCII, but they are suppose to be coming in as HEX for the device to process.

Whether they should come in as ASCII or hex depends on how the data was sent.

since the ASCII (which is HEX) when it actually gets converted to HEX is way off value wise!

There are two possible solutions. One involves posting the code on the sender, so we can see how you are sending the data. There are optional flags, such as BYTE, that can be used in the Serial.print() function to control how data is output. Whether they are useful, or not, depends on where the conversion to ASCII is occurring. We need to see the code to determine that.

The other possible solution is to correctly convert the ASCII data back to integer values, which, apparently, is not happening now. If one microcontroller can correctly convert from integer to ASCII, another should be able to convert it back. Again, we'd need to see the code on the receiver to see where the conversion is failing.

Looked at it with fresh eyes... got everything working properly this morning. It was indeed TTL vs RS232.

THANKS!

Yup, got that... there was two problems. The RS232 driver solved the issue with sending to the device.

Thanks again!