Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

UART Communication problem

Hi All,

I am using DE1 kit and connect UART to COM1 port of pc.I write a code in Nios to transfer a single character to hyperterminal. Also set all the settings like baudrate,flow control,parity etc..in appropriate manner.At the run time I can see the TXD LED blinking on the kit But I can't receive any character on hyperterminal.

Any suggestions ???

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    试用一下下面的代码。

    祝你好运!

    Try to use the code as bellow.

    Best regards!

    #include "unistd.h"                     // UNIX API
    # include <fcntl.h>                      // UNIX API
    # include <stdio.h>                      // printf()
    # include <string.h>                     // strlen()
    # include "alt_types.h"
    //++++++++++++++++++++++++++++++++++++++
    // &#20462;&#25913;UART&#21517;&#31216; &#24320;&#22987;
    // &#26681;&#25454;SOPC Builder&#35774;&#32622;&#20462;&#25913;
    //++++++++++++++++++++++++++++++++++++++
    # include "system.h"
    # define my_uart_name UART_NAME
    //--------------------------------------
    // &#20462;&#25913;UART&#21517;&#31216; &#24320;&#22987;
    //--------------------------------------
    # define CHAR_NUM 8
      
    int main()
    {
      int fd;
      alt_u8 *ptr;
      alt_u8 charBuf;                  // 100&#23383;&#33410;&#30340;&#23383;&#31526;&#32531;&#23384;
      alt_u8 num_temp1, num_temp2;          // &#20020;&#26102;&#21464;&#37327;
      alt_u8 *msg1 = "Please Enter 8 charaters : ";
      alt_u8 *msg2 = " has been Rcved\n";  
      
      //++++++++++++++++++++++++++++++++++++
      // &#21021;&#22987;&#21270; &#24320;&#22987;
      //++++++++++++++++++++++++++++++++++++
      ptr       = charBuf;                  // &#25351;&#21521;&#23383;&#31526;&#32531;&#20914;&#21306;
      num_temp2 = CHAR_NUM;                 // &#27442;&#35835;&#20837;CHAR_NUM&#20010;&#23383;&#31526;
      num_temp1 = num_temp2;
      //-----------------------------------
      // &#21021;&#22987;&#21270; &#32467;&#26463;
      //-----------------------------------  
      
      
      //++++++++++++++++++++++++++++++++++++
      // &#25171;&#24320;UART &#24320;&#22987;
      //++++++++++++++++++++++++++++++++++++
      fd = open(my_uart_name, O_RDWR, 0666);   // &#20197;&#21487;&#35835;&#20889;&#26041;&#24335;&#25171;&#24320;&#35774;&#22791;&#25991;&#20214;
      if (fd < 0)                           // &#22914;&#26524;&#25171;&#24320;&#22833;&#36133;
      { 
         printf("error\n ");
         return 1;
      }
      //------------------------------------
      // &#25171;&#24320;UART &#32467;&#26463;
      //------------------------------------
      //++++++++++++++++++++++++++++++++++++
      // &#36755;&#20986;msg1 &#24320;&#22987;
      //++++++++++++++++++++++++++++++++++++
      write(fd, msg1, strlen(msg1));
      //------------------------------------
      // &#36755;&#20986;msg1 &#32467;&#26463;
      //------------------------------------
      //++++++++++++++++++++++++++++++++++++
      // &#35835;&#21462;CHAR_NUM&#20010;ASCII&#30721;&#23383;&#31526; &#24320;&#22987;
      //++++++++++++++++++++++++++++++++++++
      while(num_temp1)                         // &#35835;&#21462;CHAR_NUM&#20010;ASCII&#30721;&#23383;&#31526;
      {
        num_temp2  = read(fd, ptr, num_temp2);
        ptr       += num_temp2;
        num_temp1 -= num_temp2;
        num_temp2  = num_temp1;
      }
      //------------------------------------
      // &#35835;&#21462;CHAR_NUM&#20010;ASCII&#30721;&#23383;&#31526; &#32467;&#26463;
      //------------------------------------
      
      
      //++++++++++++++++++++++++++++++++++++
      // &#36755;&#20986;&#35835;&#20837;&#23383;&#31526;&#21644;msg2 &#24320;&#22987;
      //++++++++++++++++++++++++++++++++++++
      write(fd, charBuf, 8);                // &#36755;&#20986;&#35835;&#20837;&#23383;&#31526;
      write(fd, msg2, strlen(msg2));        // &#36755;&#20986;msg2
      //------------------------------------
      // &#36755;&#20986;&#35835;&#20837;&#23383;&#31526;&#21644;msg2 &#32467;&#26463;
      //------------------------------------
      
      //++++++++++++++++++++++++++++++++++++
      // &#20851;&#38381;UART &#24320;&#22987;
      //++++++++++++++++++++++++++++++++++++
      close(fd);                            // &#20851;&#38381;&#35774;&#22791;
      //------------------------------------
      // &#20851;&#38381;UART &#32467;&#26463;
      //------------------------------------
      
      return 0; 
    }