Thread: Communication using RS232 port

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    146

    Communication using RS232 port

    can anyone guide me how can I send and receive (transfer) files between 2 computers using RS232...pls also give some link about the basics because I have no background of this concept nor any good material

    Thanks,
    Edesign

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Also, one more question..where can I learn VB? Is there any forum of this group related to VB??

    Thanks,
    Edesign

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The principle of serial communication is pretty simple, but there is no definite portable standard, so it would depend on several things....

    What hardware/OS are you using?

    Do you have a particular protocol in mind?


    As to VB, I'm sure there are people on the board that knows VB, but there is no board on this site for VB discussions in particular.

    --
    Mats
    Last edited by matsp; 03-12-2008 at 03:29 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    basically, i want to transfer a file between 2 pcs using an RF module(or an optical fiber)..I wrote a program using outportb which sent characters but now need to do it with entire file...i will be using windows xp and at college I have IBM machine...the hardware part is already available(for some other purpose we made it actually)...but have no idea about the software...where to start with..

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you have a PC running XP and an IBM <what architecture> running <what OS>?

    On the PC side, you could open the serial port with CreateFile().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Hello,

    I actually wrote a small program, but now the trouble is...I am using inport and outport in my program and I have included <dos.h>...still compiling it in Visual studio gives an error that these functions are not defined...

    Edesign

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you're actually using RS232...
    http://msdn2.microsoft.com/en-us/library/ms810467.aspx

    gg

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Oh my God...It seems so tough to me...Isn't there any other way...
    Or..pls if someone can give me a brief idea in simpler language...Or where can I get examples and overview that sort of explanation??

    Edesign

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    http://electrosofts.com/serial/ should help.
    Try using a DOS based compiler like Turbo C.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by edesign View Post
    Oh my God...It seems so tough to me...Isn't there any other way...
    Or..pls if someone can give me a brief idea in simpler language...Or where can I get examples and overview that sort of explanation??

    Edesign
    Here's some code that I've used in the past to communicate with a security controller which uses a Motorola 68000 processor.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    
    HANDLE hPort;
    
    BOOL WriteByte(BYTE bybyte)
    {
        DWORD iBytesWritten=0;
        DWORD iBytesToRead = 1;
        if(WriteFile(hPort,(LPCVOID) 
            &bybyte,iBytesToRead,&iBytesWritten,NULL)==0)
            return FALSE;
        else return TRUE;
    }
    
    BOOL WriteString(const void *instring, int length)
    {
        int index;
        BYTE *inbyte = (BYTE *) instring;
        for(index = 0; index< length; ++index)
        {
            if (WriteByte(inbyte[index]) == FALSE)
                return FALSE;
        }
        return TRUE;
    }
    
    BOOL ReadByte(BYTE  &resp)
    {
        BOOL bReturn = TRUE;
        BYTE rx;
        DWORD dwBytesTransferred=0;
    
        if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
        {
            if (dwBytesTransferred == 1)
            {
                resp=rx;
                bReturn  = TRUE;
            }
            else bReturn = FALSE;
        }
        else    bReturn = FALSE;
        return bReturn;
    }
    
    BOOL ReadString(void *outstring, int *length)
    {
        BYTE data;
        BYTE dataout[4096]={0};
        int index = 0;
        while(ReadByte(data)== TRUE)
        {
            dataout[index++] = data;
        }
        memcpy(outstring, dataout, index);
        *length = index;
        return TRUE;
    }
    
    void ClosePort()
    {
        CloseHandle(hPort);
        return;
    }
    
    HANDLE ConfigureSerialPort(LPCSTR  lpszPortName)
    {
        HANDLE hComm = NULL;
        DWORD dwError;
        DCB PortDCB;
        COMMTIMEOUTS CommTimeouts;
        // Open the serial port.
        hComm = CreateFile (lpszPortName, // Pointer to the name of the port
            GENERIC_READ | GENERIC_WRITE,
            // Access (read-write) mode
            0,              // Share mode
            NULL,           // Pointer to the security attribute
            OPEN_EXISTING,  // How to open the serial port
            0,              // Port attributes
            NULL);          // Handle to port with attribute
        // to copy
    
        // Initialize the DCBlength member.
        PortDCB.DCBlength = sizeof (DCB);
        // Get the default port setting information.
        GetCommState (hComm, &PortDCB);
        // Change the DCB structure settings.
        PortDCB.BaudRate = 9600;              // Current baud
        PortDCB.fBinary = TRUE;               // Binary mode; no EOF check
        PortDCB.fParity = TRUE;               // Enable parity checking
        PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control
        PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control
        PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
        // DTR flow control type
        PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity
        PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx
        PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control
        PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control
        PortDCB.fErrorChar = FALSE;           // Disable error replacement
        PortDCB.fNull = FALSE;                // Disable null stripping
        PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
        // RTS flow control
        PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on
        // error
        PortDCB.ByteSize = 8;                 // Number of bits/byte, 4-8
        PortDCB.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space
        PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2
    
        // Configure the port according to the specifications of the DCB
        // structure.
        if (!SetCommState (hComm, &PortDCB))
        {
            printf("Could not configure serial port\n");
            return NULL;
        }
        // Retrieve the time-out parameters for all read and write operations
        // on the port.
        GetCommTimeouts (hComm, &CommTimeouts);
        // Change the COMMTIMEOUTS structure settings.
        CommTimeouts.ReadIntervalTimeout = MAXDWORD;
        CommTimeouts.ReadTotalTimeoutMultiplier = 0;
        CommTimeouts.ReadTotalTimeoutConstant = 0;
        CommTimeouts.WriteTotalTimeoutMultiplier = 0;
        CommTimeouts.WriteTotalTimeoutConstant = 0;
        if (!SetCommTimeouts (hComm, &CommTimeouts))
        {
            printf("Could not set timeouts\n");
            return NULL;
        }
        return hComm;
    }
    
    int main(void)
    {
        //  Can also use COM2, COM3 or COM4 here
        hPort = ConfigureSerialPort("COM1");
        if(hPort == NULL)
        {
            printf("Com port configuration failed\n");
            return -1;
        }
        // Call your ReadString and WriteString functions here
        ClosePort();
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    i wrote a small code using inportb & outportb....just checked with a serial cable connected between 2 pcs...
    I am able to transfer files but facing 2 problems...
    1) even though the baud rate set is very high 38400bps, it takes too much of time..I mean
    by this baud rate ideally it should have a datarate of 3.75KBps, but to transfer a 15KB file it takes more than 1.5-2 minutes..I don't know why..

    2) this is the code used for reception
    Code:
     void receivefile(FILE *out)
     {
      do
      {
       c=inportb(PORT1+5);
       if(c&1)
       {
         cha=inportb(PORT1);
         fputc(cha,out);
         delay(10);
       }
      }while(1);
     }
    this does the work, but every time i need to break the execution..guessing it might have comlete reception..Is there some sort of acknowledge mechanism..so that i can know tht file transfer is complete...

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    delay(10);
    Would you think that waiting 0.01 seconds every character you output could have some effect on the transfer time? At 38400, this is equivalent of waiting for 384 bit times - so 38 characters, making it 39 two out of five waits.

    Also calling fputc on every character will be noticably less efficient than buffering up some amount (such as 256 to 1024 bytes) and storing that using fwrite().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hey edesign
    I have book called
    Code:
     interfacing-with-c-programming-real-world-applications
    it's mostly about PARALLEL PORT but it might help you. If you need it just send me a PM .
    thanks.

  15. #15
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    this does the work, but every time i need to break the execution..guessing it might have comlete reception..Is there some sort of acknowledge mechanism..so that i can know tht file transfer is complete...
    Guess you can use a sentinel character in the file you are sending so that you can check it in the received file.
    Suppose if it is '#'
    Like so...
    Code:
      do
      {
       // code to receive
      }while(cha!='#');
    instead of
    Code:
     do
     {
       // code to receive
      }while(1);
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. codes for com port Rs232 in serial communication
    By justinwandji in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2009, 01:33 AM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Communication over parallel port
    By Istari in forum C Programming
    Replies: 5
    Last Post: 08-25-2004, 03:22 AM
  5. Need help or info about serial port communication
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2002, 01:48 PM