title: Serial communication between linux and 8051 microcontroller board using rs-232 in c
body: <p>I have problem in sending data serially using RS-232 from linux OS to uc 8051.
  8051 setting:</p>
  
  <p>baudrate = 9600;<br>
  comport = port1.<br>
  parity = none<br>
  stop bit = one<br></p>
  
  <pre><code>// my code for receiving data on 8051 uc
  #include &lt;reg51.h&gt;
  
  unsigned char value;
  int i,j;
  
  void ini()     
  {
      TMOD=0x20;  //Timer1, mode 2, baud rate 9600 bps
      TH1=0XFD; 
      SCON=0x50;
      TR1=1;
  }
  
  void delay()
  {
      for(i=0;i&lt;=1000;i++)
      for (j=0;j&lt;=300;j++);
  }
  
  void recieve() 
  {
      unsigned char value;
      while(RI==0);
      value=SBUF;
      P1=value;
      RI=0;
  }
  
  void main()
  {
      while(1)
      {
          ini();
          recieve();
      }
  }
  
  // and code which run on linux is as following
  #include &lt;stdio.h&gt; // standard input / output functions
  #include &lt;string.h&gt; // string function definitions
  #include &lt;unistd.h&gt; // UNIX standard function definitions
  #include &lt;fcntl.h&gt; // File control definitions
  #include &lt;errno.h&gt; // Error number definitions
  #include &lt;termios.h&gt; // POSIX terminal control definitionss
  #include &lt;time.h&gt;   // time calls
  
  int open_port(void)
  {
      int fd; // file description for the serial port
  
      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  
      if (fd == -1) // if open is unsucessful
      {
          //perror("open_port: Unable to open /dev/ttyS0 - ");
          printf("open_port: Unable to open /dev/ttyS0. \n");
      }
      else
      {
          fcntl(fd, F_SETFL, 0);
          printf("port is open.\n");
      }
  
      return(fd);
  } //open_port
  
  int configure_port(int fd)      // configure the port
  {
      struct termios port_settings;      // structure to store the port settings in
  
      cfsetispeed(&amp;port_settings, B9600);    // set baud rates
      cfsetospeed(&amp;port_settings, B9600);
  
      port_settings.c_cflag &amp;= ~PARENB;    // set no parity, stop bits, data bits
      port_settings.c_cflag &amp;= ~CSTOPB;
      port_settings.c_cflag &amp;= ~CSIZE;
  port_settings.c_cflag |= CS8;
  
      tcsetattr(fd, TCSANOW, &amp;port_settings);    // apply the settings to the port
      return(fd);
  
  } //configure_port
  
  int query_modem(int fd)   // query modem with an AT command
  {
      char n;
      fd_set rdfs;
      struct timeval timeout;
  
      // initialise the timeout structure
      timeout.tv_sec = 10; // ten second timeout
      timeout.tv_usec = 0;
  
      // Create byte array
      unsigned char send_bytes[] = {  0x00, 0xff};
  
      write(fd, send_bytes, 2);  //Send data
      printf("Wrote the bytes. \n");
  
      // do the select
      n = select(fd + 1, &amp;rdfs, NULL, NULL, &amp;timeout);
  
      // check if an error has occured
      if(n &lt; 0)
      {
          perror("select failed\n");
      }
      else if (n == 0)
      {
          puts("Timeout!");
      }
      else
      {
          printf("\nBytes detected on the port!\n");
      }
      return 0;
  } //query_modem
  
  int main(void)
  { 
      int fd = open_port();
      configure_port(fd);
      query_modem(fd);
      return(0);
  } // main
  </code></pre>
  
  <p>But I have problem .. so plz help me out and tell me in which format linux sends data through rs-232. Also receiving format of 8051 micro-controller.
  Need sample code in C.</p>