The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read the CTS and DSR of RS232 in Unix using C language? Swing5 High Level Programming 2 04-01-2009 12:46 AM
Using non standard baudrate 28800 with rs232 palexo High Level Programming 4 02-10-2007 09:10 PM
rs232 pci-card pressy SUN Solaris 1 11-06-2004 11:02 PM
VNC via Rs232 joerg UNIX for Advanced & Expert Users 2 08-14-2002 05:22 AM
RS232 communication optimisation ManishSaxena High Level Programming 1 04-04-2002 09:25 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 10-12-2008
dudi.forum dudi.forum is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
RS232 programming problem

Hi all

I encountered a strange phenomenon when reading / writing to RS232 serial device (on my machine /dev/ttyS0)

I have simple 2 processes:

1) process which WRITE characters from /dev/ttyS0
For example write the characters "aa102222233333444445555566666777778888899999aaaaabbbbbcccccddddd"

2) Program which READ characters from /dev/ttyS0 ( the process is blocked when nothing to be read)

I short the TX and the RX pins in my RS232 PC port (the 9 pins serial connector) so I 'll be able to read what I write.

The problem is:

when WRITE process write something like "aa102222233333444445555566666777778888899999aaaaabbbbbcccccddddd"

The READ process reads some time read ONLY part of the above characters like: "8888899999aaaaabbbbbcccccdddddaa102222233333"


Can you please help me to understand what is wrong?



code:


Process write:
----------------
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 */

int main()
{
      int fd; /* File descriptor for the port */

      // num of read characters
      int n = 0;
    
      const char* serial_device = "/dev/ttyS0";
    
     fd = open( serial_device , O_WRONLY | O_NOCTTY | O_NDELAY);


      if (fd == -1)
      {
               /*
        * Could not open the port.
        */
          printf("open_port: Unable to open %s \n" , serial_device);
      }
   


       const char* sentance = "aa102222233333444445555566666777778888899999aaaaabbbbbcccccddddd";

    printf("About to write %s \n to UART on %s \n" , sentance ,  serial_device);

        n = write(fd, sentance , strlen(sentance));
    
        if (n < 0)
    {
          fputs("write() byte failed!\n", stderr);
    }
    else
    {
        
        printf("writting %d bytes to UART on %s \n" , n , serial_device);
    }


    close(fd);
    return 0;
}




Process read:
---------------

#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 */
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "signal.h"
#include <assert.h>

#define N 1000

int main()
{
      int fd; /* File descriptor for the port */
    
        
      char array[N] = {'\n'};

      // num of read characters
      int n = 0;
    
    
      const char* serial_device = "/dev/ttyS0";
    

      
      fd = open(serial_device, O_RDONLY | O_NOCTTY );

      if (fd == -1)   
      {
           /*
          * Could not open the port.
         */
          printf("open_port: Unable to open %s \n" , serial_device);
          
      }
      else
      {
          fcntl(fd, F_SETFL, 0);
      }

    
      printf("Reading process form UART on %s \n" , serial_device);

    
    while (1)
    {
        printf("Begin to read\n");
        fflush(stdout);


        n = read(fd, array , 500 );

        if (n >= 0)
        {
            array[n]='\n';
        }
        
        if (n < 0)
        {
              fputs("read() failed!\n", stderr);
        }
        else
        {
        
            printf("The content of the Serial port is: %s len is %d \n" , array , n);
            fflush(stdout);

            n = 0;
    
    
        }

    }


    close(fd);
    return 0;
}

Last edited by otheus; 10-14-2008 at 05:49 AM.. Reason: added code tags
 

Bookmarks

Tags
c programming, i/o programming, serial port

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0