Reading from Serial Port Problem.

Reading from Serial Port Problem.

Post by Mark » Mon, 08 Mar 1999 04:00:00



Hi,
        Can someone help me with a problem I'm having reading from the
serial port.
I've set the port up so that anyone can read and write to it. The code
is fine initializing it i.e. no errors etc. I've also been successful
writing to it. When I was to read stuff from it things start going
wacky.

What I want to do is read 25 bytes from the port, into the array and
return. What end up happening is either nothing gets read or random
amounts of data. This is the code I use to read from the port:

...
 nBytesToRead = 25;
 bResult = 0;

 bResult = read(FileHandle, SensorBuffer, nBytesToRead);

 if (bResult == -1) {
        }

 cout << "Actual Char's Read : " << bResult << endl;

 for(int Counter = 0; Counter <= 25; Counter++) {
        cout << SensorBuffer[Counter] << endl;
        }
...

My set up code is:

 FileHandle = open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);

  if (FileHandle == -1)
    cout << "An Error occured while opening Serial Port" << endl;
  else
    cout << "Serial Port opened succesfully" << endl;

  /* Get Current Port Configuration */
  tcgetattr(FileHandle, &options);

  /* Set Baud Rate */
  cfsetispeed(&options,B38400);
  cfsetospeed(&options,B38400);

  options.c_cflag |= (CLOCAL | CREAD);

  /* No Parity, 8 Bits */
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_oflag &= OPOST;

  /* RAW input */
  options.c_lflag &= ~(ICANON | ECHO | ISIG);

  options.c_cc[VMIN] = 25;
  options.c_cc[VTIME] = 0;

  /* Flush buffers */
  tcflush(FileHandle, TCIFLUSH);

  /* Update Port with new Configuration */
  tcsetattr(FileHandle, TCSANOW, &options);

If someone code help me out I'd be most grateful.

Thanks

Mark

 
 
 

Reading from Serial Port Problem.

Post by Robert Wues » Tue, 09 Mar 1999 04:00:00



> Personally I'm mystified as to why there seems to be no way to get an
> open() to return immediately, and then use blocking IO thereafter.

There is.  It goes something like this:

 int fd,flags;
 fd = open( name, O_RDWR | O_NDELAY );
 flags = fcntl( fd, F_GETFL, 0 );
 fcntl( fd, F_SETFL, flags & ~O_NDELAY );

Robert

 
 
 

Reading from Serial Port Problem.

Post by Joe Pfeiffe » Tue, 09 Mar 1999 04:00:00




> > Personally I'm mystified as to why there seems to be no way to get an
> > open() to return immediately, and then use blocking IO thereafter.

> There is.  It goes something like this:

>  int fd,flags;
>  fd = open( name, O_RDWR | O_NDELAY );
>  flags = fcntl( fd, F_GETFL, 0 );
>  fcntl( fd, F_SETFL, flags & ~O_NDELAY );

Thank you!  For some reason, I was under the impression that you
couldn't manipulate O_NDELAY with fcntl(); looking at the man page
again, it turns out to be one of only two flags (the other being
O_APPEND) that you *can* manipulate with it.
--
Joseph J. Pfeiffer, Jr., Ph.D.       Phone -- (505) 646-1605
Department of Computer Science       FAX   -- (505) 646-1002
New Mexico State University          http://www.cs.nmsu.edu/~pfeiffer
 
 
 

Reading from Serial Port Problem.

Post by Mark » Wed, 10 Mar 1999 04:00:00


Thanks for the help, working...no probs !

Mark



Quote:>Hi,
>        Can someone help me with a problem I'm having reading from the
>serial port.
>I've set the port up so that anyone can read and write to it. The code
>is fine initializing it i.e. no errors etc. I've also been successful
>writing to it. When I was to read stuff from it things start going
>wacky.

>What I want to do is read 25 bytes from the port, into the array and
>return. What end up happening is either nothing gets read or random
>amounts of data. This is the code I use to read from the port:

>...
> nBytesToRead = 25;
> bResult = 0;

> bResult = read(FileHandle, SensorBuffer, nBytesToRead);

> if (bResult == -1) {
>        }

> cout << "Actual Char's Read : " << bResult << endl;

> for(int Counter = 0; Counter <= 25; Counter++) {
>        cout << SensorBuffer[Counter] << endl;
>        }
>...

>My set up code is:

> FileHandle = open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);

>  if (FileHandle == -1)
>    cout << "An Error occured while opening Serial Port" << endl;
>  else
>    cout << "Serial Port opened succesfully" << endl;

>  /* Get Current Port Configuration */
>  tcgetattr(FileHandle, &options);

>  /* Set Baud Rate */
>  cfsetispeed(&options,B38400);
>  cfsetospeed(&options,B38400);

>  options.c_cflag |= (CLOCAL | CREAD);

>  /* No Parity, 8 Bits */
>  options.c_cflag &= ~PARENB;
>  options.c_cflag &= ~CSTOPB;
>  options.c_cflag &= ~CSIZE;
>  options.c_cflag |= CS8;
>  options.c_oflag &= OPOST;

>  /* RAW input */
>  options.c_lflag &= ~(ICANON | ECHO | ISIG);

>  options.c_cc[VMIN] = 25;
>  options.c_cc[VTIME] = 0;

>  /* Flush buffers */
>  tcflush(FileHandle, TCIFLUSH);

>  /* Update Port with new Configuration */
>  tcsetattr(FileHandle, TCSANOW, &options);

>If someone code help me out I'd be most grateful.

>Thanks

>Mark

--
Mark
 
 
 

1. Problem reading from serial port

Hi.  I've been banging my head against this problem and was wondering if
anyone could help.  I'm trying to get my HC11 microcontroller to
communicate with my linux pc via the serial port.

I've written up some code for getting ASCII from the microcontroller onto
the linux box.  So far, all I'm getting is garbage.  Does anyone have a
routine that can help?  (Or, better yet, routines that will do both serial
input and output of ASCII characters?)

Thanks a million!

James

2. ppp and tooltalk issue

3. Serial I/O Problem: Reading/Writing the same Port

4. cannot load ehternet module

5. Serial port for read/write problem

6. Linux on Digital AXP 150 PC (Jensen PC)

7. Problems reading serial port under SCO 3.2

8. Need good .bashrc and .bash_profile

9. Problem reading from serial port

10. Serial Port reading problem

11. Serial Port Reading problem

12. Odd Serial Port problem reading from weather station

13. serial port on v490 and Serial port on StorEdge 3510