# [SOLVED] propblem with RS232 to RS485 Pleased help!



## anhnha (Apr 26, 2012)

Hi all,
I had do the circuit like that.But I wire up in breadboard it is not work.In my project I need to communicate between computer(COMM PORT) and micro.But now I want to check 2 circuits that I had wire in breadboard:
-the first breadboard for RS232 to RS485
-the second breadboard for RS485 to RS232
in reality, I need 2 computers to communicate but now have only one therefore I used TX pin of comm port to transmit and also used RX of this port to receive data.
Because I only used on computer thus I had connect TX of comm port to 232 input of one breadboard and RX of comm port to 232 output of the other breardboard.And then I used the program i posted above to transmit and receive data from comm port.But when I am not use 5 Voltage connected to ICs and I supprised due to is read from comm port is exact but when power is connected then it is not work.At first I think that it receiced data may be TX and RX is connected to each other but when i used ohm meter to measure the resistance between TX and RX, i received the value is very large and this mean it is not connected.I am confused why it is not connected but i can received data and when power is connnected it is not work.
I have checked all connected in breadboard, all of them are good.Would you give me how to solved it,i have wire up the circuit in breadboard many times but all are the same.Finally I can not to transmit between two computer.Will i need add something in this circuit in reality?Are there anything wrong in my schematic?
would you help me check it.I have attach my file in this
And this is orginal circuit that i used:http://www.bb-elec.com/bb-elec/literature/485ptbr-3903ds.pdf


----------



## octaneman (May 13, 2009)

*Re: propblem with RS232 to RS485 Pleased help!*

Hi anhnha


If I understood you correctly you are using the +5v from the RS232 to power up the IC's correct ? 


2 Questions: 


1) Is the resistance between RX and TX 3000 ohms or more ? 

2) What is the voltage swing from the supply rails to the IC's?


----------



## anhnha (Apr 26, 2012)

*Re: propblem with RS232 to RS485 Pleased help!*

Thanks.


> If I understood you correctly you are using the +5v from the RS232 to power up the IC's correct ?


No.I used 5V power supply independantly.
In this schematic I used it like:
TX (RS232 port).-----> R1in(MAX232)----> R1out(MAX232)--->DI(MAX485)----------------RO(MAX485)--->T1in(MAX232)---->T1out(MAX232)---->RX(RS232 port)


> 1) Is the resistance between RX and TX 3000 ohms or more ?


It is very very big.It like the resistance of insulation


> 2) What is the voltage swing from the supply rails to the IC's?


It vary between 5 to 5.17 V
This is the measurement that I have do
For idle state:
TX(RS232) = -10.5V
RX(232) = 0.03V
DE=RE= 0V both U1 and U2.
R1out= 5.17V
T1in = 5.1V
For working state:
I regret that i have no oscilloscope right now.I used voltmeter to measure but I found that when I transmit data the voltage at TX of RS232 port is change a small amount.I vary between -7V to -9.8V
and I sitll received exacty data when power off.
And below is the program i had used and the result I received

```
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

#define TIMEOUT_CONSTANT    50
#define ESC                 27




int main(int argc, char* argv[])
{
    char*s="rs232 to rs485 converter";
    int          key_pressed   = 0,i=0;
    char         outchar       = 0;
    char         inchar        = 0;
    DWORD        bytes_written = 0;    // Number of bytes written to port
    DWORD        bytes_read    = 0;    // Number of bytes read from port
    COMMTIMEOUTS tempComTimeouts;      // Our temporary time-outs for COM1
    COMMTIMEOUTS savedComTimeouts;     // Stores the original time-outs
    HANDLE       comport       = NULL;  // Handle for COM port
    DCB          comSettings;          // Contains various port settings
    char buf[25];

    printf("com port communication");
    printf("COM1 program\n\n");
    
    // Open COM port
    if ((comport = 
          CreateFile("COM1", 
                    GENERIC_READ | GENERIC_WRITE,          // for reading and writing
                    0,                                     // exclusive access
                    NULL,                                  // no security attributes
                    OPEN_EXISTING,              
                    FILE_ATTRIBUTE_NORMAL,
                    NULL)) == INVALID_HANDLE_VALUE)
    {
        printf("Unable to open COM1.\n\n");
        printf("Press any key to exit.");
        getch();
        return(1);
    }

    printf("COM1 opened.\n\n");

    // Save time-out parameters for COM1 
    GetCommTimeouts(comport,&savedComTimeouts);
    
    // Set our time-outs 
    tempComTimeouts.ReadIntervalTimeout         = TIMEOUT_CONSTANT;
    tempComTimeouts.ReadTotalTimeoutMultiplier  = TIMEOUT_CONSTANT;
    tempComTimeouts.ReadTotalTimeoutConstant    = TIMEOUT_CONSTANT;
    tempComTimeouts.WriteTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
    tempComTimeouts.WriteTotalTimeoutConstant   = TIMEOUT_CONSTANT;
    SetCommTimeouts(comport,&tempComTimeouts);

    // Set Port parameters.
    // We make a call to GetCommState() first in order to fill
    // the comSettings structure with all the necessary values.
    // Then we change the ones we want and call SetCommState().
    GetCommState(comport, &comSettings);
    comSettings.BaudRate = CBR_9600;
    comSettings.StopBits = ONESTOPBIT;
    comSettings.ByteSize = 8;
    comSettings.Parity   = NOPARITY;
    comSettings.fParity  = FALSE;
    SetCommState(comport, &comSettings);
    
 
     while(1)
     {  
 
          i=0;   
          while(s[i]!=0)                                                     
            {   
                // Send data. if succesful, WriteFile() returns non-zero 
                WriteFile(comport,        // Handle
                          &s[i],      // Outgoing data
                          1,              // Number of bytes to write
                          &bytes_written, // Number of bytes written
                          NULL); i++;
            }
                                                                                   
        // Read data. if succesful, ReadFile() returns non-zero
        ReadFile(comport,                 // Handle
                   buf,                    // Incoming data
                   sizeof(buf),            // Number of bytes to read
                   &bytes_read,           // Number of bytes read
                   NULL); 
                   DWORD j;   

         printf("\n%s", buf);
         for(i=0;i<25;i++)
         buf[i]='\0';
 
   }
    // Restore time-out parameters 
    SetCommTimeouts(comport,&savedComTimeouts);
    CloseHandle(comport);
    getch();
    return(0);

}
```


----------



## octaneman (May 13, 2009)

*Re: propblem with RS232 to RS485 Pleased help!*

anhnha,


This is what I see so far;


You have set up a 555 timer in monostable mode I've calculated the pulse at 1.1 seconds (1100 ms). The trigger for the 555 timer is set from the MAX232 driver when it receives a signal from the RS232 port. Now according to the MAX232 driver datasheet there is an inverter in it that when a signal is 0 the output will be 1. On the timer if the trigger is set to 1 the output will also be 1. From what I see the output of the timer and the RS232 output sets the MAX487 state. So when RE is 0 RO is 1. When DE is 1 Y and Z are also 1. 

It would help if you knew what the frequency was on the output of the MAX487. 


2 Questions. 

1) Did you do a truth table to turn the MAX487 output on/off ? 

2) When you compiled your code are you sure there are no errors ?


----------



## anhnha (Apr 26, 2012)

*Re: propblem with RS232 to RS485 Pleased help!*



> You have set up a 555 timer in monostable mode I've calculated the pulse at 1.1 seconds (1100 ms). The trigger for the 555 timer is set from the MAX232 driver when it receives a signal from the RS232 port


Oh, I am sorry this is my mistake.In the schematic both C1 and C2 capacitor are 10nF.The the pulse is 1.1x100K.10nF=1.1ms
In this case, I used the baud rate is 9600bps, therefore the time to transmit each bit is 0.104 ms and one byte with 10 bits will take 1.04ms.All value I had took it from this website:
http://www.bb-elec.com/bb-elec/literature/485ptbr-3903ds.pdf


> Now according to the MAX232 driver datasheet there is an inverter in it that when a signal is 0 the output will be 1


No.It is not invert logic level.It only invert the voltage.I had tested it in breadboard.When I put 12V ( 0 logic level--232 standard) on RS232 inputs, then the TTL outputs of max232 is 0.01V(0 logic level ---TTL standard)
and vice versa
when I put -12V (1 logic level--232 standard) on RS232 inputs, then the TTL outputs of max232 is 5.03V(1 logic level ---TTL standard)
I found this tutorial and this is the theory that I used to built that schematic.If you don't mind, Would you take a look the Auto-switching Link in this link:
Jan Axelson's Lakeview Research


> 1) Did you do a truth table to turn the MAX487 output on/off ?


Yes.I did
When DE=RE=5V if DI is 5V the outputs A is 4.91V and B is 0.01V
 if DI is 0V the outputs A is 0.012V and B is 4.72V


> 2) When you compiled your code are you sure there are no errors ?


Yes.No error
I had rewire and built again in breadboard but it is same.I received data even when both 5V power supply is off and GND of RS232 is removed.But when power on and GND is tied to negative of 5V supply I received nothing.
I took many hours to check and do it but it still not work.
Thanks in advance.


----------



## octaneman (May 13, 2009)

*Re: propblem with RS232 to RS485 Pleased help!*

The truth table you took from the datasheet so believe that you are using the chip configuaration in (pic B). I saw the auto switching link and I circled a voltage devider circuit in (pic A) that I don't see in your diagram. Could this be what you are missing ?


----------



## anhnha (Apr 26, 2012)

*Re: propblem with RS232 to RS485 Pleased help!*



octaneman said:


> The truth table you took from the datasheet so believe that you are using the chip configuaration in (pic B). I saw the auto switching link and I circled a voltage devider circuit in (pic A) that I don't see in your diagram. Could this be what you are missing ?


In the test i had added the terminal resistor but it not change.Everything is same at before.


----------



## octaneman (May 13, 2009)

*Re: problem with RS232 to RS485 Pleased help!*

To me it appears as if the RS232 and the RS485 could be a ground loop problem.

2 Questions: 

Are the ground pins 0V ? 

Jan Axelsons article stated that when transmitting the driver should be disabled ....Is your circuit active ?


----------



## anhnha (Apr 26, 2012)

*Re: problem with RS232 to RS485 Pleased help!*

Thanks for your help.
I had debug my circuit step by step and there are several things make me confuse.

At first I had used the circuit of convert file[attached in this] to transmit data.With TX is tied to R1in of U5 and RX is tied to T1out of U6.Then i used the code that i had posted previously to transmit and received to/from com port.Then I am supprising that I can receive exactly data without power supply.
In order to debug the circuit I checked all components.First, all components in 
the circuit is removed except max232.
I put max232 into breadboard without any capacitors and power is used.After there is a strange problem that I can not understand and i think this is my problem.
The strange is that when I tied TX to one pin and RX to another pin of max232 and both 2 pins is can be any pin of max232, then if i run program to transmit and receive data to/from com port, the data in received exactly what to be sent.
I seem that all max232 pins is connected to each other result in TX and RX pins is connected to each other.
At fist thought, I think the Max232 is blown but when I checked it under the guide below, all results is relative right.
This is the guide to test max232:
"Power up the MAX232 and check voltage across supply pins to insure correct voltage input. 
The following two tests check each "TTL in - RS232 out" converter. 
Put 0 volts into pins 10 and 11, then check pins 7 and 14. Should have about 10 volts output on each. 
Put 5 volts into pins 10 and 11, then check pins 7 and 14 again. Should have about -10 volts output on each. 
If that succeeded, then check each "RS232 in - TTL out" converter. Connect pin 7 to pin8. Connect pin 13 to pin 14. This will be a "loopback test". 

Put 0 volts into pins 10 and 11, then check pins 9 and 12. Should have about .6 volts output. 
Put 5 volts into pins 10 and 11, then check pins 9 and 12 again. Should have about 4.5 volts output."
There it is make me confuse that in the circuit when i tied TX in one pins 
it is appearded that all pins is connected each other and I received data exactlly without power supply.


----------



## octaneman (May 13, 2009)

*Re: propblem with RS232 to RS485 Pleased help!*

Receiving data without power supply doesn't make sense, the circuit shouldn't be working at all. By doing the loop back test you figured out the problem.
Good job !

Please inform us how you will correct the circuit.


----------



## anhnha (Apr 26, 2012)

*Re: propblem with RS232 to RS485 Pleased help!*

Thanks all help.
At last my circuit have successed.I think the main problem is from the use only one computer to test.I found that the TX pin have to connected to DI pin of 2 max485.


----------

