fork download
  1. #include <termios.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. void *reader(void *arg);
  11. void nonblock();
  12.  
  13. int aa=0;
  14.  
  15. int main(void)
  16. {
  17. struct termios backup, mine;
  18. pthread_t thread;
  19. int leng=256,charr;
  20. char test[256]={0};
  21.  
  22. printf("start...\n");
  23.  
  24. aa = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
  25. if (aa < 0) return -1;
  26.  
  27. tcgetattr(aa,&backup);
  28. bzero(&mine,sizeof(mine));
  29.  
  30. mine.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  31. mine.c_iflag = IGNPAR | ICRNL;
  32. mine.c_oflag = 0;
  33. mine.c_lflag = 0;
  34. mine.c_cc[VMIN] = 1;
  35.  
  36. tcflush(aa,TCIFLUSH);
  37. tcsetattr(aa,TCSANOW,&mine);
  38.  
  39. if (pthread_create(&thread,NULL,reader,NULL)) return -1;
  40.  
  41. nonblock();
  42.  
  43. while(1)
  44. {
  45. if(charr == 'q') break;
  46. charr=fgetc(stdin);
  47. tcflush(aa,TCIFLUSH);
  48. write(aa,&charr,1);
  49. printf("Sending : %c\n",charr);
  50.  
  51. usleep(100);
  52. }
  53.  
  54. tcsetattr(aa,TCSANOW,&backup);
  55. close(aa);
  56. nonblock();
  57. return 0;
  58.  
  59. }
  60.  
  61. void *reader(void *arg)
  62. {
  63. int tty=aa,leng=0;
  64. char buffer[512]={0};
  65. while(1)
  66. {
  67. leng=read(tty,buffer,sizeof(buffer));
  68. if(leng > 0)
  69. {
  70. printf("Received : %s Length : %d\n",buffer,leng);
  71. }
  72. usleep(100);
  73. }
  74.  
  75. }
  76.  
  77. void nonblock()
  78. {
  79. static struct termios state;
  80. static int status = 0;
  81.  
  82. if(status == 0)
  83. {
  84. tcgetattr(STDIN_FILENO,&state);
  85. state.c_lflag &= ~ICANON;
  86. state.c_lflag &= ~ECHO;
  87. state.c_cc[VMIN] = 1;
  88. status = 1;
  89. }
  90. else state.c_lflag |= (ICANON | ECHO);
  91.  
  92. tcsetattr(STDIN_FILENO,TCSANOW,&state);
  93. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:20:10: warning: unused variable ‘test’ [-Wunused-variable]
     char test[256]={0};
          ^
prog.c:19:9: warning: unused variable ‘leng’ [-Wunused-variable]
     int leng=256,charr;
         ^
/home/BlE9pX/cc6GJJQ0.o: In function `main':
prog.c:(.text.startup+0xbe): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty