SerialPortUnixBase.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /**
  2. * @file SerialPortUnixBase.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 unix Base class unix串口基类
  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 __CSERIALPORTUNIXBASE_H__
  15. #define __CSERIALPORTUNIXBASE_H__
  16. #include "SerialPortBase.h"
  17. #include <errno.h> /* Error number definitions */
  18. #include <fcntl.h> /* File control definitions */
  19. #include <stdio.h> /* Standard input/output definitions */
  20. #include <string.h> /* String function definitions */
  21. #include <termios.h> /* POSIX terminal control definitions */
  22. #include <unistd.h> /* UNIX standard function definitions */
  23. #include <sys/ioctl.h> //ioctl
  24. // Serial Programming Guide for POSIX Operating Systems
  25. // https://digilander.libero.it/robang/rubrica/serial.htm
  26. // https://blog.csdn.net/u014650722/article/details/51610587
  27. // sigslot
  28. // https://sourceforge.net/p/sigslot/bugs/8/
  29. #include "sigslot.h"
  30. // https://blog.csdn.net/u010317005/article/details/52384890
  31. // Linux compile 1
  32. // g++ SerialPort.cpp SerialPortBase.cpp SerialPortUnixBase.cpp -fPIC -lpthread -shared -o libsp.so
  33. // g++ t.cpp -o sp -L. -lsp
  34. // export LD_LIBRARY_PATH=./
  35. // ./sp
  36. // Linux compile 2
  37. // g++ sp.cpp SerialPort.cpp SerialPortBase.cpp SerialPortUnixBase.cpp -lpthread -o sp
  38. // ./sp
  39. // sp.cpp
  40. /*
  41. #include <iostream>
  42. #include "SerialPort.h"
  43. #include "sigslot.h"
  44. using namespace itas109;
  45. using namespace std;
  46. int main()
  47. {
  48. CSerialPort sp;
  49. sp.init("/dev/ttyS0");
  50. sp.open();
  51. cout << sp.isOpened() <<endl;
  52. while (1);
  53. return 0;
  54. }
  55. */
  56. /**
  57. * @brief the CSerialPort unix Base class unix串口基类
  58. * @see inherit 继承 CSerialPortBase
  59. *
  60. */
  61. class CSerialPortUnixBase : public CSerialPortBase
  62. {
  63. public:
  64. /**
  65. * @brief Construct a new CSerialPortUnixBase object 构造函数
  66. *
  67. */
  68. CSerialPortUnixBase(){
  69. construct();
  70. }
  71. /**
  72. * @brief Construct a new CSerialPortUnixBase object 通过串口名称构造函数
  73. *
  74. * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
  75. */
  76. CSerialPortUnixBase(const std::string &portName){
  77. construct();
  78. }
  79. /**
  80. * @brief Destroy the CSerialPortUnixBase object 析构函数
  81. *
  82. */
  83. virtual ~CSerialPortUnixBase(){
  84. pthread_mutex_destroy(&m_communicationMutex);
  85. }
  86. /**
  87. * @brief parameter init 参数初始化
  88. *
  89. */
  90. virtual void construct(){
  91. fd = -1;
  92. m_baudRate = itas109::BaudRate9600;
  93. m_parity = itas109::ParityNone;
  94. m_dataBits = itas109::DataBits8;
  95. m_stopbits = itas109::StopOne;
  96. m_flowControl = itas109::FlowNone;
  97. m_readBufferSize = 512;
  98. m_isThreadRunning = false;
  99. m_operateMode = itas109::AsynchronousOperate;
  100. pthread_mutex_init(&m_communicationMutex, NULL);
  101. }
  102. /**
  103. * @brief init 初始化函数
  104. *
  105. * @param portName [in] the port name串口名称 Windows:COM1 Linux:/dev/ttyS0
  106. * @param baudRate [in] the baudRate 波特率
  107. * @param parity [in] the parity 校验位
  108. * @param dataBits [in] the dataBits 数据位
  109. * @param stopbits [in] the stopbits 停止位
  110. * @param flowControl [in] flowControl type 流控制
  111. * @param readBufferSize [in] the read buffer size 读取缓冲区大小
  112. */
  113. virtual void init(std::string portName,
  114. int baudRate = itas109::BaudRate9600,
  115. itas109::Parity parity = itas109::ParityNone,
  116. itas109::DataBits dataBits = itas109::DataBits8,
  117. itas109::StopBits stopbits = itas109::StopOne,
  118. itas109::FlowControl flowControl = itas109::FlowNone,
  119. int64_t readBufferSize = 512){
  120. m_portName = portName; // portName;//串口 /dev/ttySn, USB /dev/ttyUSBn
  121. m_baudRate = baudRate;
  122. m_parity = parity;
  123. m_dataBits = dataBits;
  124. m_stopbits = stopbits;
  125. m_flowControl = flowControl;
  126. m_readBufferSize = readBufferSize;
  127. }
  128. /**
  129. * @brief open serial port 打开串口
  130. *
  131. * @return
  132. * @retval true open success 打开成功
  133. * @retval false open failed 打开失败
  134. */
  135. virtual bool openPort(){
  136. bool bRet = false;
  137. lock();
  138. // fd = open(m_portName.c_str(),O_RDWR | O_NOCTTY);//阻塞
  139. fd = open(m_portName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); //非阻塞
  140. if (fd != -1)
  141. {
  142. // if(fcntl(fd,F_SETFL,FNDELAY) >= 0)//非阻塞,覆盖前面open的属性
  143. if (fcntl(fd, F_SETFL, 0) >= 0) // 阻塞,即使前面在open串口设备时设置的是非阻塞的,这里设为阻塞后,以此为准
  144. {
  145. // set param
  146. if (uart_set(fd, m_baudRate, m_parity, m_dataBits, m_stopbits, m_flowControl) == -1)
  147. {
  148. fprintf(stderr, "uart set failed!\n");
  149. // exit(EXIT_FAILURE);
  150. bRet = false;
  151. lastError = itas109::/*SerialPortError::*/ InvalidParameterError;
  152. }
  153. else
  154. {
  155. m_isThreadRunning = true;
  156. bRet = startThreadMonitor();
  157. if (!bRet)
  158. {
  159. m_isThreadRunning = false;
  160. lastError = itas109::/*SerialPortError::*/ SystemError;
  161. }
  162. }
  163. }
  164. else
  165. {
  166. bRet = false;
  167. lastError = itas109::/*SerialPortError::*/ SystemError;
  168. }
  169. }
  170. else
  171. {
  172. // Could not open the port
  173. char str[256];
  174. snprintf(str, sizeof(str), "open port error: Unable to open %s", m_portName.c_str());
  175. perror(str);
  176. bRet = false;
  177. lastError = itas109::/*SerialPortError::*/ OpenError;
  178. }
  179. if (!bRet)
  180. {
  181. closePort();
  182. }
  183. unlock();
  184. return bRet;
  185. }
  186. /**
  187. * @brief close 关闭串口
  188. *
  189. */
  190. virtual void closePort(){
  191. if (isOpened())
  192. {
  193. stopThreadMonitor();
  194. close(fd);
  195. fd = -1;
  196. }
  197. }
  198. /**
  199. * @brief if serial port is open success 串口是否打开成功
  200. *
  201. * @return
  202. * @retval true serial port open success 串口打开成功
  203. * @retval false serial port open failed 串口打开失败
  204. */
  205. virtual bool isOpened(){
  206. return fd != -1;
  207. }
  208. /**
  209. * @brief read specified length data 读取指定长度数据
  210. *
  211. * @param data [out] read data result 读取结果
  212. * @param maxSize [in] read length 读取长度
  213. * @return return number Of bytes read 返回读取字节数
  214. * @retval -1 read error 读取错误
  215. * @retval [other] return number Of bytes read 返回读取字节数
  216. */
  217. virtual int readData(char *data, int maxSize){
  218. int iRet = -1;
  219. lock();
  220. if (isOpened())
  221. {
  222. iRet = read(fd, data, maxSize);
  223. }
  224. else
  225. {
  226. lastError = itas109::/*SerialPortError::*/ NotOpenError;
  227. iRet = -1;
  228. }
  229. unlock();
  230. return iRet;
  231. }
  232. /**
  233. * @brief read all data 读取所有数据
  234. *
  235. * @param data [out] read data result 读取结果
  236. * @return return number Of bytes read 返回读取字节数
  237. * @retval -1 read error 读取错误
  238. * @retval [other] return number Of bytes read 返回读取字节数
  239. */
  240. virtual int readAllData(char *data){
  241. int readbytes = 0;
  242. // read前获取可读的字节数,不区分阻塞和非阻塞
  243. ioctl(fd, FIONREAD, &readbytes);
  244. return readData(data, readbytes);
  245. }
  246. /**
  247. * @brief read line data 读取一行字符串
  248. * @todo Not implemented 未实现
  249. *
  250. * @param data
  251. * @param maxSize
  252. * @return int
  253. */
  254. virtual int readLineData(char *data, int maxSize){
  255. int iRet = -1;
  256. lock();
  257. if (isOpened())
  258. {
  259. }
  260. else
  261. {
  262. lastError = itas109::/*SerialPortError::*/ NotOpenError;
  263. iRet = -1;
  264. }
  265. unlock();
  266. return iRet;
  267. }
  268. /**
  269. * @brief write specified lenfth data 写入指定长度数据
  270. *
  271. * @param data [in] write data 待写入数据
  272. * @param maxSize [in] wtite length 写入长度
  273. * @return return number Of bytes write 返回写入字节数
  274. * @retval -1 read error 写入错误
  275. * @retval [other] return number Of bytes write 返回写入字节数
  276. */
  277. virtual int writeData(const char *data, int maxSize){
  278. int iRet = -1;
  279. // lock();
  280. if (isOpened())
  281. {
  282. // Write N bytes of BUF to FD. Return the number written, or -1
  283. iRet = write(fd, data, maxSize);
  284. }
  285. else
  286. {
  287. lastError = itas109::/*SerialPortError::*/ NotOpenError;
  288. iRet = -1;
  289. }
  290. // unlock();
  291. return iRet;
  292. }
  293. /**
  294. * @brief Set Debug Model 设置调试模式
  295. * @details output serial port read and write details info 输出串口读写的详细信息
  296. * @todo Not implemented 未实现
  297. *
  298. * @param isDebug true if enable true为启用
  299. */
  300. virtual void setDebugModel(bool isDebug){
  301. }
  302. /**
  303. * @brief Set the Read Time Interval object
  304. * @details use timer import effectiveness 使用定时器提高效率
  305. * @todo Not implemented 未实现
  306. *
  307. * @param msecs read time micro second 读取间隔时间,单位:毫秒
  308. */
  309. virtual void setReadTimeInterval(int msecs){
  310. }
  311. /**
  312. * @brief setMinByteReadNotify set minimum byte of read notify 设置读取通知触发最小字节数
  313. * @param minByteReadNotify minimum byte of read notify 读取通知触发最小字节数
  314. */
  315. virtual void setMinByteReadNotify(unsigned int minByteReadNotify = 2){
  316. m_minByteReadNotify = minByteReadNotify;
  317. }
  318. /**
  319. * @brief Get the Last Error object 获取最后的错误代码
  320. *
  321. * @return return last error code, refrence {@link itas109::SerialPortError} 错误代码
  322. */
  323. virtual int getLastError() const{
  324. return lastError;
  325. }
  326. /**
  327. * @brief clear error 清除错误信息
  328. *
  329. */
  330. virtual void clearError(){
  331. lastError = itas109::NoError;
  332. }
  333. /**
  334. * @brief Set the Port Name object 设置串口名称
  335. *
  336. * @param portName [in] the port name 串口名称 Windows:COM1 Linux:/dev/ttyS0
  337. */
  338. virtual void setPortName(std::string portName){
  339. m_portName = portName;
  340. }
  341. /**
  342. * @brief Get the Port Name object 获取串口名称
  343. *
  344. * @return return port name 返回串口名称
  345. */
  346. virtual std::string getPortName() const{
  347. return m_portName;
  348. }
  349. /**
  350. * @brief Set the Baud Rate object 设置波特率
  351. *
  352. * @param baudRate [in] the baudRate 波特率
  353. */
  354. virtual void setBaudRate(int baudRate){
  355. m_baudRate = baudRate;
  356. }
  357. /**
  358. * @brief Get the Baud Rate object 获取波特率
  359. *
  360. * @return return baudRate 返回波特率
  361. */
  362. virtual int getBaudRate() const{
  363. return m_baudRate;
  364. }
  365. /**
  366. * @brief Set the Parity object 设置校验位
  367. *
  368. * @param parity [in] the parity 校验位 {@link itas109::Parity}
  369. */
  370. virtual void setParity(itas109::Parity parity){
  371. m_parity = parity;
  372. }
  373. /**
  374. * @brief Get the Parity object 获取校验位
  375. *
  376. * @return return parity 返回校验位 {@link itas109::Parity}
  377. */
  378. virtual itas109::Parity getParity() const{
  379. return m_parity;
  380. }
  381. /**
  382. * @brief Set the Data Bits object 设置数据位
  383. *
  384. * @param dataBits [in] the dataBits 数据位 {@link itas109::DataBits}
  385. */
  386. virtual void setDataBits(itas109::DataBits dataBits){
  387. m_dataBits = dataBits;
  388. }
  389. /**
  390. * @brief Get the Data Bits object 获取数据位
  391. *
  392. * @return return dataBits 返回数据位 {@link itas109::DataBits}
  393. */
  394. virtual itas109::DataBits getDataBits() const{
  395. return m_dataBits;
  396. }
  397. /**
  398. * @brief Set the Stop Bits object 设置停止位
  399. *
  400. * @param stopbits [in] the stopbits 停止位 {@link itas109::StopBits}
  401. */
  402. virtual void setStopBits(itas109::StopBits stopbits){
  403. m_stopbits = stopbits;
  404. }
  405. /**
  406. * @brief Get the Stop Bits object 获取停止位
  407. *
  408. * @return return stopbits 返回停止位 {@link itas109::StopBits}
  409. */
  410. virtual itas109::StopBits getStopBits() const{
  411. return m_stopbits;
  412. }
  413. /**
  414. * @brief Set the Flow Control object 设置流控制
  415. * @todo Not implemented 未实现
  416. *
  417. * @param flowControl [in]
  418. */
  419. virtual void setFlowControl(itas109::FlowControl flowControl){
  420. m_flowControl = flowControl;
  421. }
  422. /**
  423. * @brief Get the Flow Control object 获取流控制
  424. * @todo Not implemented 未实现
  425. *
  426. * @return itas109::FlowControl
  427. */
  428. virtual itas109::FlowControl getFlowControl() const{
  429. return m_flowControl;
  430. }
  431. /**
  432. * @brief Set the Read Buffer Size object 设置读取缓冲区大小
  433. *
  434. * @param size [in] read buffer size 读取缓冲区大小
  435. */
  436. virtual void setReadBufferSize(int64_t size){
  437. m_readBufferSize = size;
  438. }
  439. /**
  440. * @brief Get the Read Buffer Size object 获取读取缓冲区大小
  441. *
  442. * @return return read buffer size 返回读取缓冲区大小
  443. */
  444. virtual int64_t getReadBufferSize() const{
  445. return m_readBufferSize;
  446. }
  447. /**
  448. * @brief Set the Dtr object 设置DTR
  449. * @todo Not implemented 未实现
  450. *
  451. * @param set [in]
  452. */
  453. virtual void setDtr(bool set = true){}
  454. /**
  455. * @brief Set the Rts object 设置RTS
  456. * @todo Not implemented 未实现
  457. *
  458. * @param set [in]
  459. */
  460. virtual void setRts(bool set = true){}
  461. /**
  462. * @brief Get the Version object 获取版本信息
  463. *
  464. * @return return version 返回版本信息
  465. */
  466. std::string getVersion(){
  467. std::string m_version = "CSerialPortUnixBase V1.0.1.190728";
  468. return m_version;
  469. }
  470. public:
  471. /**
  472. * @brief isThreadRunning 是否启动多线程
  473. * @return
  474. * @retval true thread running 多线程已启动
  475. * @retval false thread not running 多线程未启动
  476. */
  477. bool isThreadRunning(){
  478. return m_isThreadRunning;
  479. }
  480. private:
  481. /**
  482. * @brief lock 锁
  483. *
  484. */
  485. void lock(){
  486. pthread_mutex_lock(&m_communicationMutex);
  487. }
  488. /**
  489. * @brief unlock 解锁
  490. *
  491. */
  492. void unlock(){
  493. pthread_mutex_unlock(&m_communicationMutex);
  494. }
  495. /**
  496. * @brief rate2Constant baudrate to constant 波特率转为unix常量
  497. * @param baudrate 波特率
  498. * @return constant unix常量
  499. */
  500. int rate2Constant(int baudrate){
  501. // https://jim.sh/ftx/files/linux-custom-baudrate.c
  502. #define B(x) \
  503. case x: \
  504. return B##x
  505. switch (baudrate)
  506. {
  507. #ifdef B50
  508. B(50);
  509. #endif
  510. #ifdef B75
  511. B(75);
  512. #endif
  513. #ifdef B110
  514. B(110);
  515. #endif
  516. #ifdef B134
  517. B(134);
  518. #endif
  519. #ifdef B150
  520. B(150);
  521. #endif
  522. #ifdef B200
  523. B(200);
  524. #endif
  525. #ifdef B300
  526. B(300);
  527. #endif
  528. #ifdef B600
  529. B(600);
  530. #endif
  531. #ifdef B1200
  532. B(1200);
  533. #endif
  534. #ifdef B1800
  535. B(1800);
  536. #endif
  537. #ifdef B2400
  538. B(2400);
  539. #endif
  540. #ifdef B4800
  541. B(4800);
  542. #endif
  543. #ifdef B9600
  544. B(9600);
  545. #endif
  546. #ifdef B19200
  547. B(19200);
  548. #endif
  549. #ifdef B38400
  550. B(38400);
  551. #endif
  552. #ifdef B57600
  553. B(57600);
  554. #endif
  555. #ifdef B115200
  556. B(115200);
  557. #endif
  558. #ifdef B230400
  559. B(230400);
  560. #endif
  561. #ifdef B460800
  562. B(460800);
  563. #endif
  564. #ifdef B500000
  565. B(500000);
  566. #endif
  567. #ifdef B576000
  568. B(576000);
  569. #endif
  570. #ifdef B921600
  571. B(921600);
  572. #endif
  573. #ifdef B1000000
  574. B(1000000);
  575. #endif
  576. #ifdef B1152000
  577. B(1152000);
  578. #endif
  579. #ifdef B1500000
  580. B(1500000);
  581. #endif
  582. #ifdef B2000000
  583. B(2000000);
  584. #endif
  585. #ifdef B2500000
  586. B(2500000);
  587. #endif
  588. #ifdef B3000000
  589. B(3000000);
  590. #endif
  591. #ifdef B3500000
  592. B(3500000);
  593. #endif
  594. #ifdef B4000000
  595. B(4000000);
  596. #endif
  597. default:
  598. return 0;
  599. }
  600. #undef B
  601. }
  602. /**
  603. * @brief uart_set
  604. * @param fd
  605. * @param baude
  606. * @param c_flow
  607. * @param bits
  608. * @param parity
  609. * @param stop
  610. * @return 0 success -1 error
  611. */
  612. int uart_set(int fd,
  613. int baudRate = itas109::BaudRate9600,
  614. itas109::Parity parity = itas109::ParityNone,
  615. itas109::DataBits dataBits = itas109::DataBits8,
  616. itas109::StopBits stopbits = itas109::StopOne,
  617. itas109::FlowControl flowControl = itas109::FlowNone){
  618. struct termios options;
  619. //获取终端属性
  620. if (tcgetattr(fd, &options) < 0)
  621. {
  622. perror("tcgetattr error");
  623. return -1;
  624. }
  625. //设置输入输出波特率
  626. int baudRateConstant = 0;
  627. baudRateConstant = rate2Constant(baudRate);
  628. if (0 != baudRateConstant)
  629. {
  630. cfsetispeed(&options, baudRateConstant);
  631. cfsetospeed(&options, baudRateConstant);
  632. }
  633. else
  634. {
  635. // TODO: custom baudrate
  636. fprintf(stderr, "Unkown baudrate!\n");
  637. return -1;
  638. }
  639. //设置校验位
  640. switch (parity)
  641. {
  642. /*无奇偶校验位*/
  643. case itas109::ParityNone:
  644. case 'N':
  645. options.c_cflag &= ~PARENB; // PARENB:产生奇偶位,执行奇偶校验
  646. options.c_cflag &= ~INPCK; // INPCK:使奇偶校验起作用
  647. break;
  648. /*设置奇校验*/
  649. case itas109::ParityOdd:
  650. options.c_cflag |= PARENB; // PARENB:产生奇偶位,执行奇偶校验
  651. options.c_cflag |= PARODD; // PARODD:若设置则为奇校验,否则为偶校验
  652. options.c_cflag |= INPCK; // INPCK:使奇偶校验起作用
  653. options.c_cflag |= ISTRIP; // ISTRIP:若设置则有效输入数字被剥离7个字节,否则保留全部8位
  654. break;
  655. /*设置偶校验*/
  656. case itas109::ParityEven:
  657. options.c_cflag |= PARENB; // PARENB:产生奇偶位,执行奇偶校验
  658. options.c_cflag &= ~PARODD; // PARODD:若设置则为奇校验,否则为偶校验
  659. options.c_cflag |= INPCK; // INPCK:使奇偶校验起作用
  660. options.c_cflag |= ISTRIP; // ISTRIP:若设置则有效输入数字被剥离7个字节,否则保留全部8位
  661. break;
  662. /*设为空格,即停止位为2位*/
  663. case itas109::ParitySpace:
  664. options.c_cflag &= ~PARENB; // PARENB:产生奇偶位,执行奇偶校验
  665. options.c_cflag &= ~CSTOPB; // CSTOPB:使用两位停止位
  666. break;
  667. default:
  668. fprintf(stderr, "Unkown parity!\n");
  669. return -1;
  670. }
  671. //设置数据位
  672. switch (dataBits)
  673. {
  674. case itas109::DataBits5:
  675. options.c_cflag &= ~CSIZE; //屏蔽其它标志位
  676. options.c_cflag |= CS5;
  677. break;
  678. case itas109::DataBits6:
  679. options.c_cflag &= ~CSIZE; //屏蔽其它标志位
  680. options.c_cflag |= CS6;
  681. break;
  682. case itas109::DataBits7:
  683. options.c_cflag &= ~CSIZE; //屏蔽其它标志位
  684. options.c_cflag |= CS7;
  685. break;
  686. case itas109::DataBits8:
  687. options.c_cflag &= ~CSIZE; //屏蔽其它标志位
  688. options.c_cflag |= CS8;
  689. break;
  690. default:
  691. fprintf(stderr, "Unkown bits!\n");
  692. return -1;
  693. }
  694. //停止位
  695. switch (stopbits)
  696. {
  697. case itas109::StopOne:
  698. options.c_cflag &= ~CSTOPB; // CSTOPB:使用两位停止位
  699. break;
  700. case itas109::StopOneAndHalf:
  701. fprintf(stderr, "POSIX does not support 1.5 stop bits!\n");
  702. return -1;
  703. case itas109::StopTwo:
  704. options.c_cflag |= CSTOPB; // CSTOPB:使用两位停止位
  705. break;
  706. default:
  707. fprintf(stderr, "Unkown stop!\n");
  708. return -1;
  709. }
  710. //控制模式
  711. options.c_cflag |= CLOCAL; //保证程序不占用串口
  712. options.c_cflag |= CREAD; //保证程序可以从串口中读取数据
  713. //流控制
  714. switch (flowControl)
  715. {
  716. case itas109::FlowNone: ///< No flow control 无流控制
  717. options.c_cflag &= ~CRTSCTS;
  718. break;
  719. case itas109::FlowHardware: ///< Hardware(RTS / CTS) flow control 硬件流控制
  720. options.c_cflag |= CRTSCTS;
  721. break;
  722. case itas109::FlowSoftware: ///< Software(XON / XOFF) flow control 软件流控制
  723. options.c_cflag |= IXON | IXOFF | IXANY;
  724. break;
  725. default:
  726. fprintf(stderr, "Unkown c_flow!\n");
  727. return -1;
  728. }
  729. //设置输出模式为原始输出
  730. options.c_oflag &= ~OPOST; // OPOST:若设置则按定义的输出处理,否则所有c_oflag失效
  731. //设置本地模式为原始模式
  732. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  733. /*
  734. *ICANON:允许规范模式进行输入处理
  735. *ECHO:允许输入字符的本地回显
  736. *ECHOE:在接收EPASE时执行Backspace,Space,Backspace组合
  737. *ISIG:允许信号
  738. */
  739. options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  740. /*
  741. *BRKINT:如果设置了IGNBRK,BREAK键输入将被忽略
  742. *ICRNL:将输入的回车转化成换行(如果IGNCR未设置的情况下)(0x0d => 0x0a)
  743. *INPCK:允许输入奇偶校验
  744. *ISTRIP:去除字符的第8个比特
  745. *IXON:允许输出时对XON/XOFF流进行控制 (0x11 0x13)
  746. */
  747. //设置等待时间和最小接受字符
  748. options.c_cc[VTIME] = 0; //可以在select中设置
  749. options.c_cc[VMIN] = 1; //最少读取一个字符
  750. //如果发生数据溢出,只接受数据,但是不进行读操作
  751. tcflush(fd, TCIFLUSH);
  752. //激活配置
  753. if (tcsetattr(fd, TCSANOW, &options) < 0)
  754. {
  755. perror("tcsetattr failed");
  756. return -1;
  757. }
  758. return 0;
  759. }
  760. /**
  761. * @brief thread monitor 多线程监视器
  762. *
  763. */
  764. static void *commThreadMonitor(void *pParam){
  765. // Cast the void pointer passed to the thread back to
  766. // a pointer of CSerialPortWinBase class
  767. CSerialPortUnixBase *p_base = (CSerialPortUnixBase *)pParam;
  768. if (p_base)
  769. {
  770. for (; p_base->isThreadRunning();)
  771. {
  772. int readbytes = 0;
  773. // read前获取可读的字节数,不区分阻塞和非阻塞
  774. ioctl(p_base->fd, FIONREAD, &readbytes);
  775. if (readbytes >= p_base->getMinByteReadNotify()) //设定字符数,默认为2
  776. {
  777. p_base->readReady._emit();
  778. }
  779. else
  780. {
  781. usleep(1); // fix high cpu usage on unix
  782. }
  783. }
  784. }
  785. else
  786. {
  787. // point null
  788. }
  789. pthread_exit(NULL);
  790. }
  791. /**
  792. * @brief start thread monitor 启动多线程监视器
  793. *
  794. * @return
  795. * @retval true start success 启动成功
  796. * @retval false start failed 启动失败
  797. */
  798. bool startThreadMonitor(){
  799. bool bRet = true;
  800. // start read thread
  801. int ret = pthread_create(&m_monitorThread, NULL, commThreadMonitor, (void *)this);
  802. if (ret < 0)
  803. {
  804. bRet = false;
  805. printf("Create read thread error.");
  806. }
  807. return bRet;
  808. }
  809. /**
  810. * @brief stop thread monitor 停止多线程监视器
  811. *
  812. * @return
  813. * @retval true stop success 停止成功
  814. * @retval false stop failed 停止失败
  815. */
  816. bool stopThreadMonitor(){
  817. m_isThreadRunning = false;
  818. pthread_join(m_monitorThread, NULL);
  819. return true;
  820. }
  821. public:
  822. sigslot::signal0<> readReady; ///< sigslot for read 读数据信号
  823. private:
  824. std::string m_portName;
  825. int m_baudRate;
  826. itas109::Parity m_parity;
  827. itas109::DataBits m_dataBits;
  828. itas109::StopBits m_stopbits;
  829. itas109::FlowControl m_flowControl;
  830. int64_t m_readBufferSize;
  831. int fd; /* File descriptor for the port */
  832. private:
  833. pthread_t m_monitorThread; /**< read thread */
  834. bool m_isThreadRunning;
  835. pthread_mutex_t m_communicationMutex; ///< mutex
  836. };
  837. #endif //__CSERIALPORTUNIXBASE_H__