views:

44

answers:

2

I am working on robot which has to control using wireless serial communication. Robot is running on micro controller( by burning .hex file). I want to control it using my Linux(Ubuntu) pc. I am new to serial port programming. I am able to send the data but I am not able to read data. few piece of code which is running over micro controller:

function to send data

void TxData(unsigned char tx_data)

{   SBUF = tx_data;          //Transmit data that is passed to this function
    while(TI == 0);     //wait while data is being transmitted

}

I am sending data through array of characters data_array[i]

  for (i=4;i<=6;i++)
  {   
     TxData(data_array[i]);
     RI = 0;              //Clear receive interrupt. Must be cleared by the user.
     TI = 0;        //Clear transmit interrupt. Must be cleared by the user.
   }

Now the piece of code from C program running on Linux...

> while (flag==0)  {
>      int res = read(fd,buf,255);
>      buf[res]=0; /* set end of string, so we can printf */
>      printf(":%s:%d\n", buf, res);
>      if (buf[0]=='\0') flag=1; 
       }

It prints out value of res = 0 .

Actually I want to read data character-by-character to perform calculations and take further decision. Is there other way of doing this? Please help because I am not able to proceed with project.

Note: If someone can suggest good study material(code) for serial port programming on Linux, it would help me.

Thanks....

+2  A: 

This is a good guide:

http://www.c-program.com/pdf/serialPort_Programming_c.pdf

The read call may return with no data and errno set to EAGAIN. You need to check the return value and loop around to read again if you're expecting data to arrive.

Amardeep
Thanks amardeep for ur help. I need one more favor plz. actually what I am doing is sending the whiteline sensor's(left, middle, right) value(which keeps on changing) and at other end receiving them. Can u help me in receiving these values in array so I can store them in different variables and then do computation. Also there is problem in synchronization like in above sending code I am only receiving value of data_array[6] always , it is not showing other 2 values. Is there a way to synchronize them and correctly receive them in an array or in 3 different variables?
Dinesh
A: 

First, take a look at /proc/tty/driver/serial to see that everything is setup correctly (i.e. you see the signals you should see). Then, have a look at the manual page for termios(3), you may be interested in the VMIN and VTIME explanation.

ninjalj