Advertisement
synthnassizer

readComData2

Jul 11th, 2013
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.06 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.         fd = open("/dev/ttymxc0", O_RDWR | O_NOCTTY /*| O_NDELAY*/ );
  8.         if (fd <0) {
  9.                 DEBUG_SERIAL(printf("error in opening serial device \n"));
  10.                 exit(-1);
  11.         }
  12.         tcgetattr(fd, &serial_opts);
  13.         cfsetispeed(&serial_opts, ACM_BAUD);
  14.         cfsetospeed(&serial_opts, ACM_BAUD);
  15.         serial_opts.c_cflag |= ( CLOCAL | CREAD );
  16.         //next 4 options are for 8n1
  17.         serial_opts.c_cflag &= ~PARENB;
  18.         serial_opts.c_cflag &= ~CSTOPB;
  19.         serial_opts.c_cflag &= ~CSIZE;
  20.         serial_opts.c_cflag |= CS8;
  21.         //enable raw input
  22.         serial_opts.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
  23.         //disable "&= ~(..." software flow control. ( To enable |= and remove the ~ )
  24.         serial_opts.c_iflag &= ~( IXON | IXOFF | IXANY );
  25.         //ignore parity and CR LF chars
  26.         serial_opts.c_iflag |= ( IGNPAR | ICRNL );
  27.         //enable raw output
  28.         serial_opts.c_oflag &= ~OPOST;
  29.         //c_cc
  30.         serial_opts.c_cc[VTIME]=60; //in deci-seconds
  31.         serial_opts.c_cc[VMIN]=1;
  32.         if (tcsetattr(fd, TCSANOW, &serial_opts)==-1){
  33.                 DEBUG_SERIAL(printf("SERIAL: tcsetattr set failed!\n"));
  34.         }
  35.         return SUCCESS;
  36. }
  37.  
  38. int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y){
  39. //Subtract the `struct timeval' values X and Y,
  40. //storing the result in RESULT.
  41. //Return 1 if the difference is negative, otherwise 0.
  42.     int nsec=0;
  43.  
  44.     // Perform the carry for the later subtraction by updating y.
  45.     if (x->tv_usec < y->tv_usec) {
  46.         nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  47.         y->tv_usec -= 1000000 * nsec;
  48.         y->tv_sec += nsec;
  49.     }
  50.     if (x->tv_usec - y->tv_usec > 1000000) {
  51.         nsec = (y->tv_usec - x->tv_usec) / 1000000;
  52.         y->tv_usec += 1000000 * nsec;
  53.         y->tv_sec -= nsec;
  54.     }
  55.  
  56.     // Compute the time remaining to wait. tv_usec is certainly positive.
  57.     result->tv_sec = x->tv_sec - y->tv_sec;
  58.     result->tv_usec = x->tv_usec - y->tv_usec;
  59.  
  60.     // Return 1 if result is negative.
  61.     return x->tv_sec < y->tv_sec;
  62. }
  63.  
  64. int asciiToHex(char *aChar){
  65. // Convert ASCII HEX values, to plain HEX
  66.     if ((*aChar & 0xF0) == 0x30)
  67.         *aChar = *aChar - 0x30; // Number
  68.     else if (*aChar>='A' && *aChar<='F')
  69.         *aChar = *aChar - 0x37; // Upper-case letter
  70.     else if (*aChar>='a' && *aChar<='f')
  71.         *aChar = *aChar - 0x57; // Lower-case letter
  72.     else {
  73.         DEBUG_SERIAL(printf("Invalid hex stream (nibble: 0x%0X)\n",*aChar));
  74.         pthread_mutex_unlock(&serial_lock_mutex);
  75.         return ERROR;
  76.     }
  77. }
  78.  
  79. int ch_C_cc(const unsigned int c_cc_arrayIdx, const unsigned int value){
  80. //change termios c_cc[VTIME] or c_cc[VMIN]
  81.     struct termios opts;
  82.  
  83.     if (tcgetattr(fd, &opts)==-1){
  84.         DEBUG_SERIAL(printf("SERIAL: tcgetattr set failed errno %d, %s!\n",errno,strerror(errno)));
  85.         return -1;
  86.     }
  87.  
  88.     if (c_cc_arrayIdx==VTIME)       opts.c_cc[VTIME]=10*value; //in deci-seconds
  89.     else if (c_cc_arrayIdx==VMIN)   opts.c_cc[VMIN]=value;
  90.     DEBUG_SERIAL(else                   printf("SERIAL: c_cc invalid array index!\n"));
  91.  
  92.     if (tcsetattr(fd, TCSANOW, &opts)==-1){
  93.         DEBUG_SERIAL(printf("SERIAL: tcsetattr set failed errno %d, %s!\n",errno,strerror(errno)));
  94.         return -1;
  95.     }
  96.     return 0;
  97. }
  98.  
  99. int readComData(const int toReadBytes, char *serDataBuf){
  100.     unsigned int idx=0,timeoutCounter=0; //index of the serDataBuf
  101.     int remainingBytes=toReadBytes, readBytes=0, totalReadBytes=0;
  102.     struct timeval difftime,begintime,endtime;
  103.  
  104.     DEBUG_SERIAL(printf("SERIAL: DATA will read DATA bytes %d\n",toReadBytes));
  105.     ch_C_cc(VMIN,0);
  106.     while (remainingBytes>0){
  107.         gettimeofday(&begintime);
  108.         readBytes=read(fd,&serDataBuf[totalReadBytes],remainingBytes);
  109.         if ((readBytes<remainingBytes) && (readBytes!=0)){
  110.             DEBUG_SERIAL(printf("SERIAL: DATA read %d bytes and a total of %d .\n",readBytes,totalReadBytes));
  111.             remainingBytes-=readBytes;
  112.             totalReadBytes+=readBytes;
  113.         } else if (readBytes==-1) {
  114.             DEBUG_SERIAL(printf("SERIAL: errno num: %d, string: %s\n",errno,strerror(errno)));
  115.             ch_C_cc(VMIN,1);
  116.             return ERROR;
  117.         } else if ((readBytes==0) && (totalReadBytes<toReadBytes)) {
  118.             gettimeofday(&endtime);
  119.             if (timeval_subtract(&difftime,&endtime,&begintime)==1) {
  120.                 printf("SERIAL: time diff was negative\n");
  121.                 printf("SERIAL: time diff is tv_sec=%d , tv_usec=%d\n",difftime.tv_sec,difftime.tv_usec);
  122.             } else {
  123.                 printf("SERIAL: time diff is tv_sec=%d , tv_usec=%d\n",difftime.tv_sec,difftime.tv_usec);
  124.             }
  125.             DEBUG_SERIAL(printf("SERIAL: No DATA have been read. Timeout @ byte %d, timeout counter %d.\n",totalReadBytes,timeoutCounter));
  126.             if (timeoutCounter>MAX_RETRIES) {
  127.                 ch_C_cc(VMIN,1);
  128.                 return SERIAL_TIMEOUT;
  129.             } else {
  130.                 timeoutCounter++;
  131.             }
  132.         } else {
  133.             DEBUG_SERIAL(printf("SERIAL: All remaining DATA have been read %d .\n",toReadBytes));
  134.         }
  135.     }//end while
  136.     ch_C_cc(VMIN,1);
  137.  
  138.     return SUCCESS;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement