Forum Discussion

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

RS232 coding problem

Hi all,

I require to send/receive a character through the rs232 port, however i only be able to receive character. I had problem with sending the character out. Below is my coding for sending and receiving. Please kindly provide me with advise/solution. Thanks

void serial()

{

int a = 99;

char c;

c=IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);

printf("%c",c); // result shown is correct

IOWR_ALTERA_AVALON_UART_CONTROL(UART_0_BASE, 0x40);

IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,a);//NO CHARACTER DETECTED

}

8 Replies

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

    hello, I have made similar program.

    I don't remember exactly what I did.

    the code was

    -------------------------------------------------------------------

    void putUchar(unsigned char str)

    {

    IOWR_32DIRECT(H55UART_BASE,OFF_TXDATA ,str);

    usleep(UART_WAIT); // UART_WAIT is 1000

    }

    -------------------------------------------------------------------

    I think 1msec wait ( for 9600bps) is what you need.

    how is it?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi Akira,

    I had try to add on the usleep(1000) below my code, but yet no character is detect in my VB(visual basic) where I use to detect and send character. But anyhow, thanks for your advice.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    how about doubting VB.

    in some case you can not get characters when the character is not correct ASCII code.

    you should measure the signal TX. by oscilloscope or signaltapII.

    or, just send UART signal from NiosII to "the NiosII" by itself.

    good luck.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The easiest terminal emulator to use on windows (and probably linux) is putty. Testing with that will remove some of the unknowns.

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

    hi vincoldes!

    i'm trying to finish my graduation project, for that i need to receive some text in my DE2 rs232 port and i'm using nios ii like u, i was wondering if you can help me here sending me your code? sorry but i'm really desperate =(

    hope to hear soon from you =)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi liscabello,

    I found that we have 2 ways of doing it:

    1. Using simple getc(fp) function where by pointer *fp is point to my uart. The code is something like this:

    FILE *fp

    fp = fopen ("/dev/uart_0", "r+");

    while(fp)

    {

    getc(fp);read

    }

    fclose(fp)

    2. Using IORD & IOWR for read and write.Example code as below:

    IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,*(b+j)); for write

    IORD_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,*(b+j)); for wirte

    You can refer to the handbook for further details. Sry that if I cant help much:)