Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.44 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <termios.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5.  
  6. int main()
  7. {
  8.   int fd;
  9.   struct termios options;
  10.   int n;
  11.   char buff[15]="";
  12.        
  13.   /* Open Port */
  14.   fd = open("/dev/ttySAC1", O_RDWR | O_NOCTTY | O_NDELAY);  /* <--- YOUR PORT */
  15.  
  16.        
  17.   if(fd == -1) {
  18.                   printf("ERROR Open Serial Port!");
  19.                 }
  20.              
  21.  
  22.   /* Serial Configuration */
  23.   tcgetattr(fd, &options);   // Get Current Config
  24.   cfsetispeed(&options, B9600); // Set Baud Rate
  25.   cfsetospeed(&options, B9600);
  26.   options.c_cflag = (options.c_cflag & ~CSIZE) | CS8;
  27.   options.c_iflag =  IGNBRK;
  28.   options.c_lflag = 0;
  29.   options.c_oflag = 0;
  30.   options.c_cflag |= CLOCAL | CREAD;
  31.   options.c_cc[VMIN] = 1;
  32.   options.c_cc[VTIME] = 5;
  33.   options.c_iflag &= ~(IXON|IXOFF|IXANY);
  34.   options.c_cflag &= ~(PARENB | PARODD);
  35.  
  36.   /* Save The Configure */
  37.   tcsetattr(fd, TCSANOW, &options);
  38.  
  39.   /* Flush the input (read) buffer */
  40. tcflush(fd,TCIOFLUSH);
  41. printf ("\ntest point 1\n");                      
  42. write(fd,"R",4);
  43. printf ("\ntest point 2\n");    
  44.  /* Flush the input (read) buffer */
  45. printf ("\ntest point 3\n");  
  46. tcflush(fd,TCIOFLUSH);
  47. printf ("\ntest point 4\n");  
  48. write(fd,"S",4);
  49. usleep(50);
  50. printf ("\ntest point 5\n");
  51. read(fd,buff,10);   // Read Data From Serial Port
  52. printf ("\ntest point 6\n : %s : \n",buff);   // Print Out
  53.  
  54. close(fd); // Close Port
  55.   return 0; // End Program
  56. }