LxFTDI.cpp
Go to the documentation of this file.
00001 //============================================================================
00002 // Name        : LxFTDI.cpp
00003 // Author      : Wouter Caarls,www.dbl.tudelft.nl
00004 // Version     : 0.1
00005 // Copyright   : Copyright (c) 2013 LGPL
00006 // Description : serial communicatin class linux FTDI
00007 //============================================================================
00008 
00009 #include <stdio.h>
00010 #include <sys/time.h>
00011 
00012 #include <threemxl/LxFTDI.h>
00013 
00014 /*      constructor */
00015 LxFTDI::LxFTDI()
00016 {
00017 }
00018 
00019 /* open port */
00020 bool LxFTDI::port_open(const std::string& portname, LxSerial::PortType port_type)
00021 {
00022         int ret = dev.open(portname);
00023         if (ret < 0)
00024                 std::cerr << "Error opening FTDI device " << portname << ": " << dev.error_string() << std::endl;
00025 
00026         return !ret;
00027 }
00028 
00029 bool LxFTDI::set_speed_int(const int baudrate)
00030 {
00031         return !dev.set_baud_rate(baudrate);
00032 }
00033 
00034 // close the serial port
00035 bool    LxFTDI::port_close()
00036 {
00037         return !dev.close();
00038 }
00039 
00040 int     LxFTDI::port_read(unsigned char* buffer, int numBytes)
00041 {
00042         int nBytesRead=0;
00043 
00044         while (nBytesRead < numBytes)
00045         {
00046                 int n = dev.read(buffer+nBytesRead, numBytes);
00047                 if (n < 0)
00048                         return n;
00049                 else
00050                         nBytesRead += n;
00051         }
00052 
00053         return nBytesRead;
00054 }
00055 
00056 int     LxFTDI::port_read(unsigned char* buffer, int numBytes, int seconds, int microseconds)
00057 {
00058         struct timeval timeout, now;
00059         gettimeofday(&timeout, NULL);
00060         timeout.tv_sec += seconds;
00061         timeout.tv_usec += microseconds;
00062         if (timeout.tv_usec > 1000000)
00063         {
00064                 timeout.tv_sec += 1;
00065                 timeout.tv_usec -= 1000000;
00066         }
00067 
00068         int nBytesRead=0;
00069         do
00070         {
00071                 int n = dev.read(buffer+nBytesRead, numBytes-nBytesRead);
00072                 if (n < 0)
00073                         return n;
00074                 else
00075                         nBytesRead += n;
00076                         
00077                 
00078                 gettimeofday(&now, NULL);
00079         } while (nBytesRead < numBytes &&
00080                  (now.tv_sec < timeout.tv_sec || (now.tv_sec == timeout.tv_sec && now.tv_usec < timeout.tv_usec)));
00081 
00082         return nBytesRead;
00083 }
00084 
00085 int     LxFTDI::port_write(unsigned char* buffer, int numBytes)
00086 {
00087         return dev.write(buffer, numBytes);
00088 }
00089 
00090 void LxFTDI::flush_buffer()
00091 {
00092         dev.flush(Ftdi::Context::Input | Ftdi::Context::Output);
00093 }
00094 
00095 LxFTDI::~LxFTDI()
00096 {
00097         dev.close();
00098 }


threemxl
Author(s):
autogenerated on Fri Aug 28 2015 13:21:08