user_uart.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // The MIT License (MIT)
  3. //
  4. // Copyright (c) 2019 Livox. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. #ifndef USER_UART_H_
  25. #define USER_UART_H_
  26. #include <stdint.h>
  27. #include <sys/fcntl.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <termios.h>
  31. namespace livox_ros {
  32. enum Parity {
  33. P_8N1, /* No parity (8N1) */
  34. P_7E1, /* Even parity (7E1)*/
  35. P_7O1, /* Odd parity (7O1) */
  36. P_7S1, /* Space parity is setup the same as no parity (7S1) */
  37. ParityUnkown
  38. };
  39. enum BaudRate {
  40. BR2400,
  41. BR4800,
  42. BR9600,
  43. BR19200,
  44. BR38400,
  45. BR57600,
  46. BR115200,
  47. BR230400,
  48. BR460800,
  49. BR500000,
  50. BR576000,
  51. BR921600,
  52. BR1152000,
  53. BR1500000,
  54. BR2000000,
  55. BR2500000,
  56. BR3000000,
  57. BR3500000,
  58. BR4000000,
  59. BRUnkown,
  60. };
  61. class UserUart {
  62. public:
  63. UserUart(uint8_t baudrate_index, uint8_t parity);
  64. ~UserUart();
  65. int Setup(uint8_t baudrate_index, uint8_t parity);
  66. ssize_t Write(const char *buffer, size_t size);
  67. ssize_t Read(char *buffer, size_t size);
  68. int Close();
  69. int Open(const char *filename);
  70. bool IsOpen() { return is_open_; };
  71. private:
  72. int fd_;
  73. volatile bool is_open_;
  74. uint8_t baudrate_;
  75. uint8_t parity_;
  76. };
  77. } // namespace livox_ros
  78. #endif