Thread: Wirte and read serial port

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You are using "void main" - Have you seen Salem's avatar? Change it to "int main" and return 0 at the end.
    Fact - Beethoven wrote his first symphony in C

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Click_here View Post
    You are using "void main" - Have you seen Salem's avatar? Change it to "int main" and return 0 at the end.
    Changed it already but still the output data i get sometimes correct and sometimes not correct. hmm.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The way i clear my buffer, is that a good way?
    Where are you clearing your buffer? I'm not talking about your character buffer, I'm talking about the "hardware" buffer. You do know that both the operating system and the hardware usually buffer this port, right? To clear these buffers after you initialize the port you call tcflush().

    Code:
    tcflush(fd, TCIOFUSH); // Clear both the input and output buffers.
    comment on my code below:
    What's new to comment about? You haven't changed anything. You're still not checking if read() actually read anything. Please note this quote from this tutorial: Serial Programming Guide for Linux

    Reading Data from the Port

    Reading data from a port is a little trickier. When you operate the port in raw data mode, each read(2) system call will return the number of characters that are actually available in the serial input buffers. If no characters are available, the call will block (wait) until characters come in, an interval timer expires, or an error occurs. The read function can be made to return immediately by doing the following:

    fcntl(fd, F_SETFL, FNDELAY);

    The FNDELAY option causes the read function to return 0 if no characters are available on the port. To restore normal (blocking) behavior, call fcntl() without the FNDELAY option:

    fcntl(fd, F_SETFL, 0);
    Changed it already but still the output data i get sometimes correct and sometimes not correct. hmm.
    What is this supposed to mean? You'll need to provide much more information. Does your external hardware have a method of outputting a predetermined string so you can check the communications? Can you tell us what type of hardware your trying to interface to? The manufacturer, and if the board is mass produced the model and part number of the device you're using?




    Jim

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Where are you clearing your buffer? I'm not talking about your character buffer, I'm talking about the "hardware" buffer. You do know that both the operating system and the hardware usually buffer this port, right? To clear these buffers after you initialize the port you call tcflush().

    Code:
    tcflush(fd, TCIOFUSH); // Clear both the input and output buffers.

    What's new to comment about? You haven't changed anything. You're still not checking if read() actually read anything. Please note this quote from this tutorial: Serial Programming Guide for Linux




    What is this supposed to mean? You'll need to provide much more information. Does your external hardware have a method of outputting a predetermined string so you can check the communications? Can you tell us what type of hardware your trying to interface to? The manufacturer, and if the board is mass produced the model and part number of the device you're using?




    Jim

    Hello jim, the problem seems to be solved already. I did the input and output clear buffer and its settled. Now i have another problem, below is my code to print the output from the device into a text file:

    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"); }
      if ( i >= 8){
        fprintf(fp,"%02x ",chout); }
    
    }
    Problem is the result i get is below:

    f5 23 00 c4 00 00 e7 f5 f5 00 00 00 29 (for example)

    What i need is some thing like below:

    \xf5\x23\xc4\x00\x00\xe7\xf5\xf5\x00\x00\x00\x29

    ==> I tried to do this "fprintf(fp,"\x%02x ",chout);", but i get this error below:

    error: \x used with no following hex digits

    Any suggestion?

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, 10:50 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05: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, 06:31 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM

Tags for this Thread