samir82show

serial connector1

Feb 27th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. /* baudrate settings are defined in <asm/termbits.h>, which is included by <termios.h> */
  10. #define BAUDRATE B38400
  11. /* change this definition for the correct port */
  12. #define MODEMDEVICE "/dev/ttyUSB0"
  13. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  14. #define FALSE 0
  15. #define MAX 50
  16. #define TRUE 1
  17. volatile int STOP=FALSE;
  18. int
  19. main()
  20. {
  21. int i, fd, res;
  22. char line[MAX], c;
  23. struct termios oldtio,newtio;
  24. char buf[255];
  25. /* Open modem device for reading and writing and not as controlling tty because we don't want to get killed if linenoise sends CTRL-C. */
  26. fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  27. if (fd <0) {
  28. perror(MODEMDEVICE); exit(-1);
  29. }
  30. tcgetattr(fd,&oldtio); /* save current serial port settings */
  31. bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
  32. /* BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. CRTSCTS : output hardware flow control (only used if the cable has
  33. all necessary lines. See sect. 7 of Serial-HOWTO) CS8 : 8n1 (8bit,no parity,1 stopbit) CLOCAL : local connection, no modem contol
  34. "putty_emul.c" 83L, 3563C 1,1 Top
  35. newtio.c_cc[VSWTC] = 0; /* '\0' */
  36. newtio.c_cc[VSTART] = 0; /* Ctrl-q */
  37. newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
  38. newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
  39. newtio.c_cc[VEOL] = 0; /* '\0' */
  40. newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
  41. newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
  42. newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
  43. newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
  44. newtio.c_cc[VEOL2] = 0; /* '\0' */
  45.  
  46. /* now clean the modem line and activate the settings for the port */
  47. tcflush(fd, TCIFLUSH);
  48. tcsetattr(fd,TCSANOW,&newtio);
  49.  
  50. /* terminal settings done, now handle input In this example, inputting a 'z' at the beginning of a line will exit the program. */
  51. i = 0;
  52. while ((c = getchar()) && i < MAX - 1) {
  53. line[i++] = c;
  54. if (c == '\n')
  55. break;
  56. }
  57. line[i] = 0;
  58. write(fd, line, i);
  59. i = 0;
  60. while ((c == getchar()) != '\n')
  61. line[i++] = c;
  62. line[i] = '\0';
  63. printf("%s\n", line);
  64. /* restore the old port settings */
  65. tcsetattr(fd,TCSANOW,&oldtio);
  66. return (0);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment