libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmSerialPort.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmSerialPort.h"
  9. #include "cmThread.h"
  10. #include <poll.h>
  11. #include <termios.h>
  12. #include <unistd.h> // close()
  13. #include <fcntl.h> // O_RDWR
  14. #include <sys/ioctl.h> // TIOCEXCL
  15. typedef struct cmSerialPort_str
  16. {
  17. cmErr_t _err;
  18. cmThreadH_t _thH;
  19. const char* _deviceStr;
  20. int _deviceH;
  21. unsigned _baudRate;
  22. unsigned _cfgFlags;
  23. cmSeCallbackFunc_t _cbFunc;
  24. void* _cbArg;
  25. struct termios _ttyAttrs;
  26. struct pollfd _pollfd;
  27. unsigned _pollPeriodMs;
  28. } cmSerialPort_t;
  29. cmSerialPort_t* _cmSePtrFromHandle( cmSeH_t h )
  30. {
  31. cmSerialPort_t* p = (cmSerialPort_t*)h.h;
  32. assert(p!=NULL);
  33. return p;
  34. }
  35. void _cmSeSetClosedState( cmSerialPort_t* p )
  36. {
  37. if( p->_deviceStr != NULL )
  38. cmMemFree((char*)(p->_deviceStr));
  39. p->_deviceH = -1;
  40. p->_deviceStr = NULL;
  41. p->_baudRate = 0;
  42. p->_cfgFlags = 0;
  43. p->_cbFunc = NULL;
  44. p->_cbArg = NULL;
  45. }
  46. cmSeRC_t _cmSeGetAttributes( cmSerialPort_t* p, struct termios* attr )
  47. {
  48. if( tcgetattr(p->_deviceH, attr) == -1 )
  49. return cmErrSysMsg(&p->_err,kGetAttrFailSeRC,errno,"Error getting tty attributes from %s.",p->_deviceStr);
  50. return kOkSeRC;
  51. }
  52. cmSeRC_t _cmSePoll( cmSerialPort_t* p, unsigned timeOutMs )
  53. {
  54. cmSeRC_t rc = kOkSeRC;
  55. int sysRC;
  56. if((sysRC = poll(&p->_pollfd,1,timeOutMs)) == 0)
  57. rc = kTimeOutSeRC;
  58. else
  59. {
  60. if( sysRC < 0 )
  61. rc = cmErrSysMsg(&p->_err,kReadFailSeRC,errno,"Poll failed on serial port.");
  62. }
  63. return rc;
  64. }
  65. bool _cmSeThreadFunc(void* param)
  66. {
  67. cmSerialPort_t* p = (cmSerialPort_t*)param;
  68. cmSeH_t h;
  69. h.h = p;
  70. unsigned readN;
  71. if( cmSeIsOpen(h) )
  72. cmSeReceiveCbTimeOut(h,p->_pollPeriodMs,&readN);
  73. return true;
  74. }
  75. cmSeRC_t _cmSeDestroy( cmSerialPort_t* p )
  76. {
  77. cmSeRC_t rc = kOkSeRC;
  78. // stop the thread first
  79. if( cmThreadDestroy(&p->_thH) != kOkThRC )
  80. {
  81. rc = cmErrMsg(&p->_err,kThreadErrSeRC,"Thread destroy failed.");
  82. goto errLabel;
  83. }
  84. // Block until all written output has been sent from the device.
  85. // Note that this call is simply passed on to the serial device driver.
  86. // See tcsendbreak(3) ("man 3 tcsendbreak") for details.
  87. if (tcdrain(p->_deviceH) == -1)
  88. {
  89. rc = cmErrSysMsg(&p->_err,kFlushFailSeRC,errno,"Error waiting for serial device '%s' to drain.", p->_deviceStr );
  90. goto errLabel;
  91. }
  92. // It is good practice to reset a serial port back to the state in
  93. // which you found it. This is why we saved the original termios struct
  94. // The constant TCSANOW (defined in termios.h) indicates that
  95. // the change should take effect immediately.
  96. if (tcsetattr(p->_deviceH, TCSANOW, &p->_ttyAttrs) == -1)
  97. {
  98. rc = cmErrSysMsg(&p->_err,kSetAttrFailSeRC,errno,"Error resetting tty attributes on serial device '%s'.",p->_deviceStr);
  99. goto errLabel;
  100. }
  101. if( p->_deviceH != -1 )
  102. {
  103. if( close(p->_deviceH ) != 0 )
  104. {
  105. rc = cmErrSysMsg(&p->_err,kCloseFailSeRC,errno,"Port close failed on serial dvice '%s'.", p->_deviceStr);
  106. goto errLabel;
  107. }
  108. _cmSeSetClosedState(p);
  109. }
  110. cmMemPtrFree(&p);
  111. errLabel:
  112. return rc;
  113. }
  114. cmSeH_t cmSeCreate( cmCtx_t* ctx, cmSeH_t* hp, const char* deviceStr, unsigned baudRate, unsigned cfgFlags, cmSeCallbackFunc_t cbFunc, void* cbArg, unsigned pollPeriodMs )
  115. {
  116. cmSeRC_t rc = kOkSeRC;
  117. struct termios options;
  118. cmSeH_t h;
  119. // if the port is already open then close it
  120. if((rc = cmSeDestroy(hp)) != kOkSeRC )
  121. return *hp;
  122. cmSerialPort_t* p = cmMemAllocZ(cmSerialPort_t,1);
  123. cmErrSetup(&p->_err,&ctx->rpt,"Serial Port");
  124. p->_deviceH = -1;
  125. // open the port
  126. if( (p->_deviceH = open(deviceStr, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1 )
  127. {
  128. rc = cmErrSysMsg(&p->_err,kOpenFailSeRC,errno,"Error opening serial '%s'",cmStringNullGuard(deviceStr));
  129. goto errLabel;;
  130. }
  131. // Note that open() follows POSIX semantics: multiple open() calls to
  132. // the same file will succeed unless the TIOCEXCL ioctl is issued.
  133. // This will prevent additional opens except by root-owned processes.
  134. // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.
  135. if( ioctl(p->_deviceH, TIOCEXCL) == -1 )
  136. {
  137. rc = cmErrSysMsg(&p->_err,kResourceNotAvailableSeRC,errno,"The serial device '%s' is already in use.", cmStringNullGuard(deviceStr));
  138. goto errLabel;
  139. }
  140. // Now that the device is open, clear the O_NONBLOCK flag so
  141. // subsequent I/O will block.
  142. // See fcntl(2) ("man 2 fcntl") for details.
  143. /*
  144. if (fcntl(_deviceH, F_SETFL, 0) == -1)
  145. {
  146. _error("Error clearing O_NONBLOCK %s - %s(%d).", pr.devFilePath.c_str(), strerror(errno), errno);
  147. goto errLabel;
  148. }
  149. */
  150. // Get the current options and save them so we can restore the
  151. // default settings later.
  152. if (tcgetattr(p->_deviceH, &p->_ttyAttrs) == -1)
  153. {
  154. rc = cmErrSysMsg(&p->_err,kGetAttrFailSeRC,errno,"Error getting tty attributes from the device '%s'.",deviceStr);
  155. goto errLabel;
  156. }
  157. // The serial port attributes such as timeouts and baud rate are set by
  158. // modifying the termios structure and then calling tcsetattr to
  159. // cause the changes to take effect. Note that the
  160. // changes will not take effect without the tcsetattr() call.
  161. // See tcsetattr(4) ("man 4 tcsetattr") for details.
  162. options = p->_ttyAttrs;
  163. // Set raw input (non-canonical) mode, with reads blocking until either
  164. // a single character has been received or a 100ms timeout expires.
  165. // See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios")
  166. // for details.
  167. cfmakeraw(&options);
  168. options.c_cc[VMIN] = 1;
  169. options.c_cc[VTIME] = 1;
  170. // The baud rate, word length, and handshake options can be set as follows:
  171. // set baud rate
  172. cfsetspeed(&options, baudRate);
  173. options.c_cflag |= CREAD | CLOCAL; // ignore modem controls
  174. // set data word size
  175. cmClrBits(options.c_cflag, CSIZE); // clear the word size bits
  176. cmEnaBits(options.c_cflag, CS5, cmIsFlag(cfgFlags, kDataBits5SeFl));
  177. cmEnaBits(options.c_cflag, CS6, cmIsFlag(cfgFlags, kDataBits6SeFl));
  178. cmEnaBits(options.c_cflag, CS7, cmIsFlag(cfgFlags, kDataBits7SeFl));
  179. cmEnaBits(options.c_cflag, CS8, cmIsFlag(cfgFlags, kDataBits8SeFl));
  180. cmClrBits(options.c_cflag, PARENB); // assume no-parity
  181. // if the odd or even parity flag is set
  182. if( cmIsFlag( cfgFlags, kEvenParitySeFl) || cmIsFlag( cfgFlags, kOddParitySeFl ) )
  183. {
  184. cmSetBits(options.c_cflag, PARENB);
  185. if( cmIsFlag(cfgFlags, kOddParitySeFl ) )
  186. cmSetBits( options.c_cflag, PARODD);
  187. }
  188. // set two stop bits
  189. cmEnaBits( options.c_cflag, CSTOPB, cmIsFlag(cfgFlags, k2StopBitSeFl));
  190. // set hardware flow control
  191. //cmEnaBits(options.c_cflag, CCTS_OFLOW, cmIsFlag(cfgFlags, kCTS_OutFlowCtlFl));
  192. //cmEnaBits(options.c_cflag, CRTS_IFLOW, cmIsFlag(cfgFlags, kRTS_InFlowCtlFl));
  193. //cmEnaBits(options.c_cflag, CDTR_IFLOW, cmIsFlag(cfgFlags, kDTR_InFlowCtlFl));
  194. //cmEnaBits(options.c_cflag, CDSR_OFLOW, cmIsFlag(cfgFlags, kDSR_OutFlowCtlFl));
  195. //cmEnaBits(options.c_cflag, CCAR_OFLOW, cmIsFlag(cfgFlags, kDCD_OutFlowCtlFl));
  196. cmClrBits(options.c_cflag,CRTSCTS); // turn-off hardware flow control
  197. // 7 bit words, enable even parity, CTS out ctl flow, RTS in ctl flow
  198. // note: set PARODD and PARENB to enable odd parity)
  199. //options.c_cflag |= (CS7 | PARENB | CCTS_OFLOW | CRTS_IFLOW );
  200. // Cause the new options to take effect immediately.
  201. if (tcsetattr(p->_deviceH, TCSANOW, &options) == -1)
  202. {
  203. rc = cmErrSysMsg(&p->_err,kSetAttrFailSeRC,errno,"Error setting tty attributes on serial device %.", deviceStr);
  204. goto errLabel;
  205. }
  206. memset(&p->_pollfd,0,sizeof(p->_pollfd));
  207. p->_pollfd.fd = p->_deviceH;
  208. p->_pollfd.events = POLLIN;
  209. p->_deviceStr = cmMemAllocStr( deviceStr );
  210. p->_baudRate = baudRate;
  211. p->_cfgFlags = cfgFlags;
  212. p->_cbFunc = cbFunc;
  213. p->_cbArg = cbArg;
  214. p->_pollPeriodMs = pollPeriodMs;
  215. // create the listening thread
  216. if( cmThreadCreate( &p->_thH, _cmSeThreadFunc, p, &ctx->rpt) != kOkThRC )
  217. {
  218. rc = cmErrMsg(&p->_err,kThreadErrSeRC,"Thread initialization failed.");
  219. goto errLabel;
  220. }
  221. if( hp != NULL )
  222. hp->h = p;
  223. else
  224. h.h = p;
  225. errLabel:
  226. if( rc != kOkSeRC )
  227. {
  228. _cmSeDestroy(p);
  229. h.h = NULL;
  230. }
  231. return hp != NULL ? *hp : h;
  232. }
  233. cmSeRC_t cmSeDestroy(cmSeH_t* hp )
  234. {
  235. cmSeRC_t rc = kOkSeRC;
  236. if( hp==NULL || !cmSeIsOpen(*hp) )
  237. return kOkSeRC;
  238. cmSerialPort_t* p = _cmSePtrFromHandle(*hp);
  239. if((rc = _cmSeDestroy(p)) != kOkSeRC )
  240. return rc;
  241. hp->h = NULL;
  242. return rc;
  243. }
  244. cmSeRC_t cmSeSetCallback( cmSeH_t h, cmSeCallbackFunc_t cbFunc, void* cbArg )
  245. {
  246. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  247. p->_cbFunc = cbFunc;
  248. p->_cbArg = cbArg;
  249. return kOkSeRC;
  250. }
  251. cmSeRC_t cmSeStart( cmSeH_t h )
  252. {
  253. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  254. if( cmThreadPause(p->_thH,0) != kOkThRC )
  255. return cmErrMsg(&p->_err,kThreadErrSeRC,0,"Thread start failed.");
  256. return kOkSeRC;
  257. }
  258. bool cmSeIsOpen( cmSeH_t h)
  259. {
  260. if( h.h == NULL )
  261. return false;
  262. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  263. return p->_deviceH != -1;
  264. }
  265. cmSeRC_t cmSeSend( cmSeH_t h, const void* byteA, unsigned byteN )
  266. {
  267. cmSeRC_t rc = kOkSeRC;
  268. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  269. if( !cmSeIsOpen(h) )
  270. return cmErrWarnMsg( &p->_err, kResourceNotAvailableSeRC, "An attempt was made to transmit from a closed serial port.");
  271. if( byteN == 0 )
  272. return rc;
  273. // implement a non blocking write - if less than all the bytes were written then iterate
  274. unsigned i = 0;
  275. do
  276. {
  277. int n = 0;
  278. if((n = write( p->_deviceH, ((char*)byteA)+i, byteN-i )) == -1 )
  279. {
  280. rc = cmErrSysMsg(&p->_err,kWriteFailSeRC,errno,"Write failed on serial port '%s'.", p->_deviceStr );
  281. break;
  282. }
  283. i += n;
  284. }while( i<byteN );
  285. return rc;
  286. }
  287. cmSeRC_t cmSeReceiveCbNb( cmSeH_t h, unsigned* readN_Ref)
  288. {
  289. cmSeRC_t rc = kOkSeRC;
  290. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  291. const unsigned bufN = 512;
  292. char buf[ bufN ];
  293. if( readN_Ref != NULL)
  294. *readN_Ref = 0;
  295. if((rc = cmSeReceiveNb(h,buf,bufN,readN_Ref)) == kOkSeRC )
  296. if( readN_Ref > 0 && p->_cbFunc != NULL )
  297. p->_cbFunc( p->_cbArg, buf, *readN_Ref );
  298. return rc;
  299. }
  300. cmSeRC_t cmSeReceiveCbTimeOut( cmSeH_t h, unsigned timeOutMs, unsigned* readN_Ref)
  301. {
  302. cmSeRC_t rc;
  303. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  304. if((rc = _cmSePoll(p,timeOutMs)) == kOkSeRC )
  305. rc = cmSeReceiveCbNb(h,readN_Ref);
  306. return rc;
  307. }
  308. cmSeRC_t cmSeReceiveNb( cmSeH_t h, void* buf, unsigned bufN, unsigned* readN_Ref)
  309. {
  310. cmSeRC_t rc = kOkSeRC;
  311. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  312. if( readN_Ref != NULL )
  313. *readN_Ref = 0;
  314. if( !cmSeIsOpen(h) )
  315. return cmErrWarnMsg(&p->_err, kResourceNotAvailableSeRC, "An attempt was made to read from a closed serial port.");
  316. int n = 0;
  317. // if attempt to read the port succeeded ...
  318. if((n =read( p->_deviceH, buf, bufN )) != -1 )
  319. *readN_Ref = n;
  320. else
  321. {
  322. // ... or failed and it wasn't because the port was empty
  323. if( errno != EAGAIN)
  324. rc = cmErrSysMsg(&p->_err,kReadFailSeRC,errno,"An attempt to read the serial port '%s' failed.", p->_deviceStr );
  325. }
  326. return rc;
  327. }
  328. cmSeRC_t cmSeReceive( cmSeH_t h, void* buf, unsigned bufByteN, unsigned timeOutMs, unsigned* readN_Ref )
  329. {
  330. cmSeRC_t rc = kOkSeRC;
  331. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  332. if((rc = _cmSePoll(p,timeOutMs)) == kOkSeRC )
  333. rc = cmSeReceiveNb(h,buf,bufByteN,readN_Ref);
  334. return rc;
  335. }
  336. const char* cmSeDevice( cmSeH_t h)
  337. {
  338. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  339. return p->_deviceStr;
  340. }
  341. unsigned cmSeBaudRate( cmSeH_t h)
  342. {
  343. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  344. return p->_baudRate;
  345. }
  346. unsigned cmSeCfgFlags( cmSeH_t h)
  347. {
  348. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  349. return p->_cfgFlags;
  350. }
  351. unsigned cmSeReadInBaudRate( cmSeH_t h )
  352. {
  353. struct termios attr;
  354. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  355. if((_cmSeGetAttributes(p,&attr)) != kOkSeRC )
  356. return 0;
  357. return cfgetispeed(&attr);
  358. }
  359. unsigned cmSeReadOutBaudRate( cmSeH_t h)
  360. {
  361. struct termios attr;
  362. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  363. if((_cmSeGetAttributes(p,&attr)) != kOkSeRC )
  364. return 0;
  365. return cfgetospeed(&attr);
  366. }
  367. unsigned cmSeReadCfgFlags( cmSeH_t h)
  368. {
  369. struct termios attr;
  370. unsigned result = 0;
  371. cmSerialPort_t* p = _cmSePtrFromHandle(h);
  372. if((_cmSeGetAttributes(p,&attr)) == false )
  373. return 0;
  374. switch( attr.c_cflag & CSIZE )
  375. {
  376. case CS5:
  377. cmSetBits( result, kDataBits5SeFl);
  378. break;
  379. case CS6:
  380. cmSetBits( result, kDataBits6SeFl );
  381. break;
  382. case CS7:
  383. cmSetBits( result, kDataBits7SeFl);
  384. break;
  385. case CS8:
  386. cmSetBits( result, kDataBits8SeFl);
  387. break;
  388. }
  389. cmEnaBits( result, k2StopBitSeFl, cmIsFlag( attr.c_cflag, CSTOPB ));
  390. cmEnaBits( result, k1StopBitSeFl, !cmIsFlag( attr.c_cflag, CSTOPB ));
  391. if( cmIsFlag( attr.c_cflag, PARENB ) )
  392. {
  393. cmEnaBits( result, kOddParitySeFl, cmIsFlag( attr.c_cflag, PARODD ));
  394. cmEnaBits( result, kEvenParitySeFl, !cmIsFlag( attr.c_cflag, PARODD ));
  395. }
  396. return result;
  397. }
  398. //====================================================================================================
  399. //
  400. //
  401. void _cmSePortTestCb( void* arg, const void* byteA, unsigned byteN )
  402. {
  403. const char* text = (const char*)byteA;
  404. for(unsigned i=0; i<byteN; ++i)
  405. printf("%c:%i ",text[i],(int)text[i]);
  406. if( byteN )
  407. fflush(stdout);
  408. }
  409. cmSeRC_t cmSePortTest(cmCtx_t* ctx)
  410. {
  411. // Use this test an Arduino running study/serial/arduino_xmt_rcv/main.c
  412. cmSeRC_t rc = kOkSeRC;
  413. const char* device = "/dev/ttyACM0";
  414. unsigned baud = 38400;
  415. unsigned serialCfgFlags = kDefaultCfgSeFlags;
  416. unsigned pollPeriodMs = 50;
  417. cmSeH_t h;
  418. h.h = NULL;
  419. h = cmSeCreate(ctx,&h,device,baud,serialCfgFlags,_cmSePortTestCb,NULL,pollPeriodMs);
  420. if( cmSeIsOpen(h) )
  421. cmSeStart(h);
  422. bool quitFl = false;
  423. printf("q=quit\n");
  424. while(!quitFl)
  425. {
  426. char c = getchar();
  427. if( c == 'q')
  428. quitFl = true;
  429. else
  430. if( '0' <= c && c <= 'z' )
  431. cmSeSend(h,&c,1);
  432. }
  433. cmSeDestroy(&h);
  434. return rc;
  435. }