Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2012
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/time.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <termios.h>
  12.  
  13. #define BAUD_RATE B9600
  14.  
  15. int main (void)
  16. {
  17.     fd_set read_flags,write_flags; // you know what these are
  18.     struct timeval waitd;
  19.     int thefd;             // The socket
  20.     char outbuff[512];     // Buffer to hold outgoing data
  21.     char inbuff[512];      // Buffer to read incoming data into
  22.     int err;          // holds return values
  23.     struct termios options;
  24.  
  25.     memset(&outbuff,0,sizeof(outbuff)); // memset used for portability
  26.     //opening port
  27.     thefd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY | O_ASYNC);
  28.     if (thefd == -1) {
  29.         // could not open port
  30.         fprintf(stderr,"open_port: Unable to open %s\n", "/dev/ttyS0");
  31.     } else {
  32.         fcntl(thefd, F_SETFL, O_NDELAY);
  33.         tcgetattr(thefd, &options);
  34.         // BAUD rate
  35.         cfsetispeed(&options, BAUD_RATE);
  36.         cfsetospeed(&options, BAUD_RATE);
  37.         // enable the receiver and set local mode...
  38.         options.c_cflag |= (CLOCAL | CREAD);
  39.  
  40.         // no parity (8 in 1)
  41.         options.c_cflag &= ~PARENB;
  42.         options.c_cflag &= ~CSTOPB;
  43.         options.c_cflag &= ~CSIZE;
  44.         options.c_cflag |= CS8;
  45.  
  46.         // no flow controll
  47.         //options.c_cflag &= ~CRTSCTS;    /* Also called CRTSCTS */
  48.  
  49.         // apply all changes immediately
  50.         tcsetattr(thefd, TCSANOW, &options);
  51.     }
  52.  
  53.     strcat(outbuff,"jarjam\n"); //Add the string jarjam to the output
  54.     //buffer
  55.     while(1) {
  56.         waitd.tv_sec = 1;     // Make select wait up to 1 second for data
  57.         waitd.tv_usec = 0;    // and 0 milliseconds.
  58.         FD_ZERO(&read_flags); // Zero the flags ready for using
  59.         FD_ZERO(&write_flags);
  60.         FD_SET(thefd, &read_flags);
  61.         if(strlen(outbuff)!=0) FD_SET(thefd, &write_flags);
  62.         err=select(thefd+1, &read_flags,&write_flags,
  63.                 (fd_set*)0,&waitd);
  64.         if(err < 0) continue;
  65.         if(FD_ISSET(thefd, &read_flags)) { //Socket ready for reading
  66.             FD_CLR(thefd, &read_flags);
  67.             memset(&inbuff,0,sizeof(inbuff));
  68.             if (read(thefd, inbuff, sizeof(inbuff)-1) <= 0) {
  69.                 close(thefd);
  70.                 break;
  71.             }
  72.             else printf("%s",inbuff);
  73.         }
  74.         if(FD_ISSET(thefd, &write_flags)) { //Socket ready for writing
  75.             FD_CLR(thefd, &write_flags);
  76.             write(thefd,outbuff,strlen(outbuff));
  77.             memset(&outbuff,0,sizeof(outbuff));
  78.         }
  79.         // now the loop repeats over again
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement