Like Tree30Likes

Wirte and read serial port

This is a discussion on Wirte and read serial port within the C Programming forums, part of the General Programming Boards category; > unsigned char result[8],chout[8]; Did you change this? > for(i=0;i<8;i++) When you changed this?...

  1. #46
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > unsigned char result[8],chout[8];
    Did you change this?

    > for(i=0;i<8;i++)
    When you changed this?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #47
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    > unsigned char result[8],chout[8];
    Did you change this?

    > for(i=0;i<8;i++)
    When you changed this?
    Hye salem, actually this is my updated code

    Code:
     #include <stdio.h>   /* Standard input/output definitions */
     #include <string.h>  /* String function definitions */
     #include <unistd.h>  /* UNIX standard function definitions */
     #include <fcntl.h>   /* File control definitions */
     #include <errno.h>   /* Error number definitions */
     #include <termios.h> /* POSIX terminal control definitions */
    
     /*
      * 'open_port()' - Open serial port 1.
      *
      * Returns the file descriptor on success or -1 on error.
      */
    
     int open_port(void)
     {
       int fd;                                   /* File descriptor for the port */
    
       fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    
       if (fd < 0) {                                              /* Could not open the port */
         fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
                 strerror(errno));
       }
    
       return (fd);
     }
    
    void main()
    {
     int mainfd=0, i;                                            /* File descriptor */
     char cnt, result2;
     unsigned char result,chout;
     struct termios options;
     FILE *fp;
       
     mainfd = open_port();
    
     fcntl(mainfd, F_SETFL, FNDELAY);                  /* Configure port reading */
                                         /* Get the current options for the port */
     tcgetattr(mainfd, &options);
     cfsetispeed(&options, B19200);                 /* Set the baud rates to 19200 */
     cfsetospeed(&options, B19200);
        
                                       /* Enable the receiver and set local mode */
     options.c_cflag |= (CLOCAL | CREAD);
     options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
     options.c_cflag &= ~CSTOPB;
     options.c_cflag &= ~CSIZE;
     options.c_cflag |=  CS8;                              /* Select 8 data bits */
     options.c_cflag &= ~CRTSCTS;               /* Disable hardware flow control */  
     
                                     /* Enable data to be processed as raw input */
     options.c_lflag &= ~(ICANON | ECHO | ISIG);
           
                                            /* Set the new options for the port */
     tcsetattr(mainfd, TCSANOW, &options);
    
     result2 = tcsetattr(mainfd, TCSANOW, &options);
     if ( result2 == -1 ) { perror("error: tcsetattr"); }
     if ( result2 != 0 ) { fprintf(stderr,"error in tcsetattr, result = %d\n", result2); }
    
     result2 = write(mainfd,"\xF5\x23\x00\x00\x00\x00\x23\xF5",8);
     if ( result2 == -1 ) { perror("error: write"); }
     if ( result2 != 8 ) { fprintf(stderr,"error in write, result = %d\n", result2); }
      
     usleep(5000000);      /* wait for 5 second*/
    
     fp = fopen("results_eigenvalue", "w");   /*open and write into existing file*/
    
     for(i=0;i<111;i++)
    {
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result == -1 ) { perror("error: read"); }
     /*if ( result!= 1 ) { fprintf(stderr,"error in read, result = %d\n", result); }
       
     if (chout!= 0)*/
        fprintf(fp,"%02x ",chout); 
    
    }                                                /* Close the serial port */
      close(mainfd);
     }

  3. #48
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > fprintf(fp,"%02x ",chout);
    When you open the file in a text editor, look for horizontal scroll bars.

    You output everything on one line, and it's 333 characters long!

    Also, finish off with a
    fclose(fp);
    as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #49
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    > fprintf(fp,"%02x ",chout);
    When you open the file in a text editor, look for horizontal scroll bars.

    You output everything on one line, and it's 333 characters long!

    Also, finish off with a
    fclose(fp);
    as well.
    Actually my code works, my mistake because i was opening Cutecom while i executing the code. The result that i get is correct but not in a right order. Example:

    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a f5
    f5 23 00 c4 00 00 e7 f5 f5 00 00 00 28 04 b6 80 41 16 b9 c0 e1 1b 19 e8 61 21 3a d9 61 2e b9 5a 41 2f 31 56 a1 41 21 52 a1 42 30 16 e1 46 38 42 81 52 32 d9 61 65 2f e2 41 66 93 a2 a1 6a 0d a2 a1 6a ae 24 e1 81 25 a8 e1 82 19 88 c1 85 96 9d 41 86 2d 65 41 0d aa d4 82 16 a3 69 a2 19 1d 92 42 20 21 52 a2 20 2a 2a 22 21 90 d1 a2 2d 15 e7 a2 2f 97 91 22 35 1c 28 42 3a 8c 50 82 3d 12 66 82 43 12 cf e2 4d a3 e7 a2 5f 1a 8f 42 61 23 e6 22 61 ab a7 02 62 1f 8f e2 6a b2 61 a2 6c 20 8e 22 70 18 a1 82 70 b7 a2 a2 8a ae e5 42 00 00 00 00 00

    The BOLD part was suppose to be continue at the end line but unfortunately it jumps to the top. What's the problem? By the way, it is actually 207 byte data.

  5. #50
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    I've encounter another problem, as you can see in my code, i output my result into a new file. Assume it is a 8-byte file, so it content will be "F5 01 00 00 00 00 01 F5". I wanted to read it back and put it into an unsigned char together with the space, which means fully 100% same with the original data, and i wanted to check the fifth char ( for example) whether it is 01 or 00. How can i do that??

  6. #51
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    If your file contains "F5 01 00 00 00 00 01 F5" it contains 23 characters, not counting the end of line character. To read this information back you should try scanning in the values using the "%x" format specifier. By default the scanf() functions stop processing when they encounter a white space character.


    Now also posted here. Please note many people find cross posting to different forums rude, myself included, you really should find a forum and stick with that forum.


    Jim
    Marvin Gorres likes this.

  7. #52
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    If your file contains "F5 01 00 00 00 00 01 F5" it contains 23 characters, not counting the end of line character. To read this information back you should try scanning in the values using the "%x" format specifier. By default the scanf() functions stop processing when they encounter a white space character.


    Now also posted here. Please note many people find cross posting to different forums rude, myself included, you really should find a forum and stick with that forum.


    Jim
    Ok Jim. Sorry about that. I just though you all left already. hehe. Anyway, to read back my 207 byte of data from other file i used this code:

    Code:
    fp = fopen("results_eigenvalue.txt", "w");   /*open and write into existing file*/
    
     for(i=0;i<207;i++)
    {
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result == -1 ) { perror("error: read"); }
     
        fprintf(fp,"%02x ",chout); 
    
    }
    The result is correct but not in the correct arrangement. Example:

    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a f5 f5 23 00 c4 00 00 e7 f5 f5 00 00 00 28 04 b6 80 41 16 b9 c0 e1 1b 19 e8 61 21 3a d9 61 2e b9 5a 41 2f 31 56 a1 41 21 52 a1 42 30 16 e1 46 38 42 81 52 32 d9 61 65 2f e2 41 66 93 a2 a1 6a 0d a2 a1 6a ae 24 e1 81 25 a8 e1 82 19 88 c1 85 96 9d 41 86 2d 65 41 0d aa d4 82 16 a3 69 a2 19 1d 92 42 20 21 52 a2 20 2a 2a 22 21 90 d1 a2 2d 15 e7 a2 2f 97 91 22 35 1c 28 42 3a 8c 50 82 3d 12 66 82 43 12 cf e2 4d a3 e7 a2 5f 1a 8f 42 61 23 e6 22 61 ab a7 02 62 1f 8f e2 6a b2 61 a2 6c 20 8e 22 70 18 a1 82 70 b7 a2 a2 8a ae e5 42 00 00 00 00 00

    The BOLD part was suppose to be continue at the end line but unfortunately it jumps to the top. What's the problem?

  8. #53
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    What makes you think the bold parts should be at the end? I don't see that you are clearing the input buffer anywhere in your code, so why can't those values be present in the beginning of your buffer?

    What is the purpose of your magic number 207?

    Does your external device continually send data? Or is is just a burst of X number of bytes?

    Jim

  9. #54
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    What makes you think the bold parts should be at the end? I don't see that you are clearing the input buffer anywhere in your code, so why can't those values be present in the beginning of your buffer?

    What is the purpose of your magic number 207?

    Does your external device continually send data? Or is is just a burst of X number of bytes?

    Jim
    Actually the i checked the data by using Cutecom software and all the number are the same but the arrangement is incorrect, thats why im so sure. Actually the 207 byte data are output from my device which i'm trying to send it into a txt file as you can see in the code i gave just now.

    The device send the command continuously same as when i received the 8-byte data previously, but now it is 207 byte data.

    Where do i clear the buffer and "so why can't those values be present in the beginning of your buffer?" this part i don't quite understand.

  10. #55
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    There are several problems with the way you're trying to read the data, first there may be data in the input buffer before you start. You should always clear the buffer before you start to actually work with your serial port. Second since you're not waiting for data your "read" may be returning 0 bytes read, with a serial port this is not necessarily an error, you never check to insure that there is data in the buffer, or check that read() returned a positive value. Third, it is possible to find out if you have actually emptied the buffer, you may want to investigate this possibility.

    Jim
    Marvin Gorres likes this.

  11. #56
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    There are several problems with the way you're trying to read the data, first there may be data in the input buffer before you start. You should always clear the buffer before you start to actually work with your serial port. Second since you're not waiting for data your "read" may be returning 0 bytes read, with a serial port this is not necessarily an error, you never check to insure that there is data in the buffer, or check that read() returned a positive value. Third, it is possible to find out if you have actually emptied the buffer, you may want to investigate this possibility.

    Jim
    Thanks for the advice. I will try to settle the buffer thing and check the read() value. Any other suggestion? or suggestion in correcting my code?

  12. #57
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    There are several problems with the way you're trying to read the data, first there may be data in the input buffer before you start. You should always clear the buffer before you start to actually work with your serial port. Second since you're not waiting for data your "read" may be returning 0 bytes read, with a serial port this is not necessarily an error, you never check to insure that there is data in the buffer, or check that read() returned a positive value. Third, it is possible to find out if you have actually emptied the buffer, you may want to investigate this possibility.

    Jim
    i don't really know where to clear the buffer exactly, can u point to that particular part in my code?

  13. #58
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    i don't really know where to clear the buffer exactly, can u point to that particular part in my code?
    After you're done initializing the port, but before you actually start to use the port. Plus possibly anytime you "finish" your write/read sequence.

    Jim

  14. #59
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    After you're done initializing the port, but before you actually start to use the port. Plus possibly anytime you "finish" your write/read sequence.

    Jim
    Thanks for pointing it out Jim. I've updated my code and get the correct result but sometimes it is correct and sometimes its not. comment on my code below:

    Code:
    chout = 0;
    
     fp = fopen("results_eigenvalue.txt", "w");   /*open and write into existing file*/
    
     for(i=0;i<207;i++)
    {
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result == -1 ) { perror("error: read"); }
     
        fprintf(fp,"%02x ",chout); 
    
    }
    *UPDATE: i think its because of my fingerprint. Got some black stain on it. when i switch finger. it output the correct data. lol
    Last edited by Marvin Gorres; 04-17-2013 at 03:12 AM.

  15. #60
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    The way i clear my buffer, is that a good way?

Page 4 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read in from serial port; getchar()
    By s.dodd in forum C Programming
    Replies: 9
    Last Post: 10-23-2011, 11:50 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 06:56 PM
  3. serial port read
    By mackrackit in forum C++ Programming
    Replies: 6
    Last Post: 03-22-2006, 01:40 AM
  4. How to read from a serial port
    By WDT in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2004, 07:31 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 09:21 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21