Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello, I'm new to this one, please help me out, I'm trying read serial COM port and want to write that received data to file, now its writing only one ...
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    Post i want to write data to text file which is coming on serial port infintely

    Hello,

    I'm new to this one, please help me out, I'm trying read serial COM port and want to write that received data to file, now its writing only one sentence, but i want to write full file which coming on serial port, as i'm sending file from hyper terminal and reading on linux pc, If i put while loop its not writing anything,

    without while loop its writing only one line and if send big file then application terminates and then writes to file.

    But i need do write any size which coming on serial port.

    Finally i want write full file which is coming on hyper terminal, after writing the file it has wait for next data. This is my code, please check it and please give me suggestions

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

    #define BAUDRATE B115200
    #define COMPORT "/dev/ttyS0"
    #define _POSIX_SOURCE 1 /* POSIX compliant source */
    #define FALSE 0
    #define TRUE 1


    #define BUFSIZE (1024*1024)
    volatile int STOP=FALSE;

    main()
    {
    int fd,c, res;
    FILE *fp;
    struct termios oldtio,newtio;
    char buf[255];

    fd = open(COMPORT, O_RDWR | O_NOCTTY );
    if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyS0 - ");
    return 1;
    }

    fp = fopen("/usr/akash/gcc/output/Client.txt","a+");
    assert(fp != NULL);

    tcgetattr(fd,&oldtio); /* save current port settings */

    //bzero(&newtio, sizeof(newtio));
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;

    /* set input mode (non-canonical, no echo,...) */
    newtio.c_lflag = 0;
    newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
    newtio.c_cc[VMIN] = 0; /* blocking read until 0 chars received */

    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);


    while (1) { /* loop for input */
    res = read(fd,buf,BUFSIZE);
    printf(":%s", buf);
    fwrite(buf, sizeof(char), strlen(buf), fp); // i want to write buf to text file
    if (buf[0]=='z') STOP=TRUE;
    }
    tcsetattr(fd,TCSANOW,&oldtio);
    }






    Regards
    Akash

  2. #2
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    You are reading raw data, and assuming that the received buffer will be string formatted text (null-terminated character array). You cannot use strlen() to determine how much data is in the received buffer. The byte count of received data is returned by read() , but you disregard it. There is nothing to terminate your input while() loop.
    --- rod.

    PS. Please post code in [CODE ][/CODE] tags to preserve formatting and make it readable.
    Stuff happens. Then stays happened.

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5
    so please help how to terminate while loop and then write to file...

    but i have to wait for next data..

    Please ...

  4. #4
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    So you want to get one file, close it off, and then start writing a new file? That sounds like you need a loop nested within another loop: one that iterates over the content of a single file, and the outer loop iterating indefinitely over all received files.
    How will you identify the end-of-file condition? Whatever that state is, should be the state used to stop the loop. Right now, you have a loop that runs while a constant (1) is non-zero; forever. In the outer loop, you will have to close the file, and then open a new file. It will be up to your application to determine how such files get named.
    --- rod.
    Stuff happens. Then stays happened.

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    i want to write data to text file which is coming on serial port infintely

    Hey i dont want to creat new file....... I want to write received content to the same file infinetly ...

    Can you give some example...

    if you have any sample code... please send it to

    ag.guttedar@gmail.com

  6. #6
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    Okay, but you will still need to close the file. I suggest not using fopen()/fwrite(), etc but rather stick with the basic open()/read()/write()/close(). As you receive buffers of data via read() from the serial port, simply perform the complementary write() to the output file. Use the size of the buffer returned by the read() call.

    At some point, you will have to terminate the program, and close the file, otherwise the last data written make not get flushed from OS buffers and written to disk. How will you determine when that time has arrived?
    Code:
       fp = open( argv[1], O_RDWR );
    
       while( ( res = read(fd,buf,BUFSIZE) ) ) > 0) { /* loop for input */
          write(fp, buf, res );                 // i want to write buf to text file 
          write( stdout, buf, res );    /* only a good idea for text */
       }
       close( fp );
    --- rod.
    Stuff happens. Then stays happened.

  7. #7
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Post

    I suppose you should use open syscall for your '.txt' file with O_SYNC flag. Then any write call will cause actual writing data to file.

  8. #8
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    Re

    I'm not getting exactly what to do... Hey please give some example with code.. ....

  9. #9
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5
    Thanks theNbomr,

    But one thing am not getting... can i use normal WRITE api to file write...?

    i thought write is used to write to port not to file, as i read data from serial port by READ api..

    Any how I will try it... Hope it works

  10. #10
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    why not just do it in the shell?

    cat /dev/ttyS0 > captured.txt



    unless you are expecting binary data and might get 0x04 (e.g. ^D) this will run forever. if you need it to not buffer, can do it in a shell and use stty to make the output -obuff

    or can redirect the cat output to stderr which is already unbuffered - should be something like:

    cat /dev/ttyS0 1>&2 > capture.txt - or something much like that, can't test just at the moment.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...