Thread: Serial IO Using C

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Serial IO Using C

    Code:
     
    int main(int argc, char **argv)
    {
    	FILE *input,*output;
    	char text[10000];
    	int i;
      
    	input = fopen("/dev/ttyAM2", "r");      //open the terminal keyboard
    	output = fopen("/dev/ttyAM2", "w");     //open the terminal screen
    	
    	while(1)
    	{
    //	fputc(i, output);
    //	fputc('\n', output);
    
    	fputs("022078176708\n", output);  // works only with end of line
    
    	sleep(1);
    		  
    	fscanf(input, "%s",text); // worked with fputc(i), + putc('\n')
    
    	}
    	fclose(output);
    	fclose(input);
    	
     return 0;
    }


    I am trying to receive serial input from a hardware device that transmits ascii data. The problem seems to be that the data does not have any control characters in it and none of the constructs that I have used so far (fscanf, fgetc, fgets, fread or read) seem to be able to see what is in the buffer.

    As an experiment I tied the receive and transmit lines together (this is on an embedded Linux system), and send data out on the xmit side and try to read it on the rcv side. It seems that the only way the data makes it through is when there is a newline sent along with the data. Unfortunately the device I need to listen to has no such output.

    I know this can be done, but have never seen it. Any ideas would be appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what about using fflush of the output stream?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    fflush on the input and output streams have no impact on the behavior.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The tty driver itself is filtering out the control characters. You need to open() the device, not fopen() it, and you need to program it into raw mode.

    The man page for tcsetattr() should bring up all the relevant functions.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Polychromix View Post
    fflush on the input and output streams have no impact on the behavior.
    Don't fflush input streams.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. Please help with serial communication problem - Long
    By spdylude in forum Windows Programming
    Replies: 3
    Last Post: 04-06-2005, 09:41 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM