Receiving characters on serial port

This is a discussion on Receiving characters on serial port within the Networking/Device Communication forums, part of the General Programming Boards category; Hello All, I am developing a serial port( RS232) application. I have soemproblem in receiving characters. There is communication between ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    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
    Posts
    192

    Smile Is this what you want?

    Hi,
    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
    fnReadSerByte().

    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.....
    Code:
    ......
    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.
    -Harsha.
    Help everyone you can

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    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
    Posts
    5,681
    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, 07:52 AM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 06:56 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21