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]=10*SERIAL_TIMEOUT_SEC; //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 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;
- else DEBUG_SERIAL(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; //index of the serDataBuf
- int remainingBytes=toReadBytes, readBytes=0, totalReadBytes=0;
- DEBUG_SERIAL(printf("SERIAL: DATA will read DATA bytes %d\n",toReadBytes));
- ch_C_cc(VMIN,0);
- while (remainingBytes>0){
- 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)) {
- DEBUG_SERIAL(printf("SERIAL: No DATA have been read. Prob Timeout @ byte %d. exiting fn\n",totalReadBytes));
- ch_C_cc(VMIN,1);
- return SERIAL_TIMEOUT;
- } 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