Arduino UNO R3 and MAX3232

Hello guys,

I know that I will be hated for making this topic. I know that there is a lot of tutorials about this but none of them is helping me.

I want to read data from RS232, I know that I cannot hook up directly RS to Arduino, so I bought some converter(RS232 - UART TTL) based on MAX3232. I have tried a lot of codes on the forum but none of them seems to work for me. For now I have:

#include <SoftwareSerial.h>
int incomingByte = 0;   // for incoming serial data
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
         mySerial.begin(9600);
}

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

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

What I am trying to do is at least print received data from RS232(it is old laptop with RS232 connection and program called "Terminal v1.93b by Br@y++").

Nothing is working... Is there something wrong with code? Shouldn't it work?

SoftwareSerial mySerial(10, 11); // RX, TX

You don't really have a mySerial connected to the Arduino, do you? Why such a dumb name for the instance? Why not name it to match what is really connected?

int incomingByte = 0;   // for incoming serial data

Why the hell are you using Byte in the name of something that is NOT a byte?

Where is your schematic?

int incomingByte = 0;   // for incoming serial data

Why the hell are you using Byte in the name of something that is NOT a byte?

Well at least there was a meaningful variable name there :slight_smile:

To the OP: how are you sure of the speed of the rs232 ? 9600 bauds for sure? Which pin is connected to what?

Well at least there was a meaningful variable name there

How can it be meaningful when it is wrong?

The name is meaningful (cf your previous comment on mySerial). The type is inappropriate

(And I had a smiley)

J-M-L:
Well at least there was a meaningful variable name there :slight_smile:

To the OP: how are you sure of the speed of the rs232 ? 9600 bauds for sure? Which pin is connected to what?

I am sure, because as I wrote I am using program to send/receive data on RS port. I can set up all connection details including speed.

Of course Rx and Tx from converter are connected to port 10/11. I was trying to switch them around but no success.

I have also tried some example code directly from Arduino Studio but also no success.

PaulS:

SoftwareSerial mySerial(10, 11); // RX, TX

You don't really have a mySerial connected to the Arduino, do you? Why such a dumb name for the instance? Why not name it to match what is really connected?

It is a example code, which was used in some Arduino tuts. So if you call person who wrote it a "dumb" then don't waste your time in this topic.

I asked for help nicely, you don't have to act like a buffon...

Tell us, how you connected the RS232 converter to Arduino.

Do you have common GND with converter and arduino?

Variable naming is very important. Even if you are copying from some other place, it's your responsibility to code it correct.

sarouje:
Tell us, how you connected the RS232 converter to Arduino.

Do you have common GND with converter and arduino?

Variable naming is very important. Even if you are copying from some other place, it's your responsibility to code it correct.

So I have RS232 comming out from PC to a converter socket(DB9). Converter(it is based on MAX3232 chip) have 4 pins: VCC(I have connected 5V from arduino), GND(GND from arduino), Rx and Tx. Rx i tried to connect to 10 and Tx to 11. I had no success with reading any data, so I swapped them around - also no success.

I am sorry for in naming variables good, but first I wanted code to work :stuck_out_tongue:

can you clarify Bauds & settings from your PC?

J-M-L:
can you clarify Bauds & settings from your PC?

Yes I can set up all connection details in terminal(program to read/send data to COM). Speed is 9600.

The default on Arduino side as you don't pass any extra param to begin() is 8 data bits, no parity, one stop bit

is that matching what you have as well on the PC?

Yes, it is:
Baud rate: 9600
Data bits: 8
Parity: none
Stop bits: 1
Handshaking: none

Do you have something like this?

with this declarationSoftwareSerial mySerial(10, 11); // RX, TX

  • RX is digital pin 10 and needs to connect to TX of the converter
  • TX is digital pin 11 and needs to connect to RX of the converter

do you have an oscilloscope?

I have this:

I found out something new.

So I have this code:

#include <SoftwareSerial.h>

SoftwareSerial RS232Out(10, 11); // RX, TX
int i = 0;

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

void loop()
{
 char buffer[4];
 if(i>30000) {
   Serial.println("Sending 02");
   sprintf(buffer, "%02X ", 0x02);
   RS232Out.write(buffer);
  i=0;
 }
 if (RS232Out.available()) {
   int inByte = RS232Out.read();
   Serial.write(inByte); 
 }
 if (Serial.available()) {
   int inByte = Serial.read();
   RS232Out.write(inByte); 
   Serial.write(inByte);
 }
 i++;
}

I connect my laptop RS232 output to the converter and I don't receive any messages on COM1.

BUT! I have also some converter from RS232 to USB and I connect this to my laptop(it's installing on COM4) and everything works fine. I can send and receive messages both ways.

Any idea why is that?

what's the purpose of

    sprintf(buffer, "%02X ", 0x02);
    RS232Out.write(buffer);

(feels a bit overkill to dynamically build a string from static number..."

"%02X " means

  • Minimum number of characters to be printed is 2
  • Left-pads the number with zeroes instead of spaces
  • un Unsigned hexadecimal integer in uppercase
  • and with a white space after

and You pass 0x02 as parameter - so that's the number 2 in decimal:

so basically you build the ascii string ['0','2',' ','\0'] --> "02 " in your char buffer and this is what you send out.

Now I will know for a future.
Didn't know that you can handle that another way - I'm a totally noob here, trying to learn some stuff.

I am still wondering why it's working when I use RS232 to USB and it is not working when I hook directly to RS232?

BUT! I have also some converter from RS232 to USB and I connect this to my laptop(it's installing on COM4) and everything works fine. I can send and receive messages both ways.

where do you connect the USB ??

Well isn't that obvious that I have connected USB to USB port xD

I connected it to the same laptop with the same software and the same code on arduino.

:slight_smile:

not 100% obvious - just wanted to be sure.

On the DB9 RS232 port you would actually need only 3 wires:

are you sure your unit is a 5V one? I just see VCC in the picture. could that be 3.3? (and you cooked it? :frowning: )

Hey,

thank you J-M-L for your interest in my problem :wink:

Well the converter is from 3.3V to 5V but it works perfectly fine.
Maybe I will explain it one more time.

When I am using RS232 port on my laptop and connect a cable there and the other end to the converter(which is connected to arduino), it is not working(no messages are coming through).

When I use USB to RS232 converter(one end is USB and I connect it to the laptop and second end is DB9 end and I connect it to the converter) instead normal RS232 port everything is working, all signals are coming through.

RS232 on laptop is working fine, because I was testing it with another computer.