이것은 간단히 Serial 통신을 하는 예제입니다.
serial로부터 받은 데이터를 고스란히 다시 반향하여 보내는 것입니다.

현재 코드는 다음과 같이 만들어져 있습니다
  1. port=COM1 ("/dev/ttyS0")
  2. baud=9600
  3. parity=true
  4. stop=1
  5. data=8bit


  1. /*
  2. Copyright (C) Information Equipment co.,LTD
  3. All rights reserved.
  4. Code by JaeHyuk Cho
  5. CVSTAG="$Header$"
  6. */
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. #include
  13. #include
  14. #include
  15. #include
  16. #include
  17. #include
  18. #define mz_dump(m_d,m_s) do{int s_o,s_w,s_i;unsigned char s_b[17];if(((void *)(m_d))!=NULL){s_b[16]='\0';s_o=(int)0;\
  19. while(s_o<(m_s)){s_w=((m_s)-s_o)<16?((m_s)-s_o):16;printf("%08X",s_o);for(s_i=0;s_iif(s_i==8)printf(" | ");else printf(" ");\
  20. s_b[s_i]=*(((unsigned char *)(m_d))+s_o+s_i);printf("%02X",s_b[s_i]);if((s_b[s_i]&0x80)||(s_b[s_i]<' '))s_b[s_i]='.';}\
  21. while(s_i<16){if(s_i==8)printf(" ");else printf(" ");s_b[s_i]=' ';s_i++;}\
  22. printf(" [%s]\n",(char *)s_b);s_o+=16;}}else printf("error: dump null\n");}while(0)
  23. int g_mz_break = 0;
  24. void mz_my_signal(int s_signal)
  25. {
  26. switch(s_signal)
  27. {
  28. case SIGINT:
  29. g_mz_break = 1;
  30. (void)signal(s_signal, mz_my_signal);
  31. break;
  32. default:
  33. (void)fprintf(stdout, "unknown signal ! (%d)\n", s_signal);
  34. break;
  35. }
  36. }
  37. int main(void)
  38. {
  39. /* configuration */
  40. char *s_device_name = "/dev/ttyS0";
  41. speed_t s_baud = B9600;
  42. tcflag_t s_data_bits = CS8;
  43. int s_parity_bit = 1;
  44. int s_handle, s_index, s_check;
  45. struct termios s_prev_termios, s_new_termios;
  46. ssize_t s_read_bytes, s_write_bytes;
  47. struct timeval s_timeval;
  48. fd_set s_fd_in;
  49. unsigned char s_buffer[ 4 <<>
  50. (void)signal(SIGINT, mz_my_signal);
  51. s_handle = open(s_device_name, O_RDWR | O_NOCTTY);
  52. if(s_handle != (-1))
  53. {
  54. if(tcgetattr(s_handle, (struct termios *)(&s_prev_termios)) == 0)
  55. {
  56. (void)memcpy((void *)(&s_new_termios), (void *)(&s_prev_termios), (size_t)sizeof(struct termios));
  57. s_new_termios.c_iflag = IGNBRK | ((s_parity_bit == 0) ? ((tcflag_t)0) : IGNPAR);
  58. s_new_termios.c_oflag = (tcflag_t)0;
  59. s_new_termios.c_cflag = s_data_bits | CLOCAL | CREAD;
  60. s_new_termios.c_lflag = (tcflag_t)0;
  61. for(s_index = 0;s_index <>
  62. s_new_termios.c_cc[VMIN] = (cc_t)1;
  63. s_new_termios.c_cc[VTIME] = (cc_t)0;
  64. if(cfsetispeed((struct termios *)(&s_new_termios), s_baud) == 0 &&
  65. cfsetospeed((struct termios *)(&s_new_termios), s_baud) == 0)
  66. {
  67. if(tcsetattr(s_handle, TCSANOW, (struct termios *)(&s_new_termios)) == 0)
  68. {
  69. if(tcflush(s_handle, TCIOFLUSH) == 0)
  70. {
  71. (void)fprintf(stdout, "Ready serial\n");
  72. do
  73. {
  74. FD_ZERO(&s_fd_in);
  75. FD_SET(s_handle, &s_fd_in);
  76. s_timeval.tv_sec = (long)1, s_timeval.tv_usec = (long)0;
  77. s_check = select(s_handle + 1, (fd_set *)(&s_fd_in), (fd_set *)0, (fd_set *)0, (struct timeval *)(&s_timeval));
  78. if(s_check > 0)
  79. {
  80. s_read_bytes = read(s_handle, (void *)(&s_buffer[0]), (size_t)sizeof(s_buffer));
  81. if(s_read_bytes > ((ssize_t)0))
  82. {
  83. (void)fprintf(stdout, "recv data %d byte(s) {\n", (int)s_read_bytes);
  84. mz_dump(&s_buffer[0], s_read_bytes);
  85. (void)fprintf(stdout, "}\n");
  86. #if 1 /* echo part */
  87. s_write_bytes = write(s_handle, (void *)(&s_buffer[0]), (size_t)s_read_bytes);
  88. (void)fprintf(stdout, "echo %d/%d byte(s)\n", (int)s_write_bytes, (int)s_read_bytes);
  89. #endif
  90. }
  91. else if(s_read_bytes == ((ssize_t)0))(void)fprintf(stdout, "no data\n");
  92. else (void)perror("read");
  93. }
  94. else if(s_check == 0)(void)fprintf(stdout, "wait\n");
  95. else (void)perror("select");
  96. }while(g_mz_break == 0);
  97. (void)fprintf(stdout, "End of serial\n");
  98. }
  99. else (void)perror("tcflush");
  100. }
  101. else (void)perror("tcsetattr");
  102. }
  103. else (void)perror("cfset{i/o}speed");
  104. if(tcsetattr(s_handle, TCSANOW, (struct termios *)(&s_prev_termios)) != 0)(void)perror("tcsetattr(restore)");
  105. }
  106. else (void)perror("tcgetattr");
  107. (void)close(s_handle);
  108. }
  109. else (void)perror("open");
  110. return(1);
  111. }
  112. /* vim: set expandtab: */
  113. /* End of source */

profile
도구의 결함은 장인의 손으로 극복한다.