Advertisement
synthnassizer

readComData-noSelectFn

Jul 9th, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. int open_com_device() {
  2. //based on: http://www.cmrr.umn.edu/~strupp/serial.html#2_1
  3.     struct termios serial_opts;
  4.  
  5.     /* Open modem device for reading and writing and not as controlling tty
  6.     because we don't want to get killed if linenoise sends CTRL-C.*/
  7.  
  8.  
  9.     fd = open("/dev/ttymxc0", O_RDWR | O_NOCTTY /*| O_NDELAY*/ );
  10.  
  11.     if (fd <0) {
  12.         DEBUG_SERIAL(printf("error in opening serial device \n"));
  13.         exit(-1);
  14.     }
  15.  
  16.     tcgetattr(fd, &serial_opts);
  17.     cfsetispeed(&serial_opts, ACM_BAUD);
  18.     cfsetospeed(&serial_opts, ACM_BAUD);
  19.     serial_opts.c_cflag |= ( CLOCAL | CREAD );
  20.     //next 4 options are for 8n1
  21.     serial_opts.c_cflag &= ~PARENB;
  22.     serial_opts.c_cflag &= ~CSTOPB;
  23.     serial_opts.c_cflag &= ~CSIZE;
  24.     serial_opts.c_cflag |= CS8;
  25.     //enable raw input
  26.     serial_opts.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
  27.     //disable "&= ~(..." software flow control. ( To enable |= and remove the ~ )
  28.     serial_opts.c_iflag &= ~( IXON | IXOFF | IXANY );
  29.     //ignore parity and CR LF chars
  30.     serial_opts.c_iflag |= ( IGNPAR | ICRNL );
  31.     //enable raw output
  32.     serial_opts.c_oflag &= ~OPOST;
  33.     //c_cc
  34.     serial_opts.c_cc[VTIME]=10*SERIAL_TIMEOUT_SEC; //in deci-seconds
  35.     serial_opts.c_cc[VMIN]=1;
  36.     if (tcsetattr(fd, TCSANOW, &serial_opts)==-1){
  37.         DEBUG_SERIAL(printf("SERIAL: tcsetattr set failed!\n"));
  38.     }
  39.     return SUCCESS;
  40. }
  41.  
  42. int ch_C_cc(const unsigned int c_cc_arrayIdx, const unsigned int value){
  43. //change termios c_cc[VTIME] or c_cc[VMIN]
  44.     struct termios opts;
  45.  
  46.     if (tcgetattr(fd, &opts)==-1){
  47.         DEBUG_SERIAL(printf("SERIAL: tcgetattr set failed errno %d, %s!\n",errno,strerror(errno)));
  48.         return -1;
  49.     }
  50.  
  51.     if (c_cc_arrayIdx==VTIME)   opts.c_cc[VTIME]=10*value; //in deci-seconds
  52.     else if (c_cc_arrayIdx==VMIN)   opts.c_cc[VMIN]=value;
  53.     else DEBUG_SERIAL(printf("SERIAL: c_cc invalid array index!\n"));
  54.  
  55.     if (tcsetattr(fd, TCSANOW, &opts)==-1){
  56.         DEBUG_SERIAL(printf("SERIAL: tcsetattr set failed errno %d, %s!\n",errno,strerror(errno)));
  57.         return -1;
  58.     }
  59.     return 0;
  60. }
  61.  
  62. int readComData(const int toReadBytes, char *serDataBuf){
  63.     unsigned int idx=0; //index of the serDataBuf
  64.     int remainingBytes=toReadBytes, readBytes=0, totalReadBytes=0;
  65.     DEBUG_SERIAL(printf("SERIAL: DATA will read DATA bytes %d\n",toReadBytes));
  66.  
  67.     ch_C_cc(VMIN,0);
  68.     while (remainingBytes>0){
  69.         readBytes=read(fd,&serDataBuf[totalReadBytes],remainingBytes);
  70.         if ((readBytes<remainingBytes) && (readBytes!=0)){
  71.             DEBUG_SERIAL(printf("SERIAL: DATA read %d bytes and a total of %d .\n",readBytes,totalReadBytes));
  72.             remainingBytes-=readBytes;
  73.             totalReadBytes+=readBytes;
  74.         } else if (readBytes==-1) {
  75.             DEBUG_SERIAL(printf("SERIAL: errno num: %d, string: %s\n",errno,strerror(errno)));
  76.             ch_C_cc(VMIN,1);
  77.             return ERROR;
  78.         } else if ((readBytes==0) && (totalReadBytes<toReadBytes)) {
  79.             DEBUG_SERIAL(printf("SERIAL: No DATA have been read. Prob Timeout @ byte %d. exiting fn\n",totalReadBytes));
  80.             ch_C_cc(VMIN,1);
  81.             return SERIAL_TIMEOUT;
  82.         } else {
  83.             DEBUG_SERIAL(printf("SERIAL: All remaining DATA have been read %d .\n",toReadBytes));
  84.         }
  85.     }//end while
  86.     ch_C_cc(VMIN,1);
  87.  
  88.     return SUCCESS;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement