stricky problem with converter

Discussion in 'Embedded Systems and Microcontrollers' started by anhnhamoi, Apr 24, 2012.

  1. anhnhamoi

    Thread Starter New Member

    Apr 24, 2012
    11
    0
    Hi All,
    I made rs232 to rs485 and rs485 to rs232 to communicate between 2 computers.This is the schematic that i wire it in breabboard:
    and this is my program to test:
    Code ( (Unknown Language)):
    1.  
    2. #include <cstdlib>
    3. #include <iostream>
    4. #include <windows.h>
    5. #include <stdio.h>
    6. #include <conio.h>
    7. #include <string.h>
    8.  
    9. #define TIMEOUT_CONSTANT    50
    10. #define ESC                 27
    11.  
    12.  
    13.  
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17.     char*s="rs232 to rs485 converter";
    18.     int          key_pressed   = 0,i=0;
    19.     char         outchar       = 0;
    20.     char         inchar        = 0;
    21.     DWORD        bytes_written = 0;    // Number of bytes written to port
    22.     DWORD        bytes_read    = 0;    // Number of bytes read from port
    23.     COMMTIMEOUTS tempComTimeouts;      // Our temporary time-outs for COM1
    24.     COMMTIMEOUTS savedComTimeouts;     // Stores the original time-outs
    25.     HANDLE       comport       = NULL;  // Handle for COM port
    26.     DCB          comSettings;          // Contains various port settings
    27.     char buf[25];
    28.  
    29.     printf("com port communication");
    30.     printf("COM1 program\n\n");
    31.    
    32.     // Open COM port
    33.     if ((comport =
    34.           CreateFile("COM1",
    35.                     GENERIC_READ | GENERIC_WRITE,          // for reading and writing
    36.                     0,                                     // exclusive access
    37.                     NULL,                                  // no security attributes
    38.                     OPEN_EXISTING,              
    39.                     FILE_ATTRIBUTE_NORMAL,
    40.                     NULL)) == INVALID_HANDLE_VALUE)
    41.     {
    42.         printf("Unable to open COM1.\n\n");
    43.         printf("Press any key to exit.");
    44.         getch();
    45.         return(1);
    46.     }
    47.  
    48.     printf("COM1 opened.\n\n");
    49.  
    50.     // Save time-out parameters for COM1
    51.     GetCommTimeouts(comport,&savedComTimeouts);
    52.    
    53.     // Set our time-outs
    54.     tempComTimeouts.ReadIntervalTimeout         = TIMEOUT_CONSTANT;
    55.     tempComTimeouts.ReadTotalTimeoutMultiplier  = TIMEOUT_CONSTANT;
    56.     tempComTimeouts.ReadTotalTimeoutConstant    = TIMEOUT_CONSTANT;
    57.     tempComTimeouts.WriteTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
    58.     tempComTimeouts.WriteTotalTimeoutConstant   = TIMEOUT_CONSTANT;
    59.     SetCommTimeouts(comport,&tempComTimeouts);
    60.  
    61.     // Set Port parameters.
    62.     // We make a call to GetCommState() first in order to fill
    63.     // the comSettings structure with all the necessary values.
    64.     // Then we change the ones we want and call SetCommState().
    65.     GetCommState(comport, &comSettings);
    66.     comSettings.BaudRate = CBR_9600;
    67.     comSettings.StopBits = ONESTOPBIT;
    68.     comSettings.ByteSize = 8;
    69.     comSettings.Parity   = NOPARITY;
    70.     comSettings.fParity  = FALSE;
    71.     SetCommState(comport, &comSettings);
    72.    
    73.  
    74.      while(1)
    75.      {  
    76.  
    77.           i=0;  
    78.           while(s[i]!=0)                                                    
    79.             {  
    80.                 // Send data. if succesful, WriteFile() returns non-zero
    81.                 WriteFile(comport,        // Handle
    82.                           &s[i],      // Outgoing data
    83.                           1,              // Number of bytes to write
    84.                           &bytes_written, // Number of bytes written
    85.                           NULL); i++;
    86.             }
    87.                                                                                    
    88.         // Read data. if succesful, ReadFile() returns non-zero
    89.         ReadFile(comport,                 // Handle
    90.                    buf,                    // Incoming data
    91.                    sizeof(buf),            // Number of bytes to read
    92.                    &bytes_read,           // Number of bytes read
    93.                    NULL);
    94.                    DWORD j;  
    95.  
    96.          printf("\n%s", buf);
    97.          for(i=0;i<25;i++)
    98.          buf[i]='\0';
    99.  
    100.    }
    101.     // Restore time-out parameters
    102.     SetCommTimeouts(comport,&savedComTimeouts);
    103.     CloseHandle(comport);
    104.     getch();
    105.     return(0);
    106.  
    107. }
    108.  
    109. [/i][/i][/i]

    This schematic is only for test.I used only one computer to test, thus i tied TX of comm port to 232 input of max232 and RX like schematic.The problem is is that when i USED ground pin of comm port and tied to negative of 5V voltage supply, I received data exactly what I sent but When I not connected GND of comm port then I supprise when I received EVEN 5V voltage supply is not connected.BUt if GND of comm port is not used I only received if 5V voltage supply is not connected.Could you explain me why this happen?What should I do?And is there any wrong in this schematic?Thanks.
     
  2. anhnhamoi

    Thread Starter New Member

    Apr 24, 2012
    11
    0
    Can you help me explain me, i am confuse whether it need to tied GND of comm port or not?
     
Loading...