Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. /*Non-Canonical Input Processing*/
  2.  
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <termios.h>
  7. #include <stdio.h>
  8.  
  9. #define BAUDRATE B38400
  10. #define MODEMDEVICE "/dev/ttyS1"
  11. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  12. #define FALSE 0
  13. #define TRUE 1
  14.  
  15. volatile int STOP=FALSE;
  16.  
  17. int main(int argc, char** argv)
  18. {
  19. int fd,c, res;
  20. struct termios oldtio,newtio;
  21. char buf[255];
  22. char buf2[255];
  23. int i, sum = 0, speed = 0;
  24.  
  25. if ( (argc < 2) ||
  26. ((strcmp("/dev/ttyS0", argv[1])!=0) &&
  27. (strcmp("/dev/ttyS1", argv[1])!=0) )) {
  28. printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
  29. exit(1);
  30. }
  31.  
  32.  
  33. /*
  34. Open serial port device for reading and writing and not as controlling tty
  35. because we don't want to get killed if linenoise sends CTRL-C.
  36. */
  37.  
  38.  
  39. fd = open(argv[1], O_RDWR | O_NOCTTY );
  40. if (fd <0) {perror(argv[1]); exit(-1); }
  41.  
  42. if ( tcgetattr(fd,&oldtio) == -1) { /* save current port settings */
  43. perror("tcgetattr");
  44. exit(-1);
  45. }
  46.  
  47. bzero(&newtio, sizeof(newtio));
  48. newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
  49. newtio.c_iflag = IGNPAR;
  50. newtio.c_oflag = 0;
  51.  
  52. /* set input mode (non-canonical, no echo,...) */
  53. newtio.c_lflag = 0;
  54.  
  55. newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
  56. newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received */
  57.  
  58.  
  59.  
  60. /*
  61. VTIME e VMIN devem ser alterados de forma a proteger com um temporizador a
  62. leitura do(s) pr�ximo(s) caracter(es)
  63. */
  64.  
  65.  
  66.  
  67. tcflush(fd, TCIOFLUSH);
  68.  
  69. if ( tcsetattr(fd,TCSANOW,&newtio) == -1) {
  70. perror("tcsetattr");
  71. exit(-1);
  72. }
  73.  
  74. printf("New termios structure set\n");
  75.  
  76.  
  77.  
  78. // for (i = 0; i < 255; i++) {
  79. // buf[i] = 'a';
  80. // }
  81. //
  82. // /*testing*/
  83. // buf[25] = '\n';
  84.  
  85. gets(buf);
  86. int n= strlen(buf)+1;
  87. res = write(fd,buf,n);
  88. printf("%d bytes written\n", res);
  89.  
  90.  
  91. /*
  92. O ciclo FOR e as instru��es seguintes devem ser alterados de modo a respeitar
  93. o indicado no gui�o
  94. */
  95. char* fr;
  96. strcpy(buf2, "");
  97.  
  98. while (STOP==FALSE) { /* loop for input */
  99. read(fd,fr,1); /* returns after 5 chars have been input */
  100. strcat(buf2,fr); /* so we can printf... */
  101.  
  102.  
  103. if (!strcmp(fr,"\0")) STOP=TRUE;
  104. }
  105. printf ("%s\n",buf2);
  106.  
  107.  
  108.  
  109. if ( tcsetattr(fd,TCSANOW,&oldtio) == -1) {
  110. perror("tcsetattr");
  111. exit(-1);
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. close(fd);
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement