Advertisement
Guest User

Lib Serial

a guest
Jun 9th, 2012
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. /**
  2. @Author : Stéphane Paton
  3. @Website : http://www.mangetamain.fr
  4. @Ref : http://www.mangetamain.fr/decouvrez-les-attaques-par-canaux-auxiliaires-sur-cryptoprocesseurs.html
  5.  
  6. Code d'exemple de la libserial en C++ pour récupérer la valeur analogique d'un
  7. port de sortie sur mon Arduino
  8. */
  9.  
  10. #include <iostream>
  11. #include <sstream>
  12. #include <string>
  13. #include <stdexcept>
  14. #include <SerialStream.h>
  15.  
  16. void getVoltage();
  17. inline double convertToDouble(std::string const& s);
  18.  
  19. int main(int argc,char** argv)
  20. {
  21.     getVoltage();
  22.     return 0;
  23. }
  24.  
  25. void getVoltage()
  26. {
  27.     // Open the serial port.
  28.     float voltage;
  29.     using namespace LibSerial ;
  30.     SerialStream serial_port ;
  31.     serial_port.Open("/dev/ttyACM3");
  32.     if ( ! serial_port.good() )
  33.     {
  34.         std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
  35.                   << "Error: Could not open serial port."
  36.                   << std::endl ;
  37.         exit(1) ;
  38.     }
  39.     // Set the baud rate of the serial port.
  40.     serial_port.SetBaudRate( SerialStreamBuf::BAUD_9600 ) ;
  41.     if ( ! serial_port.good() )
  42.     {
  43.         std::cerr << "Error: Could not set the baud rate." << std::endl ;
  44.         exit(1) ;
  45.     }
  46.     // Set the number of data bits.
  47.     serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
  48.     if ( ! serial_port.good() )
  49.     {
  50.         std::cerr << "Error: Could not set the character size." << std::endl ;
  51.         exit(1) ;
  52.     }
  53.     // Disable parity.
  54.     serial_port.SetParity( SerialStreamBuf::PARITY_NONE ) ;
  55.     if ( ! serial_port.good() )
  56.     {
  57.         std::cerr << "Error: Could not disable the parity." << std::endl ;
  58.         exit(1) ;
  59.     }
  60.     // Set the number of stop bits.
  61.     serial_port.SetNumOfStopBits( 1 ) ;
  62.     if ( ! serial_port.good() )
  63.     {
  64.         std::cerr << "Error: Could not set the number of stop bits."
  65.                   << std::endl ;
  66.         exit(1) ;
  67.     }
  68.     // Turn on hardware flow control.
  69.     serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_HARD ) ;
  70.     if ( ! serial_port.good() )
  71.     {
  72.         std::cerr << "Error: Could not use hardware flow control."
  73.                   << std::endl ;
  74.         exit(1) ;
  75.     }
  76.  
  77.     // Lecture
  78.     std::string buffer;
  79.     char next_byte;
  80.     while ( true )
  81.     {
  82.     // Tant qu'on ne reçoit pas un "\n" sur le port série,
  83.     // on concatène le buffer
  84.         serial_port.get(next_byte);
  85.         if(next_byte != '\n') buffer = buffer+next_byte;
  86.         else
  87.     {
  88.         // Quand on a reçu le \n de fin de ligne, on affiche le buffer
  89.         // transformé en float (la tension) et on le vide
  90.         voltage = convertToDouble(buffer);
  91.         std::cout<<voltage<<std::endl;
  92.         buffer.clear();
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98.  class BadConversion : public std::runtime_error {
  99.  public:
  100.    BadConversion(std::string const& s)
  101.      : std::runtime_error(s)
  102.      { }
  103.  };
  104.  
  105.  inline double convertToDouble(std::string const& s)
  106.  {
  107.    std::istringstream i(s);
  108.    double x;
  109.    if (!(i >> x))
  110.      throw BadConversion("convertToDouble(\"" + s + "\")");
  111.    return x;
  112.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement