rs 232 
Author Message
 rs 232

I am looking for a small example of C code for reading lots of data
comming from
the RS232 port on a unix machine ( with System V (not Berkeley)) on a
SGI
machine. I programmed it memory mapped (fopen()), setbuf() is on and my
buffer should be large enough. Writting isn't the problem, but for some
reason I'm not able to read wat's comming in. In fact I want to know if
it's a programming problem or if it's the system receives his data  in a
different way.
If anyone could send me this, or explain it to me how this works, I'll
be greatfull to him/her.
Thanks



Mon, 13 Nov 2000 03:00:00 GMT  
 rs 232



Quote:
>I am looking for a small example of C code for reading lots of data
>comming from
>the RS232 port on a unix machine ( with System V (not Berkeley)) on a
>SGI
>machine.

The C language has no support for serial ports (or any other specific forms
of hardware). Unix does provide support for these things but you should
discuss that in a Unix related newsgroup such as comp.unix.programmer.

--
-----------------------------------------


-----------------------------------------



Mon, 13 Nov 2000 03:00:00 GMT  
 rs 232

Quote:

> I am looking for a small example of C code for reading lots of data
> comming from
> the RS232 port on a unix machine ( with System V (not Berkeley)) on a
> SGI
> machine. I programmed it memory mapped (fopen()), setbuf() is on and my
> buffer should be large enough. Writting isn't the problem, but for some
> reason I'm not able to read wat's comming in. In fact I want to know if
> it's a programming problem or if it's the system receives his data  in a
> different way.
> If anyone could send me this, or explain it to me how this works, I'll
> be greatfull to him/her.
> Thanks


I am currently working on a project using a SUN workstation with UNIX
System V which includes a serial data connection (involving both reading
and writing to the serial port).  I am not dealing with large amounts of
data as you however to read the data available from the serial port I
simply check to see how many bytes are actually available to be read
(using an ioctl function call) and if more than zero bytes are available
I simply use the 'read()' system call.  The folloowing is a code segment
illustarting this (including my port initialization:

int serialPortInit(char *SerialPortName){

   /* Holds all the parameters/settings of serial port. */
   struct termios newterm;

   if ((serialFd = open(SerialPortName, O_RDWR | O_NOCTTY)) < 0 ){  
      fprintf(stderr, "Could not open serial port.");
      return ERROR;
   }

   /*Set I/O flags*/ /*Note IXOFF was tried and caused more errors*/
   newterm.c_iflag = IGNBRK;

   /*Set local flags*/
   newterm.c_lflag = 0;
   newterm.c_oflag = 0 ;

  /*Set Control flags:  Allow 8 data bits, no parity and one stop bit.
*/
   newterm.c_cflag = (CS8 | CREAD | CLOCAL );

   /* Will wait until the specified amount of bytes are read from the
      serial port.  If set to 0, once the specified time (see below) has
      expired, the read call can return with no read data. */
   newterm.c_cc[VMIN] = 0;  
   newterm.c_cc[VTIME] = 0;  

   /* Set port speed. */
   cfsetospeed(&newterm, B9600);
   cfsetispeed(&newterm, B9600);

   /* Get rid of any data on the port by flushing. */
   if (tcflush(serialFd, TCIOFLUSH) == -1)
      fprintf(stderr, "Falied Flush\n");

   /* Set the serial port to the above settings. */
   if (tcsetattr(serialFd, TCSANOW, &newterm) == -1)
      fprintf(stderr, "Failed setattr\n");

   lrfInitialized = 1;
   return (1);

Quote:
}

fdHasCharacters (int fileDes)
{
  long int bytesToRead = 0 ;

  if(ioctl (fileDes, FIONREAD, (caddr_t) &bytesToRead) == -1) {
     printf("fdHasCharacters(): ioctl (...FIONREAD...) failed -- ") ;
     return (-1) ;  
  }
  return bytesToRead ; /* Number of bytes available at serial port. */

Quote:
}

int getReading(unsigned char *buffer){

   unsigned char *ptr;
   int numOfBytes, numReadBytes,
       success = 0;

   /* Read any bytes available at the serial port.  Can only obtain one
      reading at a time. */
   if(numOfBytes = fdHasCharacters(serialFd)){
      numReadBytes = (numOfBytes >= LR_READINGLEN) ? LR_READINGLEN - 1 :
                      numOfBytes;                                

      if(read(serialFd, ptr = buffer, numReadBytes ) == numReadBytes){
         success = 1;
         *ptr = '\0';
      }
   }
   return success;

Quote:
}

Where LR_READINGLEN is defined to be some integer value (12 in my case).

Hope this helps!!

-------------------------------------------------------------------
Bill Kapralos



Mon, 13 Nov 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. IBM Compat RS-232 Comm Lib???

2. Summary: Help with RS-232 Communication

3. Help in RS-232 communication

4. RS-232 routines in C

5. C++ RS-232 communications help

6. RS 232 COM Port

7. C source code for async/sync communcation using RS-232

8. C source for RS-232-C links ?

9. Use existing Active-X Control - Mscomm32.ocx for RS-232 Serial Communications

10. PROBLEMS IN READING FROM COMM1 (RS-232)

11. communication using RS-232 cable

12. How to access RS-232 port?

 

 
Powered by phpBB® Forum Software