CMUsbSerial Class Reference

Comunication through USB ( Serial ). More...

Public Member Functions

 CMUsbSerial (const char *port, int baud)
 ~CMUsbSerial ()
int read_bytes (char *data, int nbytes)
int read_fromto (char *data, char begin, char end)
int read_until (char *data, char end)
int write_bytes (char *data)

Private Member Functions

int init ()

Private Attributes

char * port
int fd
int baud

Detailed Description

Header for easy comunication through USB ( Serial ).

Author:
Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>

Codigo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Codigo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.


Constructor & Destructor Documentation

CMUsbSerial::CMUsbSerial ( const char *  port = "/dev/ttyUSB0",
int  baud = 115200 
)

Constructor.

Parameters:
[in] port The dev of the device.
[in] baud The bauds of the interface.

00035 {
00036      this->port = (char*)malloc((strlen(port)+1) * sizeof(char*));
00037      strcpy(this->port,port);
00038      this->baud=baud;
00039      //this->debug.debug("Trying to create the shared memory segment...");
00040      this->init();
00041      //if((this->id = shmget((key_t)this->id,this->buffer_size, IPC_CREAT | 0666))<0)
00042      //{ this->debug.error(strerror(errno),__FILE__,__FUNCTION__,(__LINE__)); }
00043 }

CMUsbSerial::~CMUsbSerial (  ) 

Destructor.

00046 {
00047   free(this->port);
00048 }


Member Function Documentation

int CMUsbSerial::init (  )  [private]

Initialize the usb for use it.

Returns:
Returns 0 if succesfull, -1 if not.

00051 {
00052      struct termios toptions;
00053 
00054      this->fd = open(this->port, O_RDWR | O_NOCTTY | O_NDELAY);
00055      if (this->fd == -1)  {
00056              perror("init_serialport: Unable to open port ");
00057              return -1;
00058      }
00059 
00060      if (tcgetattr(this->fd, &toptions) < 0) {
00061              perror("init_serialport: Couldn't get term attributes");
00062              return -1;
00063      }
00064      speed_t brate = this->baud; // let you override switch below if needed
00065      switch(this->baud) {
00066      case 4800:   brate=B4800;   break;
00067      case 9600:   brate=B9600;   break;
00068      #ifdef B14400
00069      case 14400:  brate=B14400;  break;
00070      #endif
00071      case 19200:  brate=B19200;  break;
00072      #ifdef B28800
00073      case 28800:  brate=B28800;  break;
00074      #endif
00075      case 38400:  brate=B38400;  break;
00076      case 57600:  brate=B57600;  break;
00077      case 115200: brate=B115200; break;
00078      }
00079      cfsetispeed(&toptions, brate);
00080      cfsetospeed(&toptions, brate);
00081 
00082      // 8N1
00083      toptions.c_cflag &= ~PARENB;
00084      toptions.c_cflag &= ~CSTOPB;
00085      toptions.c_cflag &= ~CSIZE;
00086      toptions.c_cflag |= CS8;
00087      // no flow control
00088      toptions.c_cflag &= ~CRTSCTS;
00089 
00090      toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
00091      toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
00092 
00093      toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
00094      toptions.c_oflag &= ~OPOST; // make raw
00095 
00096      // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
00097      toptions.c_cc[VMIN]  = 0;
00098      toptions.c_cc[VTIME] = 20;
00099 
00100      if( tcsetattr(this->fd, TCSANOW, &toptions) < 0) {
00101          perror("init_serialport: Couldn't set term attributes");
00102          return -1;
00103      }
00104 
00105 }

int CMUsbSerial::read_bytes ( char *  data,
int  nbytes 
)

Read from the USB ( Serial ).

Parameters:
[in] data Variable to put data.
[in] nbytes The number of bytes.
Returns:
Returns 0 if succesfull, -1 if not.

00108 {
00109      char b[1];
00110      int i=0;
00111      do {
00112          int n = read(this->fd, b, 1);  // read a char at a time
00113          if( n==-1) return -1;    // couldn't read
00114          if( n==0 ) {
00115              usleep( 10 * 1000 ); // wait 10 msec try again
00116              continue;
00117          }
00118          data[i] = b[0]; i++;
00119      } while( i < nbytes );
00120 
00121      data[i] = '\0';  // null terminate the string
00122      return 0;
00123 }

int CMUsbSerial::read_fromto ( char *  data,
char  begin,
char  end 
)

Read from the USB ( Serial ).

Parameters:
[in] data Variable to put data.
[in] begin The begin character.
[in] end The end character.
Returns:
Returns 0 if succesfull, -1 if not.

00126 {
00127      bool start=false;
00128      char b[1];
00129      int i=0;
00130      do {
00131          int n = read(this->fd, b, 1);  // read a char at a time
00132          if( n==-1) return -1;    // couldn't read
00133          if( n==0 ) {
00134              usleep( 10 * 1000 ); // wait 10 msec try again
00135              continue;
00136          }
00137          if( start == true)
00138          {
00139              data[i] = b[0]; i++;
00140          }
00141          if( b[0] == begin) start=true;
00142      } while( b[0] != end );
00143 
00144      data[i-1] = '\0';  // null terminate the string
00145      return 0;
00146 }

int CMUsbSerial::read_until ( char *  data,
char  end 
)

Read from the USB ( Serial ).

Parameters:
[in] data Variable to put data.
[in] end The end character.
Returns:
Returns 0 if succesfull, -1 if not.

00149 {
00150      char b[1];
00151      int i=0;
00152      do {
00153          int n = read(this->fd, b, 1);  // read a char at a time
00154          if( n==-1) return -1;    // couldn't read
00155          if( n==0 ) {
00156              usleep( 10 * 1000 ); // wait 10 msec try again
00157              continue;
00158          }
00159          data[i] = b[0]; i++;
00160      } while( b[0] != end );
00161 
00162      data[i-1] = '\0';  // null terminate the string
00163      return 0;
00164 }

int CMUsbSerial::write_bytes ( char *  data  ) 

Write data to USB ( Serial ).

Parameters:
[in] data Variable data from.
Returns:
Returns 0 if succesfull, -1 if not.

00167 {
00168      int len = strlen(data);
00169      int n = write(this->fd, data, len);
00170      if( n!=len )
00171          return -1;
00172      return 0;
00173 }


Field Documentation

int CMUsbSerial::baud [private]

The bauds.

int CMUsbSerial::fd [private]

The file descriptor.

char* CMUsbSerial::port [private]

The /dev/ttyUSB.

 All Data Structures Functions Variables
Generated on Mon Sep 12 07:59:09 2011 for API MokArdu by  doxygen 1.6.3