PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HardwareSerial.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // HardwareSerial.h
  2. // Author: Mike McCauley (mikem@airspayce.com)
  3. // Copyright (C) 2015 Mike McCauley
  4. // $Id: HardwareSerial.h,v 1.3 2015/08/13 02:45:47 mikem Exp mikem $
  5. #ifndef HardwareSerial_h
  6. #define HardwareSerial_h
  7. #include <stdio.h>
  8. /////////////////////////////////////////////////////////////////////
  9. /// \class HardwareSerial HardwareSerial.h <RHutil/HardwareSerial.h>
  10. /// \brief Encapsulates a Posix compliant serial port as a HarwareSerial
  11. ///
  12. /// This class provides access to a serial port on Unix and OSX.
  13. /// It is equivalent to HardwareSerial in Arduino, and can be used by RH_Serial
  14. /// We implement just enough to provide the services RadioHead needs.
  15. /// Additional methods not present on Arduino are also provided for waiting for characters.
  16. ///
  17. /// The device port is configured for 8 bits, no parity, 1 stop bit and full raw transparency, so it can be used
  18. /// to send and receive any 8 bit character. A limited range of baud rates is supported.
  19. ///
  20. /// \par Device Names
  21. ///
  22. /// Device naming conventions vary from OS to OS. ON linux, an FTDI serial port may have a name like
  23. /// /dev/ttyUSB0. On OSX, it might be something like /dev/tty.usbserial-A501YSWL
  24. /// \par errors
  25. ///
  26. /// A number of these methods print error messages to stderr in the event of an IO error.
  27. class HardwareSerial
  28. {
  29. public:
  30. /// Constructor
  31. // \param [in] deviceName Name of the derial port device to connect to
  32. HardwareSerial(const char* deviceName);
  33. /// Open and configure the port.
  34. /// The named port is opened, and the given baud rate is set.
  35. /// The port is configure for raw input and output and 8,N,1 protocol
  36. /// with no flow control.
  37. /// This must be called before any other operations are attempted.
  38. /// IO failures and unsupported baud rates will result in an error message on stderr.
  39. /// \param[in] baud The desired baud rate. The only rates supported are: 50, 75, 110, 134, 150
  40. /// 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400. On some platform
  41. /// such as Linux you may also use: 460800, 921600.
  42. void begin(int baud);
  43. /// Close the port.
  44. /// If begin() has previously been called successfully, the device port will be closed.
  45. /// It may be reopened again with another call to begin().
  46. void end();
  47. /// Flush remaining data.
  48. /// Blocks until any data yet to be transmtted is sent.
  49. void flush();
  50. /// Peek at the nex available character without consuming it.
  51. /// CAUTION: Not implemented.
  52. int peek(void);
  53. /// Returns the number of bytes immediately available to be read from the
  54. /// device.
  55. /// \return 0 if none available else the number of characters available for immediate reading
  56. int available();
  57. /// Read and return the next available character.
  58. /// If no character is available prints a message to stderr and returns 0;
  59. /// \return The next available character
  60. int read();
  61. /// Transmit a single character oin the serial port.
  62. /// Returns immediately.
  63. /// IO errors are repored by printing aa message to stderr.
  64. /// \param[in] ch The character to send. Anything in the range 0x00 to 0xff is permitted
  65. /// \return 1 if successful else 0
  66. size_t write(uint8_t ch);
  67. // These are not usually in HardwareSerial but we
  68. // need them in a Unix environment
  69. /// Wait until a character is available from the port.
  70. void waitAvailable();
  71. /// Wait until a a character is available from the port.
  72. /// or the timeout expires
  73. /// \param[in] timeout The maximum time to wait in milliseconds. 0 means wait forever.
  74. /// \return true if a message is available as reported by available()
  75. bool waitAvailableTimeout(uint16_t timeout);
  76. protected:
  77. bool openDevice();
  78. bool closeDevice();
  79. bool setBaud(int baud);
  80. private:
  81. const char* _deviceName;
  82. int _device; // file desriptor
  83. int _baud;
  84. };
  85. #endif