SerialPort.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /**
  2. * @file SerialPort.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 a lightweight library of serial port, which can easy to read and write serical port on windows and linux with
  8. * C++ 轻量级跨平台串口读写类库
  9. * @version 4.1.0.201010
  10. * @date 2020-10-10
  11. * @copyright The CSerialPort is Copyright (C) 2021 itas109. \n
  12. * Contact: itas109@qq.com \n\n
  13. * You may use, distribute and copy the CSerialPort under the terms of \n
  14. * GNU Lesser General Public License version 3, which is displayed below.
  15. */
  16. #ifndef __CSERIALPORT_H__
  17. #define __CSERIALPORT_H__
  18. #include "SerialPort_global.h"
  19. #include <string>
  20. #include "sigslot.h"
  21. using namespace sigslot;
  22. #include "CSerialPort/SerialPortUnixBase.h"
  23. #define CSERIALPORTBASE CSerialPortUnixBase
  24. #include <iostream>
  25. class CSerialPortBase;
  26. namespace itas109
  27. {
  28. /**
  29. * @brief the CSerialPort class 串口类库
  30. * @see reference 引用 CSerialPortBase
  31. */
  32. class DLL_EXPORT CSerialPort : public has_slots<>
  33. {
  34. public:
  35. /**
  36. * @brief Construct a new CSerialPort object 构造函数
  37. *
  38. */
  39. CSerialPort(){
  40. p_serialPortBase = new CSERIALPORTBASE();
  41. p_serialPortBase->setMinByteReadNotify(1);
  42. ((CSERIALPORTBASE *)p_serialPortBase)->readReady.connect(this, &CSerialPort::onReadReady);
  43. }
  44. /**
  45. * @brief Construct a new CSerialPort object 通过串口名称构造函数
  46. *
  47. * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
  48. */
  49. CSerialPort(const std::string &portName){
  50. p_serialPortBase = new CSERIALPORTBASE(portName);
  51. p_serialPortBase->setMinByteReadNotify(1);
  52. ((CSERIALPORTBASE *)p_serialPortBase)->readReady.connect(this, &CSerialPort::onReadReady);
  53. }
  54. /**
  55. * @brief Destroy the CSerialPort object 析构函数
  56. *
  57. */
  58. ~CSerialPort(){
  59. ((CSERIALPORTBASE *)p_serialPortBase)->readReady.disconnect_all();
  60. if (p_serialPortBase)
  61. {
  62. delete p_serialPortBase;
  63. p_serialPortBase = NULL;
  64. }
  65. }
  66. /**
  67. * @brief init 初始化函数
  68. *
  69. * @param portName [in] the port name串口名称 Windows:COM1 Linux:/dev/ttyS0
  70. * @param baudRate [in] the baudRate 波特率
  71. * @param parity [in] the parity 校验位
  72. * @param dataBits [in] the dataBits 数据位
  73. * @param stopbits [in] the stopbits 停止位
  74. * @param flowControl [in] flowControl type 流控制
  75. * @param readBufferSize [in] the read buffer size 读取缓冲区大小
  76. */
  77. void init(std::string portName,
  78. int baudRate = itas109::BaudRate115200,
  79. itas109::Parity parity = itas109::ParityNone,
  80. itas109::DataBits dataBits = itas109::DataBits8,
  81. itas109::StopBits stopbits = itas109::StopOne,
  82. itas109::FlowControl flowControl = itas109::FlowNone,
  83. int64_t readBufferSize = 512){
  84. if (p_serialPortBase)
  85. {
  86. p_serialPortBase->init(portName, baudRate, parity, dataBits, stopbits, flowControl, readBufferSize);
  87. }
  88. }
  89. /**
  90. * @brief Set the Operate Mode object 设置串口操作模式
  91. *
  92. * @param operateMode [in] the operate mode 串口操作模式 {@link itas109::OperateMode}
  93. */
  94. void setOperateMode(itas109::OperateMode operateMode = itas109::AsynchronousOperate){
  95. if (p_serialPortBase)
  96. {
  97. p_serialPortBase->setOperateMode(operateMode);
  98. }
  99. }
  100. /**
  101. * @brief open serial port 打开串口
  102. *
  103. * @return
  104. * @retval true open success 打开成功
  105. * @retval false open failed 打开失败
  106. */
  107. bool open(){
  108. if (p_serialPortBase)
  109. {
  110. return p_serialPortBase->openPort();
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
  117. /**
  118. * @brief close 关闭串口
  119. *
  120. */
  121. void close(){
  122. if (p_serialPortBase)
  123. {
  124. p_serialPortBase->closePort();
  125. }
  126. }
  127. /**
  128. * @brief if serial port is open success 串口是否打开成功
  129. *
  130. * @return
  131. * @retval true serial port open success 串口打开成功
  132. * @retval false serial port open failed 串口打开失败
  133. */
  134. bool isOpened(){
  135. if (p_serialPortBase)
  136. {
  137. return p_serialPortBase->isOpened();
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. }
  144. /**
  145. * @brief read specified length data 读取指定长度数据
  146. *
  147. * @param data [out] read data result 读取结果
  148. * @param maxSize [in] read length 读取长度
  149. * @return return number Of bytes read 返回读取字节数
  150. * @retval -1 read error 读取错误
  151. * @retval [other] return number Of bytes read 返回读取字节数
  152. */
  153. int readData(char *data, int maxSize){
  154. if (p_serialPortBase)
  155. {
  156. return p_serialPortBase->readData(data, maxSize);
  157. }
  158. else
  159. {
  160. return -1;
  161. }
  162. }
  163. /**
  164. * @brief read all data 读取所有数据
  165. *
  166. * @param data [out] read data result 读取结果
  167. * @return return number Of bytes read 返回读取字节数
  168. * @retval -1 read error 读取错误
  169. * @retval [other] return number Of bytes read 返回读取字节数
  170. */
  171. int readAllData(char *data){
  172. if (p_serialPortBase)
  173. {
  174. return p_serialPortBase->readAllData(data);
  175. }
  176. else
  177. {
  178. return -1;
  179. }
  180. }
  181. /**
  182. * @brief read line data 读取一行字符串
  183. * @todo Not implemented 未实现
  184. *
  185. * @param data
  186. * @param maxSize
  187. * @return int
  188. */
  189. int readLineData(char *data, int maxSize){
  190. if (p_serialPortBase)
  191. {
  192. return p_serialPortBase->readLineData(data, maxSize);
  193. }
  194. else
  195. {
  196. return -1;
  197. }
  198. }
  199. /**
  200. * @brief write specified lenfth data 写入指定长度数据
  201. *
  202. * @param data [in] write data 待写入数据
  203. * @param maxSize [in] wtite length 写入长度
  204. * @return return number Of bytes write 返回写入字节数
  205. * @retval -1 read error 写入错误
  206. * @retval [other] return number Of bytes write 返回写入字节数
  207. */
  208. int writeData(const char *data, int maxSize){
  209. if (p_serialPortBase)
  210. {
  211. return p_serialPortBase->writeData(data, maxSize);
  212. }
  213. else
  214. {
  215. return -1;
  216. }
  217. }
  218. /**
  219. * @brief Set Debug Model 设置调试模式
  220. * @details output serial port read and write details info 输出串口读写的详细信息
  221. * @todo Not implemented 未实现
  222. *
  223. * @param isDebug true if enable true为启用
  224. */
  225. void setDebugModel(bool isDebug){
  226. if (p_serialPortBase)
  227. {
  228. p_serialPortBase->setDebugModel(isDebug);
  229. }
  230. }
  231. /**
  232. * @brief Set the Read Time Interval object
  233. * @details use timer import effectiveness 使用定时器提高效率
  234. * @todo Not implemented 未实现
  235. *
  236. * @param msecs read time micro second 读取间隔时间,单位:毫秒
  237. */
  238. void setReadTimeInterval(int msecs){
  239. if (p_serialPortBase)
  240. {
  241. p_serialPortBase->setReadTimeInterval(msecs);
  242. }
  243. }
  244. /**
  245. * @brief setMinByteReadNotify set minimum byte of read notify 设置读取通知触发最小字节数
  246. * @param minByteReadNotify minimum byte of read notify 读取通知触发最小字节数
  247. */
  248. void setMinByteReadNotify(unsigned int minByteReadNotify = 2){
  249. if (p_serialPortBase)
  250. {
  251. p_serialPortBase->setMinByteReadNotify(minByteReadNotify);
  252. }
  253. }
  254. /**
  255. * @brief Get the Last Error object 获取最后的错误代码
  256. *
  257. * @return return last error code, refrence {@link itas109::SerialPortError} 错误代码
  258. */
  259. int getLastError() const{
  260. if (p_serialPortBase)
  261. {
  262. return p_serialPortBase->getLastError();
  263. }
  264. else
  265. {
  266. // null error
  267. return itas109::/*SerialPortError::*/ SystemError;
  268. }
  269. }
  270. /**
  271. * @brief clear error 清除错误信息
  272. *
  273. */
  274. void clearError(){
  275. if (p_serialPortBase)
  276. {
  277. p_serialPortBase->clearError();
  278. }
  279. }
  280. /**
  281. * @brief Set the Port Name object 设置串口名称
  282. *
  283. * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
  284. */
  285. void setPortName(std::string portName){
  286. if (p_serialPortBase)
  287. {
  288. p_serialPortBase->setPortName(portName);
  289. }
  290. }
  291. /**
  292. * @brief Get the Port Name object 获取串口名称
  293. *
  294. * @return return port name 返回串口名称
  295. */
  296. std::string getPortName() const{
  297. if (p_serialPortBase)
  298. {
  299. p_serialPortBase->getPortName();
  300. }
  301. }
  302. /**
  303. * @brief Set the Baud Rate object 设置波特率
  304. *
  305. * @param baudRate [in] the baudRate 波特率
  306. */
  307. void setBaudRate(int baudRate){
  308. if (p_serialPortBase)
  309. {
  310. p_serialPortBase->setBaudRate(baudRate);
  311. }
  312. }
  313. /**
  314. * @brief Get the Baud Rate object 获取波特率
  315. *
  316. * @return return baudRate 返回波特率
  317. */
  318. int getBaudRate() const{
  319. if (p_serialPortBase)
  320. {
  321. return p_serialPortBase->getBaudRate();
  322. }
  323. else
  324. {
  325. return -1;
  326. }
  327. }
  328. /**
  329. * @brief Set the Parity object 设置校验位
  330. *
  331. * @param parity [in] the parity 校验位 {@link itas109::Parity}
  332. */
  333. void setParity(itas109::Parity parity){
  334. if (p_serialPortBase)
  335. {
  336. p_serialPortBase->setParity(parity);
  337. }
  338. }
  339. /**
  340. * @brief Get the Parity object 获取校验位
  341. *
  342. * @return return parity 返回校验位 {@link itas109::Parity}
  343. */
  344. itas109::Parity getParity() const{
  345. if (p_serialPortBase)
  346. {
  347. return p_serialPortBase->getParity();
  348. }
  349. else
  350. {
  351. // should retrun error
  352. return itas109::/*Parity::*/ ParityNone;
  353. }
  354. }
  355. /**
  356. * @brief Set the Data Bits object 设置数据位
  357. *
  358. * @param dataBits [in] the dataBits 数据位 {@link itas109::DataBits}
  359. */
  360. void setDataBits(itas109::DataBits dataBits){
  361. if (p_serialPortBase)
  362. {
  363. p_serialPortBase->setDataBits(dataBits);
  364. }
  365. }
  366. /**
  367. * @brief Get the Data Bits object 获取数据位
  368. *
  369. * @return return dataBits 返回数据位 {@link itas109::DataBits}
  370. */
  371. itas109::DataBits getDataBits() const{
  372. if (p_serialPortBase)
  373. {
  374. return p_serialPortBase->getDataBits();
  375. }
  376. else
  377. {
  378. // should retrun error
  379. return itas109::/*DataBits::*/ DataBits8;
  380. }
  381. }
  382. /**
  383. * @brief Set the Stop Bits object 设置停止位
  384. *
  385. * @param stopbits [in] the stopbits 停止位 {@link itas109::StopBits}
  386. */
  387. void setStopBits(itas109::StopBits stopbits){
  388. if (p_serialPortBase)
  389. {
  390. p_serialPortBase->setStopBits(stopbits);
  391. }
  392. }
  393. /**
  394. * @brief Get the Stop Bits object 获取停止位
  395. *
  396. * @return return stopbits 返回停止位 {@link itas109::StopBits}
  397. */
  398. itas109::StopBits getStopBits() const{
  399. if (p_serialPortBase)
  400. {
  401. return p_serialPortBase->getStopBits();
  402. }
  403. else
  404. {
  405. // should retrun error
  406. return itas109::/*StopBits::*/ StopOne;
  407. }
  408. }
  409. /**
  410. * @brief Set the Flow Control object 设置流控制
  411. * @todo Not implemented 未实现
  412. *
  413. * @param flowControl [in]
  414. */
  415. void setFlowControl(itas109::FlowControl flowControl){
  416. if (p_serialPortBase)
  417. {
  418. p_serialPortBase->setFlowControl(flowControl);
  419. }
  420. }
  421. /**
  422. * @brief Get the Flow Control object 获取流控制
  423. * @todo Not implemented 未实现
  424. *
  425. * @return itas109::FlowControl
  426. */
  427. itas109::FlowControl getFlowControl() const{
  428. if (p_serialPortBase)
  429. {
  430. return p_serialPortBase->getFlowControl();
  431. }
  432. else
  433. {
  434. // should retrun error
  435. return itas109::/*FlowControl::*/ FlowNone;
  436. }
  437. }
  438. /**
  439. * @brief Set the Read Buffer Size object
  440. *
  441. * @param size [in] read buffer size 设置缓冲区大小
  442. */
  443. void setReadBufferSize(int64_t size){
  444. if (p_serialPortBase)
  445. {
  446. p_serialPortBase->setReadBufferSize(size);
  447. }
  448. }
  449. /**
  450. * @brief Get the Read Buffer Size object 获取读取缓冲区大小
  451. *
  452. * @return return read buffer size 返回读取缓冲区大小
  453. */
  454. int64_t getReadBufferSize() const{
  455. if (p_serialPortBase)
  456. {
  457. return p_serialPortBase->getReadBufferSize();
  458. }
  459. else
  460. {
  461. return -1;
  462. }
  463. }
  464. /**
  465. * @brief Set the Dtr object 设置DTR
  466. * @todo Not implemented 未实现
  467. *
  468. * @param set [in]
  469. */
  470. void setDtr(bool set = true){
  471. if (p_serialPortBase)
  472. {
  473. p_serialPortBase->setDtr(set);
  474. }
  475. }
  476. /**
  477. * @brief Set the Rts object 设置RTS
  478. * @todo Not implemented 未实现
  479. *
  480. * @param set [in]
  481. */
  482. void setRts(bool set = true){
  483. if (p_serialPortBase)
  484. {
  485. p_serialPortBase->setRts(set);
  486. }
  487. }
  488. /**
  489. * @brief Get the Version object 获取版本信息
  490. *
  491. * @return return version 返回版本信息
  492. */
  493. std::string getVersion(){
  494. return "https://github.com/itas109/CSerialPort - V4.1.0.201010";
  495. }
  496. public:
  497. void onReadReady(){
  498. readReady._emit();
  499. }
  500. sigslot::signal0<> readReady; // sigslot
  501. private:
  502. CSerialPortBase *p_serialPortBase;
  503. };
  504. } // namespace itas109
  505. #endif //__CSERIALPORT_H__