SerialPortBase.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * @file SerialPortBase.h
  3. * @author itas109 (itas109@qq.com) \n\n
  4. * Blog : https://blog.csdn.net/itas109 \n
  5. * Github : https://github.com/itas109 \n
  6. * QQ Group : 12951803
  7. * @brief the CSerialPort Base class 串口基类
  8. * @date 2020-04-29
  9. * @copyright The CSerialPort is Copyright (C) 2021 itas109. \n
  10. * Contact: itas109@qq.com \n\n
  11. * You may use, distribute and copy the CSerialPort under the terms of \n
  12. * GNU Lesser General Public License version 3, which is displayed below.
  13. */
  14. #ifndef __CSERIALPORTBASE_H__
  15. #define __CSERIALPORTBASE_H__
  16. #include "SerialPort_global.h"
  17. #include <string>
  18. /**
  19. * @brief the CSerialPort Base class 串口基类
  20. *
  21. */
  22. class CSerialPortBase
  23. {
  24. public:
  25. /**
  26. * @brief Construct a new CSerialPortBase object 构造函数
  27. *
  28. */
  29. CSerialPortBase(){
  30. lastError = 0;
  31. m_minByteReadNotify = 1;
  32. m_operateMode = itas109::AsynchronousOperate;
  33. }
  34. /**
  35. * @brief Construct a new CSerialPortBase object 通过串口名称构造函数
  36. *
  37. * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
  38. */
  39. CSerialPortBase(const std::string &portName){
  40. lastError = 0;
  41. m_minByteReadNotify = 1;
  42. m_operateMode = itas109::AsynchronousOperate;
  43. }
  44. /**
  45. * @brief Destroy the CSerialPortBase object 析构函数
  46. *
  47. */
  48. virtual ~CSerialPortBase(){};
  49. /**
  50. * @brief parameter init 参数初始化
  51. *
  52. */
  53. virtual void construct() = 0;
  54. /**
  55. * @brief init 初始化函数
  56. *
  57. * @param portName [in] the port name串口名称 Windows:COM1 Linux:/dev/ttyS0
  58. * @param baudRate [in] the baudRate 波特率
  59. * @param parity [in] the parity 校验位
  60. * @param dataBits [in] the dataBits 数据位
  61. * @param stopbits [in] the stopbits 停止位
  62. * @param flowControl [in] flowControl type 流控制
  63. * @param readBufferSize [in] the read buffer size 读取缓冲区大小
  64. */
  65. virtual void init(std::string portName,
  66. int baudRate,
  67. itas109::Parity parity,
  68. itas109::DataBits dataBits,
  69. itas109::StopBits stopbits,
  70. itas109::FlowControl flowControl,
  71. int64_t readBufferSize) = 0;
  72. /**
  73. * @brief Set the Operate Mode object 设置串口操作模式
  74. *
  75. * @param operateMode [in] the operate mode 串口操作模式 {@link itas109::OperateMode}
  76. */
  77. virtual void setOperateMode(itas109::OperateMode operateMode){
  78. m_operateMode = operateMode;
  79. }
  80. /**
  81. * @brief open serial port 打开串口
  82. *
  83. * @return
  84. * @retval true open success 打开成功
  85. * @retval false open failed 打开失败
  86. */
  87. virtual bool openPort() = 0;
  88. /**
  89. * @brief close 关闭串口
  90. *
  91. */
  92. virtual void closePort() = 0;
  93. /**
  94. * @brief if serial port is open success 串口是否打开成功
  95. *
  96. * @return
  97. * @retval true serial port open success 串口打开成功
  98. * @retval false serial port open failed 串口打开失败
  99. */
  100. virtual bool isOpened() = 0;
  101. /**
  102. * @brief read specified length data 读取指定长度数据
  103. *
  104. * @param data [out] read data result 读取结果
  105. * @param maxSize [in] read length 读取长度
  106. * @return return number Of bytes read 返回读取字节数
  107. * @retval -1 read error 读取错误
  108. * @retval [other] return number Of bytes read 返回读取字节数
  109. */
  110. virtual int readData(char *data, int maxSize) = 0;
  111. /**
  112. * @brief read all data 读取所有数据
  113. *
  114. * @param data [out] read data result 读取结果
  115. * @return return number Of bytes read 返回读取字节数
  116. * @retval -1 read error 读取错误
  117. * @retval [other] return number Of bytes read 返回读取字节数
  118. */
  119. virtual int readAllData(char *data) = 0;
  120. /**
  121. * @brief read line data 读取一行字符串
  122. * @todo Not implemented 未实现
  123. *
  124. * @param data
  125. * @param maxSize
  126. * @return int
  127. */
  128. virtual int readLineData(char *data, int maxSize) = 0;
  129. /**
  130. * @brief write specified lenfth data 写入指定长度数据
  131. *
  132. * @param data [in] write data 待写入数据
  133. * @param maxSize [in] wtite length 写入长度
  134. * @return return number Of bytes write 返回写入字节数
  135. * @retval -1 read error 写入错误
  136. * @retval [other] return number Of bytes write 返回写入字节数
  137. */
  138. virtual int writeData(const char *data, int maxSize) = 0;
  139. /**
  140. * @brief Set Debug Model 设置调试模式
  141. * @details output serial port read and write details info 输出串口读写的详细信息
  142. *
  143. * @param isDebug true if enable true为启用
  144. */
  145. virtual void setDebugModel(bool isDebug) = 0;
  146. /**
  147. * @brief Set the Read Time Interval object
  148. * @details use timer import effectiveness 使用定时器提高效率
  149. *
  150. * @param msecs read time micro second 读取间隔时间,单位:毫秒
  151. */
  152. virtual void setReadTimeInterval(int msecs) = 0;
  153. /**
  154. * @brief setMinByteReadNotify set minimum byte of read notify 设置读取通知触发最小字节数
  155. * @param minByteReadNotify minimum byte of read notify 读取通知触发最小字节数
  156. */
  157. virtual void setMinByteReadNotify(unsigned int minByteReadNotify) = 0;
  158. /**
  159. * @brief getMinByteReadNotify get minimum byte of read notify 获取读取通知触发最小字节数
  160. * @return minimum byte of read notify 读取通知触发最小字节数
  161. */
  162. virtual unsigned int getMinByteReadNotify(){
  163. return m_minByteReadNotify;
  164. }
  165. /**
  166. * @brief Get the Last Error object 获取最后的错误代码
  167. *
  168. * @return return last error code, refrence {@link itas109::SerialPortError} 错误代码
  169. */
  170. virtual int getLastError() const{
  171. return lastError;
  172. }
  173. /**
  174. * @brief clear error 清除错误信息
  175. *
  176. */
  177. virtual void clearError(){}
  178. /**
  179. * @brief Set the Port Name object 设置串口名称
  180. *
  181. * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
  182. */
  183. virtual void setPortName(std::string portName) = 0;
  184. /**
  185. * @brief Get the Port Name object 获取串口名称
  186. *
  187. * @return return port name 返回串口名称
  188. */
  189. virtual std::string getPortName() const = 0;
  190. /**
  191. * @brief Set the Baud Rate object 设置波特率
  192. *
  193. * @param baudRate [in] the baudRate 波特率
  194. */
  195. virtual void setBaudRate(int baudRate) = 0;
  196. /**
  197. * @brief Get the Baud Rate object 获取波特率
  198. *
  199. * @return return baudRate 返回波特率
  200. */
  201. virtual int getBaudRate() const = 0;
  202. /**
  203. * @brief Set the Parity object 设置校验位
  204. *
  205. * @param parity [in] the parity 校验位 {@link itas109::Parity}
  206. */
  207. virtual void setParity(itas109::Parity parity) = 0;
  208. /**
  209. * @brief Get the Parity object 获取校验位
  210. *
  211. * @return return parity 返回校验位 {@link itas109::Parity}
  212. */
  213. virtual itas109::Parity getParity() const = 0;
  214. /**
  215. * @brief Set the Data Bits object 设置数据位
  216. *
  217. * @param dataBits [in] the dataBits 数据位 {@link itas109::DataBits}
  218. */
  219. virtual void setDataBits(itas109::DataBits dataBits) = 0;
  220. /**
  221. * @brief Get the Data Bits object 获取数据位
  222. *
  223. * @return return dataBits 返回数据位 {@link itas109::DataBits}
  224. */
  225. virtual itas109::DataBits getDataBits() const = 0;
  226. /**
  227. * @brief Set the Stop Bits object 设置停止位
  228. *
  229. * @param stopbits [in] the stopbits 停止位 {@link itas109::StopBits}
  230. */
  231. virtual void setStopBits(itas109::StopBits stopbits) = 0;
  232. /**
  233. * @brief Get the Stop Bits object 获取停止位
  234. *
  235. * @return return stopbits 返回停止位 {@link itas109::StopBits}
  236. */
  237. virtual itas109::StopBits getStopBits() const = 0;
  238. /**
  239. * @brief Set the Flow Control object 设置流控制
  240. *
  241. * @param flowControl [in]
  242. */
  243. virtual void setFlowControl(itas109::FlowControl flowControl) = 0;
  244. /**
  245. * @brief Get the Flow Control object 获取流控制
  246. *
  247. * @return itas109::FlowControl
  248. */
  249. virtual itas109::FlowControl getFlowControl() const = 0;
  250. /**
  251. * @brief Set the Read Buffer Size object 设置读取缓冲区大小
  252. *
  253. * @param size [in] read buffer size 读取缓冲区大小
  254. */
  255. virtual void setReadBufferSize(int64_t size) = 0;
  256. /**
  257. * @brief Get the Read Buffer Size object 获取读取缓冲区大小
  258. *
  259. * @return return read buffer size 返回读取缓冲区大小
  260. */
  261. virtual int64_t getReadBufferSize() const = 0;
  262. /**
  263. * @brief Set the Dtr object 设置DTR
  264. *
  265. * @param set [in]
  266. */
  267. virtual void setDtr(bool set = true) = 0;
  268. /**
  269. * @brief Set the Rts object 设置RTS
  270. *
  271. * @param set [in]
  272. */
  273. virtual void setRts(bool set = true) = 0;
  274. /**
  275. * @brief Get the Version object 获取版本信息
  276. *
  277. * @return return version 返回版本信息
  278. */
  279. std::string getVersion();
  280. protected:
  281. /**
  282. * @brief lock 锁
  283. *
  284. */
  285. void lock();
  286. /**
  287. * @brief unlock 解锁
  288. *
  289. */
  290. void unlock();
  291. protected:
  292. int lastError; ///< last error code 最后的错误代码
  293. itas109::OperateMode m_operateMode; ///< operate mode 串口操作类型
  294. unsigned int m_minByteReadNotify; ///< minimum byte of read notify 读取通知触发最小字节数
  295. private:
  296. };
  297. #endif //__CSERIALPORTBASE_H__