UNIXSerialPort.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include "UNIXSerialPortP.h"
  9. #include "WonX.h"
  10. #include "etc.h"
  11. /*****************************************************************************/
  12. /* メンバ関数の定義 */
  13. /*****************************************************************************/
  14. /*---------------------------------------------------------------------------*/
  15. /* ポートの open/close */
  16. /*---------------------------------------------------------------------------*/
  17. int UNIXSerialPort_Open(UNIXSerialPort unix_serial_port)
  18. {
  19. unix_serial_port->opened = 1;
  20. return (0);
  21. }
  22. int UNIXSerialPort_Close(UNIXSerialPort unix_serial_port)
  23. {
  24. unix_serial_port->opened = 0;
  25. return (0);
  26. }
  27. int UNIXSerialPort_IsOpened(UNIXSerialPort unix_serial_port)
  28. { return (unix_serial_port->opened != 0); }
  29. int UNIXSerialPort_IsClosed(UNIXSerialPort unix_serial_port)
  30. { return (unix_serial_port->opened == 0); }
  31. /*---------------------------------------------------------------------------*/
  32. /* 受信データがあるかどうか */
  33. /*---------------------------------------------------------------------------*/
  34. /*
  35. * タイムアウト時間をミリ秒単位で指定.
  36. * 0 のときは,即時
  37. * -1 のときは,無期限待ち
  38. */
  39. int UNIXSerialPort_IsDataExisting(UNIXSerialPort unix_serial_port,
  40. int timeout)
  41. {
  42. fd_set bitmap;
  43. struct timeval t;
  44. struct timeval * t_p;
  45. #if 0
  46. int c;
  47. #endif
  48. if (UNIXSerialPort_IsClosed(unix_serial_port)) return (0);
  49. /*
  50. * 0 のときは,即時
  51. * -1 のときは,無期限待ち
  52. */
  53. if (timeout == -1) {
  54. t_p = NULL;
  55. } else {
  56. t.tv_sec = timeout / 1000;
  57. t.tv_usec = (timeout % 1000) * 1000;
  58. t_p = &t;
  59. }
  60. /*
  61. * FreeBSD3.4 で実験したところ,
  62. * アラームシグナルを使用する場合,select()でのブロック中に
  63. * アラームシグナルが発生すると,その直後にselect()もタイムアウト
  64. * してしまうので,注意.
  65. * (select() がタイムアウトした後にアラームシグナルが発生する場合は,
  66. * 正常に動作した)
  67. * 論理上は問題が無いが,期待した時間だけ待ってくれないという現象が
  68. * 起きる可能性がある.
  69. */
  70. #if 0
  71. /* 読み飛ばしたい文字があるときは,こっちのコードを使う */
  72. do {
  73. FD_ZERO(&bitmap);
  74. FD_SET(fileno(stdin), &bitmap);
  75. select(fileno(stdin) + 1, &bitmap, NULL, NULL, t_p);
  76. if (!FD_ISSET(fileno(stdin), &bitmap))
  77. return (0);
  78. c = fgetc(stdin);
  79. } while (0); /* 読み飛ばしたい文字があるときは,ここでcをチェックする */
  80. ungetc(c, stdin);
  81. #else
  82. FD_ZERO(&bitmap);
  83. FD_SET(fileno(stdin), &bitmap);
  84. select(fileno(stdin) + 1, &bitmap, NULL, NULL, t_p);
  85. if (!FD_ISSET(fileno(stdin), &bitmap))
  86. return (0);
  87. #endif
  88. return (1);
  89. }
  90. /*---------------------------------------------------------------------------*/
  91. /* 受信 */
  92. /*---------------------------------------------------------------------------*/
  93. int UNIXSerialPort_ReceiveCharacter(UNIXSerialPort unix_serial_port,
  94. int timeout)
  95. {
  96. int c;
  97. if (UNIXSerialPort_IsClosed(unix_serial_port)) return (-1);
  98. c = UNIXSerialPort_IsDataExisting(unix_serial_port, timeout)
  99. ? fgetc(stdin) : -1;
  100. c = (c == '\n') ? '\r' : c; /* \r のエミュレーション */
  101. return (c);
  102. }
  103. /*---------------------------------------------------------------------------*/
  104. /* 送信 */
  105. /*---------------------------------------------------------------------------*/
  106. int UNIXSerialPort_SendCharacter(UNIXSerialPort unix_serial_port,
  107. unsigned char c)
  108. {
  109. if (UNIXSerialPort_IsClosed(unix_serial_port)) return (0);
  110. wonx_print_character(stdout, c);
  111. return (1);
  112. }
  113. /*---------------------------------------------------------------------------*/
  114. /* オブジェクトの作成 */
  115. /*---------------------------------------------------------------------------*/
  116. UNIXSerialPort UNIXSerialPort_Create()
  117. {
  118. UNIXSerialPort unix_serial_port;
  119. unix_serial_port = (UNIXSerialPort)malloc(sizeof(_UNIXSerialPort));
  120. if (unix_serial_port == NULL)
  121. WonX_Error("UNIXSerialPort_Create", "Cannot allocate memory.");
  122. unix_serial_port->opened = 0;
  123. return (unix_serial_port);
  124. }
  125. /*---------------------------------------------------------------------------*/
  126. /* オブジェクトの削除 */
  127. /*---------------------------------------------------------------------------*/
  128. UNIXSerialPort UNIXSerialPort_Destroy(UNIXSerialPort unix_serial_port)
  129. {
  130. if (unix_serial_port == NULL)
  131. WonX_Error("UNIXSerialPort_Destroy", "Object is not created.");
  132. if (UNIXSerialPort_IsOpened(unix_serial_port)) {
  133. UNIXSerialPort_Close(unix_serial_port);
  134. unix_serial_port->opened = 0;
  135. }
  136. free(unix_serial_port);
  137. return (NULL);
  138. }
  139. /*****************************************************************************/
  140. /* ここまで */
  141. /*****************************************************************************/
  142. /*****************************************************************************/
  143. /* End of File. */
  144. /*****************************************************************************/