Advertisement
Guest User

serial break detection

a guest
Jun 26th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <termios.h>
  8. #include <unistd.h>
  9. #include <sys/ioctl.h>
  10. #include <unistd.h>
  11. #include <termios.h>
  12. #include <signal.h>
  13. __sighandler_t sighandle(int signum, __sighandler_t h) {
  14.     //fprintf(stderr, "BREAK DETECTED\n");
  15.     printf("break com received\n");
  16.     signal(SIGINT, (__sighandler_t) sighandle);
  17.     return SIG_IGN;
  18. }
  19.  
  20. int
  21. set_interface_attribs (int fd, int speed, int parity)
  22. {
  23.         struct termios tty;
  24.         memset (&tty, 0, sizeof tty);
  25.         if (tcgetattr (fd, &tty) != 0)
  26.         {
  27.                 printf ("error %d from tcgetattr", errno);
  28.                 return -1;
  29.         }
  30.  
  31.         cfsetospeed (&tty, speed);
  32.         cfsetispeed (&tty, speed);
  33.  
  34.         tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8| BRKINT;     // 8-bit chars
  35.         // disable IGNBRK for mismatched speed tests; otherwise receive break
  36.         // as \000 chars
  37.         tty.c_iflag &= ~IGNBRK;         // ignore break signal
  38.         tty.c_lflag = 0;                // no signaling chars, no echo,
  39.                                         // no canonical processing
  40.         tty.c_oflag = 0;                // no remapping, no delays
  41.         tty.c_cc[VMIN]  = 0;            // read doesn't block
  42.         tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
  43.  
  44.         tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  45.  
  46.         tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
  47.                                         // enable reading
  48.         tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
  49.         tty.c_cflag |= parity;
  50.         tty.c_cflag &= ~CSTOPB;
  51.         tty.c_cflag &= ~CRTSCTS;
  52.  
  53.         if (tcsetattr (fd, TCSANOW, &tty) != 0)
  54.         {
  55.                 printf ("error %d from tcsetattr", errno);
  56.                 return -1;
  57.         }
  58.         return 0;
  59. }
  60.  
  61. void
  62. set_blocking (int fd, int should_block)
  63. {
  64.         struct termios tty;
  65.         memset (&tty, 0, sizeof tty);
  66.         if (tcgetattr (fd, &tty) != 0)
  67.         {
  68.                 printf ("error %d from tggetattr", errno);
  69.                 return;
  70.         }
  71.  
  72.         tty.c_cc[VMIN]  = should_block ? 1 : 0;
  73.         tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
  74.  
  75.         if (tcsetattr (fd, TCSANOW, &tty) != 0)
  76.                 printf ("error %d setting term attributes", errno);
  77. }
  78.  
  79.  
  80. void main(){
  81.  
  82. char portname[] = "/dev/ttyO1";
  83.  
  84.     signal(SIGINT, (__sighandler_t) sighandle);//abilitazione ricezione break
  85.  
  86. int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
  87. int value=0;
  88. if (fd < 0)
  89. {
  90.         printf ("error %d opening %s: %s", errno, portname, strerror (errno));
  91.         return;
  92. }else{
  93. printf("init port\n");
  94. }
  95.  
  96. value=set_interface_attribs (fd, B19200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
  97. if(value==0)    printf("port opened ok\n");
  98. set_blocking (fd, 0);                // set no blocking
  99. printf("port no blocking ok\n");
  100. write (fd, "hello!\n", 7);           // send 7 character greeting
  101. char buf [100];
  102. int n = 0;
  103. int esci=0;
  104. int i=0;
  105. while(esci==0){
  106. n=read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read
  107. for(i=0;i<n;i++){
  108. printf("%c",buf[i]);
  109. if(buf[i]=='*')
  110. esci=1;
  111. }
  112. }
  113. printf("\n");
  114. close(fd);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement