Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int open_com_device() {
- //based on: http://www.cmrr.umn.edu/~strupp/serial.html#2_1
- struct termios serial_opts;
- /* Open modem device for reading and writing and not as controlling tty
- because we don't want to get killed if linenoise sends CTRL-C.*/
- fd = open("/dev/ttymxc0", O_RDWR | O_NOCTTY /*| O_NDELAY*/ );
- if (fd <0) {
- DEBUG_SERIAL(printf("error in opening serial device \n"));
- exit(-1);
- }
- tcgetattr(fd, &serial_opts);
- cfsetispeed(&serial_opts, ACM_BAUD);
- cfsetospeed(&serial_opts, ACM_BAUD);
- serial_opts.c_cflag |= ( CLOCAL | CREAD );
- //next 4 options are for 8n1
- serial_opts.c_cflag &= ~PARENB;
- serial_opts.c_cflag &= ~CSTOPB;
- serial_opts.c_cflag &= ~CSIZE;
- serial_opts.c_cflag |= CS8;
- //enable raw input
- serial_opts.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
- //disable "&= ~(..." software flow control. ( To enable |= and remove the ~ )
- serial_opts.c_iflag &= ~( IXON | IXOFF | IXANY );
- //ignore parity and CR LF chars
- serial_opts.c_iflag |= ( IGNPAR | ICRNL );
- //enable raw output
- serial_opts.c_oflag &= ~OPOST;
- //c_cc
- serial_opts.c_cc[VTIME]=60; //in deci-seconds
- serial_opts.c_cc[VMIN]=1;
- if (tcsetattr(fd, TCSANOW, &serial_opts)==-1){
- DEBUG_SERIAL(printf("SERIAL: tcsetattr set failed!\n"));
- }
- return SUCCESS;
- }
- int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y){
- //Subtract the `struct timeval' values X and Y,
- //storing the result in RESULT.
- //Return 1 if the difference is negative, otherwise 0.
- int nsec=0;
- // Perform the carry for the later subtraction by updating y.
- if (x->tv_usec < y->tv_usec) {
- nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
- y->tv_usec -= 1000000 * nsec;
- y->tv_sec += nsec;
- }
- if (x->tv_usec - y->tv_usec > 1000000) {
- nsec = (y->tv_usec - x->tv_usec) / 1000000;
- y->tv_usec += 1000000 * nsec;
- y->tv_sec -= nsec;
- }
- // Compute the time remaining to wait. tv_usec is certainly positive.
- result->tv_sec = x->tv_sec - y->tv_sec;
- result->tv_usec = x->tv_usec - y->tv_usec;
- // Return 1 if result is negative.
- return x->tv_sec < y->tv_sec;
- }
- int asciiToHex(char *aChar){
- // Convert ASCII HEX values, to plain HEX
- if ((*aChar & 0xF0) == 0x30)
- *aChar = *aChar - 0x30; // Number
- else if (*aChar>='A' && *aChar<='F')
- *aChar = *aChar - 0x37; // Upper-case letter
- else if (*aChar>='a' && *aChar<='f')
- *aChar = *aChar - 0x57; // Lower-case letter
- else {
- DEBUG_SERIAL(printf("Invalid hex stream (nibble: 0x%0X)\n",*aChar));
- pthread_mutex_unlock(&serial_lock_mutex);
- return ERROR;
- }
- }
- int ch_C_cc(const unsigned int c_cc_arrayIdx, const unsigned int value){
- //change termios c_cc[VTIME] or c_cc[VMIN]
- struct termios opts;
- if (tcgetattr(fd, &opts)==-1){
- DEBUG_SERIAL(printf("SERIAL: tcgetattr set failed errno %d, %s!\n",errno,strerror(errno)));
- return -1;
- }
- if (c_cc_arrayIdx==VTIME) opts.c_cc[VTIME]=10*value; //in deci-seconds
- else if (c_cc_arrayIdx==VMIN) opts.c_cc[VMIN]=value;
- DEBUG_SERIAL(else printf("SERIAL: c_cc invalid array index!\n"));
- if (tcsetattr(fd, TCSANOW, &opts)==-1){
- DEBUG_SERIAL(printf("SERIAL: tcsetattr set failed errno %d, %s!\n",errno,strerror(errno)));
- return -1;
- }
- return 0;
- }
- int readComData(const int toReadBytes, char *serDataBuf){
- unsigned int idx=0,timeoutCounter=0; //index of the serDataBuf
- int remainingBytes=toReadBytes, readBytes=0, totalReadBytes=0;
- struct timeval difftime,begintime,endtime;
- DEBUG_SERIAL(printf("SERIAL: DATA will read DATA bytes %d\n",toReadBytes));
- ch_C_cc(VMIN,0);
- while (remainingBytes>0){
- gettimeofday(&begintime);
- readBytes=read(fd,&serDataBuf[totalReadBytes],remainingBytes);
- if ((readBytes<remainingBytes) && (readBytes!=0)){
- DEBUG_SERIAL(printf("SERIAL: DATA read %d bytes and a total of %d .\n",readBytes,totalReadBytes));
- remainingBytes-=readBytes;
- totalReadBytes+=readBytes;
- } else if (readBytes==-1) {
- DEBUG_SERIAL(printf("SERIAL: errno num: %d, string: %s\n",errno,strerror(errno)));
- ch_C_cc(VMIN,1);
- return ERROR;
- } else if ((readBytes==0) && (totalReadBytes<toReadBytes)) {
- gettimeofday(&endtime);
- if (timeval_subtract(&difftime,&endtime,&begintime)==1) {
- printf("SERIAL: time diff was negative\n");
- printf("SERIAL: time diff is tv_sec=%d , tv_usec=%d\n",difftime.tv_sec,difftime.tv_usec);
- } else {
- printf("SERIAL: time diff is tv_sec=%d , tv_usec=%d\n",difftime.tv_sec,difftime.tv_usec);
- }
- DEBUG_SERIAL(printf("SERIAL: No DATA have been read. Timeout @ byte %d, timeout counter %d.\n",totalReadBytes,timeoutCounter));
- if (timeoutCounter>MAX_RETRIES) {
- ch_C_cc(VMIN,1);
- return SERIAL_TIMEOUT;
- } else {
- timeoutCounter++;
- }
- } else {
- DEBUG_SERIAL(printf("SERIAL: All remaining DATA have been read %d .\n",toReadBytes));
- }
- }//end while
- ch_C_cc(VMIN,1);
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement