stricky problem with converter

Thread Starter

anhnhamoi

Joined Apr 24, 2012
11
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:
Rich (BB code):
#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!=0)                                                     
            {   
                // Send data. if succesful, WriteFile() returns non-zero 
                WriteFile(comport,        // Handle
                          &s,      // 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='\0';
 
   }
    // Restore time-out parameters 
    SetCommTimeouts(comport,&savedComTimeouts);
    CloseHandle(comport);
    getch();
    return(0);

}


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.
 

Attachments

Top