Problem with read data from serial device


 
Thread Tools Search this Thread
Top Forums Programming Problem with read data from serial device
# 1  
Old 02-17-2010
Problem with read data from serial device

I have problem with C programming. I want to send & receive data through serial communication. I send data(command) to device to get data from device but when receive data, it can't get altogether of data.
It get only some data. What should I do to get altogether of data?
If all of you have the solution. Please advice me.
Many thanks.
# 2  
Old 02-17-2010
Could you explain the problem in more detail? What are you sending to? What are you sending? What are you receiving? What should you be receiving?
# 3  
Old 02-19-2010
Solution

If your problem is regarding text data means the following may be the problem,

1. It may be the buffer problem.You need to flush the buffers like stdin,stdout after sending and receiving the data.

2 .You should have the buffer size which is enough to hold your data.For example ,if you are getting the input data in a variable called buf[100].So your data should have the maximum 100 characters which includes the null character to terminate the string .
# 4  
Old 02-25-2010
Thanks for all solution.
I'll explain in detail , I want to send & receive data in serial communication like minicom in Linux. Now I sent '\r' to the device. It should send data back to the program like
Login Failed.
Employee No.?
but it can receive only
Faild
ployNo.

My source is
Code:
 #include <termios.h>
      #include <stdio.h>
      #include <unistd.h>
      #include <fcntl.h>
      #include <sys/signal.h>
      #include <sys/types.h>
      #include <errno.h>
        
      #define BAUDRATE B9600
      #define MODEMDEVICE "/dev/ttyS0"
      #define _POSIX_SOURCE 1 /* POSIX compliant source */
      #define FALSE 0
      #define TRUE 1
        
      volatile int STOP=FALSE; 
        
      void signal_handler_IO (int status);   /* definition of signal handler */
      int wait_flag=TRUE;                    /* TRUE while no signal received */
      char buf[255];
      char new_buf[255];
      int fd;
      main()
      {
        int c, res,i;
        struct termios oldtio,newtio;
        struct sigaction saio;           /* definition of signal action */
        close(fd); //make sure that port is closed 
        /* open the device to be non-blocking (read will return immediatly) */
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
        if (fd <0) {perror(MODEMDEVICE); exit(-1); }
       
        /* install the signal handler before making the device asynchronous */
        saio.sa_handler = signal_handler_IO;
        sigemptyset(&saio.sa_mask);//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 | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR | ICRNL;
        newtio.c_oflag = 0;
        newtio.c_lflag = ICANON;
        newtio.c_cc[VMIN]=1;
        newtio.c_cc[VTIME]=0;
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd,TCSANOW,&newtio);
         
        write(fd, "\r", 1);
        sleep(1);
        int count=0; 
        /* loop while waiting for input. normally we would do something
           useful here */ 
        while (STOP==FALSE) {
          //sleep(3);
          /* after receiving SIGIO, wait_flag = FALSE, input is available
             and can be read */
          if (wait_flag==FALSE) {
            sleep(3); 
            res = read(fd,buf,255);
            //printf("res = %d\n", res);
            if(res > 1)
               printf("%s",buf);
            buf[res]='\0';
            else if(res < 1)
      STOP = TRUE;  
            wait_flag = TRUE;      /* wait for new input */
          }
        }
        /* restore old port settings */
        tcsetattr(fd,TCSANOW,&oldtio);
      }
        
      /***************************************************************************
      * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
      * characters have been received.                                           *
      ***************************************************************************/
        
      void signal_handler_IO (int status)
      {
        int i=0;
        //printf("fd = %d\n",fd);
        printf("received SIGIO signal.\n");
        wait_flag = FALSE;
      }

What do I wrong with this source code.
Please advice, thanks.

Last edited by jim mcnamara; 02-28-2010 at 12:18 PM..
# 5  
Old 02-25-2010
Why are you null-terminating the buffer after you print it? You're closing the barn, but the horse's already left.

Never call printf from a signal handler. fprintf(stderr, "stuff"); might work since that's unbuffered but even that is not guaranteed. Use write(), along with sprintf if necessary.

Are you sure the device uses hardware flow control?

wait_flag should be volatile.

If your goal is to just wait for I/O, you don't need to use SIGIO, you can just configure your terminal to make you wait when you do a read(). This would be better IMHO, since under some circumstances signals can be missed. You could also use select().

code tags for code please. [ code ] my fancy program [ /code ] but without the spaces in the tags. This will show your code with proper indenting.

Last edited by Corona688; 02-25-2010 at 02:27 PM..
# 6  
Old 02-26-2010
Could you edit my code ?
# 7  
Old 02-28-2010
Could you please use code tags?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read line from serial device with BASH

I'm new to Linux (Ubuntu 16.04), and very new to BASH scripting. I have a Numato 8-channel USB GPIO device, which is a DAQ that appears in the system as a serial port. In Linux it appears as ttyACM0. I can easily manipulate a GPO with, for example: echo "gpio set 7" > /dev/ttyACM0 ...followed... (12 Replies)
Discussion started by: Chalk-X
12 Replies

2. Programming

Read from serial port

Hi I try to communicate with a GSM modem, from C, for sending SMS. I use standart AT-commands. Working well with terminal. There is no problem writing ti the port. But when I try to read I only get a echo, I write "ATI" and get "ATI" back, I should get somthing like "SIEMENS 35... (4 Replies)
Discussion started by: dmiller
4 Replies

3. Programming

Wrong data with Read from a serial port.

hi, I've a problem on my C/C++ program with Posix Library. I have to read data from the serial but I have incorrect data, in fact I get a bunch of zeros: "2953.3174, 2785.2126, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ,... (24 Replies)
Discussion started by: enaud
24 Replies

4. SCO

Modifying serial printers device

I am trying to change one of my serial printers from /dev/ttyr002 to /dev/ttyr014: lpstat -s device for check3: /dev/ttyr002 device for check4: /dev/ttyr002 I changed the tty setting for check3 in: /etc/printcap /var/spool/lp/admins/lp/printers/check3 to /dev/ttyr014 Then I get:... (4 Replies)
Discussion started by: herot
4 Replies

5. AIX

Tape drive problem - no process to read data written to a pipe

Hi Everyone, The machine I'm working on is an AIX 5.3 LPAR running on a P650. oslevel -r shows 5300-08. I'm trying to take a backup to a SCSI tape drive, which has been working up until this point. I know of nothing that has changed recently to cause this problem. But when I try to take a... (0 Replies)
Discussion started by: need2bageek
0 Replies

6. Shell Programming and Scripting

problem to read data in array

My input is a long list of data start with "#": #read_1 123456898787987 #read_2 54645646540646406 #read_3 4654564654316 . . I got a bit confusing about how to set all in an array first. And then when I run a program name "statistic_program", it will read the array in scalar and do... (24 Replies)
Discussion started by: patrick87
24 Replies

7. Ubuntu

Ubuntu 9.04 Serial application to telnet to serial device

Hello! I am working on an application which reads environmental instruments which have serial ports. The application requires a serial port to be present to talk to the device (i.e. /dev/ttyS0 ). In some instances the environmental devices will be 100's of yards away from the computer, so a... (5 Replies)
Discussion started by: mvona
5 Replies

8. Linux

Device serial number

Hey! I'm trying to figure out a sollution for a problem I have at my company with an Iomega MiniMax 500 GB USB disk. If i run cat /proc/bus/usb/devices I get this information: T: Bus=01 Lev=02 Prnt=04 Port=00 Cnt=01 Dev#= 5 Spd=480 MxCh= 0 D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00... (2 Replies)
Discussion started by: noratx
2 Replies

9. Programming

Accessing device with Prolific USB-serial controller.

I am trying to access DG-100 gps logger on Mac OS X with POSIX API. The device uses a Prolific usb-serial controller, and connect to the usb port on my mac. After I install the Prolific driver, it shows up as /dev/tty.usbserial and /dev/cu.usbserial. The vendor has published the data format. So... (1 Reply)
Discussion started by: monkeybiz
1 Replies

10. UNIX for Dummies Questions & Answers

serial port device path

hi. Im trying to install a switch. And the manual says i should type a command including a SerialPortDevicePath. which is the filepath to serial port used for connection. However.. nothing about how to find this info. Could anyone help me where to find this path? thx mr.T (6 Replies)
Discussion started by: tyskertøs
6 Replies
Login or Register to Ask a Question