LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices

Reply
 
Search this Thread
Old 07-24-2011, 07:47 AM   #1
japonomatj
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
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;
        }

Last edited by japonomatj; 07-24-2011 at 07:49 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - Newbie 16 09-01-2010 10:07 AM
Read Write access to a iso9660 filesystem..mount a .iso image as read write ceazar123 Linux - General 2 08-26-2010 04:32 PM
[SOLVED] Serial Programming, Write-Read Issue a5k3r Programming 3 08-09-2010 11:49 AM
Samba issue on Lenny with new kernel 2.6.26.1.486 no read write access ? phil81 Debian 25 03-26-2009 03:35 PM
ext hdd mounting read/write issue Jestrik Linux - Hardware 1 06-12-2004 09:46 AM


All times are GMT -5. The time now is 09:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration