Logo Search packages:      
Sourcecode: icomlib version File versions  Download package

PComm::PCOpen ( const char *  pcrDev = PCRDEV,
tcflag_t  baudRate = B9600 
)

Opens another file descriptor to the serial device.

Parameters:
pcrDev Serial port device to be opened.
baudRate Initial serial speed.
Opens a file descriptor to the device pcrDev (which is by default your system's PCRDEV define) at the speed baudRate (which is by default as delinated in your termios.h set to 9600 baud).

Warning: You can only call this as long as the object is alive. If you do destry the object remember to call this function only after setting up the port configuration correctly.

Returns:
true or false based on success value.

Definition at line 79 of file pcomm.cpp.

References fd, FDSet, newtio, oldtio, and timeOut.

Referenced by PComm(), PCP::PCPSetPort(), and resetCall().

{

    /* Open modem device for reading and writing and not as controlling tty
       because we don't want to get killed if linenoise sends CTRL-C.  */
    fd = open(pcrDev, O_RDWR | O_NOCTTY );
    if (fd <0) {
            perror(pcrDev); 
            return false; 
#ifdef DEBUG_VER_
      } else {
            fprintf(stderr, "PCOMM: PCOpen(%s, %ld): Success at %d\n", 
                  pcrDev, baudRate, fd);
#endif // DEBUG_VER_
      }     
      

    oldtio  = new termios;
    newtio  = new termios;


      // functions necessary to initialise socket sets for
      // use with the select() function(s).
      timeOut = new timeval;
      FDSet = new fd_set;
      // clear out that memory.
      bzero(timeOut, sizeof(struct timeval));
      bzero(FDSet, sizeof(fd_set));
      // remove any socket descriptors if necessary
      // and add socket 'fd' to the FDSet
      FD_ZERO(FDSet);
      FD_SET(fd, FDSet);
      // set default timeout values to five seconds
      timeOut->tv_sec  = 5;
      timeOut->tv_usec = 0;
      // now we should be ready to use the select() statement.
      // just make sure that if the socket `fd' is closed
      // to repopulate the fd_set and to call select with
      // fd+1 as the first arg

    tcgetattr(fd,oldtio); /* save current serial port settings */
    bzero(newtio, sizeof(struct termios));

    /*
       BAUDRATE: Set bps rate. use cfsetispeed and cfsetospeed.
       CLOCAL  : local connection, no modem contol
       CREAD   : enable receiving characters
       ~HUPCL  : dont reset DTR to low when program finishes
       ~PARENB : dont enable parity bits
       ~CSTOPB : no stop bits
       ~CSIZE  :
       CS8     : 8n1 (8bit,no parity,1 stopbit)
     */
    cfsetispeed( newtio, baudRate );
    cfsetospeed( newtio, baudRate );
    newtio->c_cflag |=  (CLOCAL | CREAD);
    newtio->c_cflag &= ~HUPCL ;
    newtio->c_cflag &= ~PARENB ;
    newtio->c_cflag &= ~CSTOPB ;
    newtio->c_cflag &= ~CSIZE;
    newtio->c_cflag |= CS8;

    /* ICANON  : enable canonical input disable all echo functionality,
                 and don't send signals to calling program */
    newtio->c_lflag |= ICANON;
    newtio->c_lflag &= ~(ECHO | ECHOCTL);

    /* Raw output.  */
    newtio->c_oflag &= ~OPOST;

    /* now clean the modem line and activate the settings for the port */
    /* tcflush(fd, TCIFLUSH); */
    tcsetattr(fd,TCSANOW,newtio);

    return true;
}


Generated by  Doxygen 1.6.0   Back to index