Thread: Receiving characters on serial port

  1. #1
    Registered User
    Join Date
    Feb 2003

    Receiving characters on serial port

    Hello All,

    I am developing a serial port( RS232) application. I have soemproblem in receiving characters. There is communication between two processors.

    Receiving a single byte works fine. But if I do not know, how many bytes are there I will have to use following bytes. Since following loop in infinite loop it will never come out. Please let me know..

    This is sample routine to read a characters from the serial port.
    char lsr, rbr,typeahead[1000];
    Int nextput = 0;

    PHP Code:
    for( ;;)
    lsr inbyte 0x3FD);

    if ( (
    lsr 0x01) != )
    rbr inbyte(0x3F8);
    typeahead[nextput++]  = rbr;
    lsr lsr FE// Clear RI bit

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002

    Smile Is this what you want?

    Let me see if I got you right.

    you have a routine to read a single byte from the serial port (let us call it

    Now, you want a way to read all characters that arrive sequentially. Since you dont know how many characters will arrive, you will need a nice loop to do it.

    Well, There are two ways to do it.

    1. Use the First Byte to represent the number of characters that follow and then use the loop like this.....
    int counter;
    unsigned char noOfBytes;
    noOfBytes = fnReadSerByte();
    for(counter = 0; counter < noOfBytes; counter++)
      var[counter] = fnReadSerByte();
    2. Use a STOP character (any valid Character that will not be present in your transmission even by mistake), and read the serial port until the received character is NOT THAT CHARACTER.

    Hope it Helps.
    Help everyone you can

  3. #3
    Registered User
    Join Date
    Jul 2004
    I'm not quite sure what you're asking by your example.
    First off, are you using interrupts? That's a good way to do it when they're
    bursty. You read the Rx register as long as there are bytes to read, then you stop and do
    something else (like let the message parser read characters out of the buffer
    until IT has a complete message).

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Um dbtid the thread is nearly a month old and since the OP hasn't come back to offer more information the thread is dead. Please leave it there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM