read() from ttyS1 issue while write() is Ok

Hi! I’ve got a problem with reading prom serial port, when I run this code on Digi ConnectCore Wi-9c. But writing to serial port is Ok. By the way, when I’m running this code on “full” Linux it is working ok - I can read and write to serial without mistakes. Could anyone help me?

 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 


        /* baudrate settings are defined in , which is
        included by  */
        #define BAUDRATE B38400            
        /* change this definition for the correct port */
        #define MODEMDEVICE "/dev/ttyS0"
        #define _POSIX_SOURCE 1 /* POSIX compliant source */

        #define FALSE 0
        #define TRUE 1

        volatile int STOP=FALSE; 

        int main()
        {
          int fd, res;
          struct termios oldtio,newtio;
          char buf[255];
        /* 
          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(MODEMDEVICE, O_RDWR | O_NOCTTY ); 
         
         
         if (fd <0) {perror(MODEMDEVICE); exit(-1); }
         
         
         
         tcgetattr(fd,&oldtio); /* save current serial port settings */
         bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
        
         newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
         
         newtio.c_iflag = IGNPAR | ICRNL;
         
         newtio.c_oflag = 0;
         
         newtio.c_lflag = ICANON;
         
         newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */ 
         newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
         newtio.c_cc[VERASE]   = 0;     /* del */
         newtio.c_cc[VKILL]    = 0;     /* @ */
         newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
         newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
         newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
         newtio.c_cc[VSWTC]    = 0;     /* '\0' */
         newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */ 
         newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
         newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
         newtio.c_cc[VEOL]     = 0;     /* '\0' */
         newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
         newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
         newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
         newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
         newtio.c_cc[VEOL2]    = 0;     /* '\0' */
        
        /* 
          now clean the modem line and activate the settings for the port
        */
         tcflush(fd, TCIFLUSH);
         tcsetattr(fd,TCSANOW,&newtio);
        
         char Key;

         char input[255];

         printf("result:%i
", fcntl(fd, F_SETFL, 0));

         while (STOP==FALSE) {   

            printf("Now reading from port:
");

           	res = read(fd,buf,255);
            
            printf("Data from port
");
            buf[res]=0;             // set end of string, so we can printf 
            printf(":%s:%d
", buf, res);

            if (buf[0]=='z') STOP=TRUE;



            printf("Now waiting for char
");
            
            gets(input);//scanf("%c", &Key);
            Key = input[0];
            if (1)  //if a key was hit
            {
              printf("Char is:");
                  printf("%c
", Key);//fputc((int) Key,output);

              printf("Writing char to uart:
");
                  write(fd,&Key,1);          //write 1 byte to the port
 	        }

         }
         /* restore the old port settings */
         tcsetattr(fd,TCSANOW,&oldtio);
         close(fd);
         return 0;
        }

Hi Roman,

The below link helped me in programming serial port…
http://www.easysw.com/~mike/serial/serial.html#3_1

It may help you also…