Linux - reading from serial port error in C (Resource temporarily unavaliable) -


i trying read data serial port using usb/rs232 converter. sending data atmega , received in minicom. trying read sth in program ends error "resource temporarily unavailable". know caused o_ndelay, removing gives me plenty of empty messages isn't good.

actually, want achieve program, every second transmit char atmega , wait response. depending on response different actions. after few unanswered transmissions program indicate error communication.

but i'd @ least receive properly.

here code:

#include <unistd.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #include <stdio.h> #include <string.h>   int fd; //file descriptor int bytes; // bytes read  int portopen(void) {     fd = open("/dev/ttyusb1", o_rdwr | o_noctty | o_ndelay);     if(fd < 0)     {         //opening error         printf("port opening error\r\n");     }     else     {         printf("port opened\r\n");         //port config         struct termios options;         tcgetattr(fd, &options);          cfsetispeed(&options, b9600);         cfsetospeed(&options, b9600);          options.c_cflag &= ~parenb;         options.c_cflag &= ~cstopb;         options.c_cflag &= ~csize;         options.c_cflag |= cs8;         options.c_cflag |= clocal;         options.c_cflag |= cread;         options.c_cflag &= ~crtscts;          options.c_cc[vmin]   =  0;                           options.c_cc[vtime]  =  5;                            options.c_lflag &=  ~(icanon | echo | echoe | isig);         //modifying c_iflag bitwise or , and         options.c_iflag &=  ~(icrnl | inlcr | igncr | iuclc);         options.c_iflag &=  ~(ixon | ixoff | ixany);         //modifying c_oflag bitwise or , and         options.c_oflag &=  ~opost;         tcflush(fd, tciflush);         //setting new settings port         tcsetattr(fd, tcsanow, &options);     }     return 1;  }  int portread(void) {     int n = 0, spot = 0;     char buf = '\0';      /* whole response*/     char response[1024];     memset(response, '\0', sizeof response);      {         n = read( fd, &buf, 1 );         sprintf( &response[spot], "%c", buf );         spot += n;     } while( buf != '\r' && n > 0);      if (n < 0)      {         printf("error reading: %s\r\n",strerror(errno));     }     else if (n == 0)      {         printf("read nothing!\r\n");     }     else      {         printf("response: %s", response);     }     return 1; }  int main(void) {     portopen();     int j;     for(j=0; j<100; j++) //just testing     {         portread();     }   return 0;    } 


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -