LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Embedded & Single-board computer (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/)
-   -   read() from ttyS1 issue while write() is Ok (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/read-from-ttys1-issue-while-write-is-ok-893438/)

japonomatj 07-24-2011 07:47 AM

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?

Code:

        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>
        #include <termios.h>
        #include <stdio.h>
        #include <unistd.h>


        /* baudrate settings are defined in <asm/termbits.h>, which is
        included by <termios.h> */
        #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\n", fcntl(fd, F_SETFL, 0));

        while (STOP==FALSE) { 

            printf("Now reading from port:\n");

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

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



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

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

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



All times are GMT -5. The time now is 03:49 PM.