RS232 programming problem


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users RS232 programming problem
# 1  
Old 10-12-2008
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 06:49 AM.. Reason: added code tags
# 2  
Old 10-14-2008
1. You should start the READ process first.

2. When you do the read, you're trying to read 500 bytes. It never gets to 500. So I'm not sure how your program outputs what it does. If you want, you can read 1 byte at a time and collect the input into a string which you output after it's 20 bytes long or so. Otherwise, use poll().
# 3  
Old 10-14-2008
Thanks for you replay









Thanks for you replay

I think I was not describing my problem properly,

I Run the READ process first, and then the WRITE process

I write the code following the Serial Programming guide:
http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html

Because I open the device file /dev/ttyS0 with the flags then the read system call is blocked. mean that the read is blocked until there is data in the file descriptor (no need to poll, unless I need to poll between 2 files descriptors)

The problem might be partially solved (for the first time running the read process) by flushing the rx buffers by the code:

tcflush(fd , TCIOFLUSH ); //this code is supposed to flush out the content of write buffer of the file descriptor fd.

However, process READ still get (sometimes) only part of chars what process WRITE writing to the fd.


Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Connecting PIC16F877A using Easypic5 via RS232 with Linux

I searched the forums for RS232 issues, but all of the results don't help me solving my problem, so I had to open new thread. This is the problem: I have PIC16F877A micro-controller and I'm programming it with Easypic5(the PIC programming device(board) from MikroElektronika) and I want to... (1 Reply)
Discussion started by: ivan_vasileski
1 Replies

2. Programming

Programming problem

Hi Everybody. I have an Ubuntu linux 10.04 os and want be able to use the website of intuit but it only runs on unix. Is there any way that I can put the needed drivers in this os since both linux/unix share some distinctive programs. If this is not possible how can I save the information that I... (1 Reply)
Discussion started by: mk631219
1 Replies

3. UNIX for Dummies Questions & Answers

Can A File Be Copied to an RS232 Port on Unix?

We have a text/csv file (4mb) sitting on a Unix box. The PC belongs to a retail store and they're asking me to bring the file over to Windows so they can import the file. Can a file be copied to an rs232 port? If so how? The idea is that I'll hook up a null modem cable to the Unix com port,... (2 Replies)
Discussion started by: alivebyscience
2 Replies

4. Shell Programming and Scripting

Problem with awk programming

i want to calculate power of 10 as follows: coef=2.0588 ra1=$(awk 'BEGIN{print 10^$coef}') it gives a result 1 when, if i calculate ra1=$(awk 'BEGIN{print 10^2.0588}') it gives a result 114.499 what should i do? (2 Replies)
Discussion started by: SANGUINE
2 Replies

5. Programming

How to read the CTS and DSR of RS232 in Unix using C language?

Hello to all Gurus out there, Could you show me a source code in Unix platform using C language. I want to read the status or voltage level of the DSR and CTS. Thanks a lot, Swing5 (2 Replies)
Discussion started by: Swing5
2 Replies

6. AIX

Transferring files via RS232 cable

Current setup: Some old AIX standalone machine (F series) My WinXP laptop Null modem cable from laptop to AIX machine (console port). On Hypterminal on WinXP I can connect to the AIX machine (serial connection), but when I try to transfer file, it will just sit there. I'm guessing the... (3 Replies)
Discussion started by: apra143
3 Replies

7. Programming

Using non standard baudrate 28800 with rs232

Hi, can somebody help me with this one? I have to configure rs232 write/read inside a C program. I'm using the termios.h (POSIX) The problem is that the only baudrate that works for me is 28800 and it is not supported any more from this driver. :( So I can't write : cfsetispeed(&options,... (4 Replies)
Discussion started by: palexo
4 Replies

8. Solaris

rs232 pci-card

hello mates, do you know a manufactor for rs232 serial pci cards for the sparc architecture? it seems that there is no original product from sun for that purpose. strange, isn't it? problem is that i have a V250 with only one db9 rs232 serial port, and i need it for a modem connection, but i also... (1 Reply)
Discussion started by: pressy
1 Replies

9. UNIX for Advanced & Expert Users

VNC via Rs232

Hi ! Is there somebody who is able to connect a VNC session via serial cable ? There is a hint on the VNC homepage that it is possible to configure that. But how? thanks joerg (2 Replies)
Discussion started by: joerg
2 Replies

10. Programming

RS232 communication optimisation

Need help for RS232 communication with switches in order to optimise the data traffic. How to manipulate different flags in termios struct to do the same? (1 Reply)
Discussion started by: ManishSaxena
1 Replies
Login or Register to Ask a Question