serial_com.hpp 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef SERIAL_H
  2. #define SERIAL_H
  3. #include <fcntl.h>
  4. #include <termio.h>
  5. #include <unistd.h>
  6. namespace CommunicationStuff {
  7. enum Status {
  8. OK = 0,
  9. DEV_NOT_FOUND = -1,
  10. BAUD_NOT_SUPPORT = -2,
  11. DATABITS_NOT_SUPPORT = -3,
  12. CONFIG_FAIL = -4,
  13. PARITYMODE_NOT_SUPPORT = -5,
  14. STOPBITS_NOT_SUPPORT = -6,
  15. };
  16. enum ParityMode {
  17. PARITY_NONE = 0,
  18. PARITY_ODD = 1,
  19. PARITY_EVEN = 2,
  20. };
  21. enum StopBits {
  22. ONE = 1,
  23. TWO = 2,
  24. };
  25. class Serial
  26. {
  27. public:
  28. Serial(const char* device);
  29. ~Serial();
  30. // Status open(const char* dev);
  31. Status setup(const int baud_speed, const int data_bits,
  32. const int parity_mode, const int stop_bits);
  33. Status change_baud(const int baud_speed);
  34. // Status close();
  35. int write(const unsigned char* buffer, int length);
  36. int read(unsigned char* buffer, int length);
  37. private:
  38. int fd_;
  39. struct termios options_;
  40. };
  41. } // namespace CommunicationStuff
  42. #endif