Thread: Serial Programming, Write-Read Issue

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Serial Programming, Write-Read Issue

    Hi,

    I am still new to serial programming in linux. So to communicate with an A to D device I found some code online and modified it for my purpose. The problem is I can never perform "Write then Read".

    Whereas when I perform "just Read" and "Read then Write and then Read", the code works with no issues. But if I write a command to the port first and then try to read the response, the program just waits for the response indefinitely.

    Another interesting thing I noticed is while the program is waiting for the response, if I open a connection to the port using "CuteCom" then the program starts reading the response.

    Also if I perform the write operation using a different binary, then I can read the responses with no issues.

    I really don't understand why this is happening. Any form of help on this regard would really be appreciated. My system details and code are below.

    Thanks and Regards,

    Aravind


    System Details:
    Ubuntu 10.04 & Connected the RS-232 cable to my laptop by using RS-232 to USB converter.

    Code:
    Code:
    #include <termios.h>
    
    #include <stdio.h>
    
    #include <unistd.h>
    
    #include <fcntl.h>
    
    #include <sys/signal.h>
    
    #include <sys/stat.h>
    
    #include <sys/types.h>
    
    #include <sys/time.h>
    
      
    
    #define BAUDRATE B115200
    
    #define PORT "/dev/ttyUSB0"
    
    #define _POSIX_SOURCE 1 /* POSIX compliant source */
    
    #define FALSE 0
    
    #define TRUE 1
    
    
    
    
    void signal_handler_IO ( int status );   // definition of signal handler        
    
    
    
    volatile int STOP = FALSE;         
    
    int wait_flag = TRUE;                  // TRUE while no signal received
    
    
    
    
    
    int main()
    
    {
    
        int fd, res, sen;
    
        int i;
    
        struct termios oldtio, newtio;
    
        struct sigaction saio; /* definition of signal action */     
    
        char comm[7], resp[10];
    
    
    
    	for ( i = 0; i < 10; i++ ) { resp[i] = 0x00; }
    
    	  
    
            
    
        /* open the device to be non-blocking (read will return immediatly) */
    
        fd = open( PORT, O_RDWR | O_NOCTTY | O_NONBLOCK );
    
        if (fd < 0) { perror(PORT); return 1; }
    
            
    
    	/* install the signal handler before making the device asynchronous */
    
        saio.sa_handler = signal_handler_IO;
    
        //saio.sa_mask = 0;
    
        saio.sa_flags = 0;
    
        saio.sa_restorer = NULL;
    
        sigaction( SIGIO, &saio, NULL );
    
              
    
        /* allow the process to receive SIGIO */
    
        fcntl( fd, F_SETOWN, getpid() );
    
        /* Make the file descriptor asynchronous (the manual page says only 
    
           O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
    
        fcntl( fd, F_SETFL, FASYNC );
    
            
    
        //tcgetattr(fd,&oldtio); /* save current port settings */
    
        /* set new port settings for canonical input processing */
    
        newtio.c_cflag = BAUDRATE | CRTSCTS | CLOCAL | CREAD;
    
        newtio.c_iflag = IGNPAR | ICRNL;
    
        //newtio.c_iflag = IGNPAR;
    
        newtio.c_oflag = 0;
    
        newtio.c_lflag = ICANON;
    
        //newtio.c_lflag = 0;
    
        newtio.c_cc[VMIN] = 0;
    
        newtio.c_cc[VTIME] = 0;
    
        tcflush(fd, TCIFLUSH);
    
        tcsetattr( fd, TCSANOW, &newtio );
    
    
    
    	comm[0] = 'V'; comm[1] = '\r';
    
    	printf("Write start...\n");
    
    	sen = write( fd, comm, 2 );
    
    	printf("Write stop...\nsen = %d\n", sen);
    
    	
    
    	printf("Read start...\n");
    
        res = read( fd, resp, 4 );
    
    	printf("Read stop...\n");
    
        resp[res] = '\0';
    
        printf("Response = %s\n", resp );
    
       
    
        /* restore old port settings */
    
    	tcsetattr( fd, TCSANOW, &oldtio );
    
    
    
      return 0;
    
    
    
    }
    
            
    
     /***************************************************************************
    
     * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
    
     * characters have been received.                                           *
    
     ***************************************************************************/
    
            
    
    void signal_handler_IO (int status)
    
    {
    
    	printf("received SIGIO signal.\n");
    
    	wait_flag = FALSE;
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    A couple of things you can try:
    1. flush both in/out buffers
    Code:
     tcflush(fd, TCIOFLUSH);
    2. Many embedded systems look for the cr/lf pair so you might try adding '\n' to your send string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Talking to device over Serial: Write works, Read doesn't
    By theBishop in forum C Programming
    Replies: 2
    Last Post: 07-09-2008, 01:40 PM
  3. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  4. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 10:25 PM
  5. InferRed Serial COMM, cannot write?
    By Xei in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 01:40 PM