Forum: µC & Digital Electronics atmega8 ftdi usb communication

von Igor (Guest)

Rate this post
0 useful
not useful
Hi people,

I have questions related to libftdi.
I use atmega8 communicating with PC via FTDI UB232R,
it's a little board with FT232RQ chip on it.
I am sending test messages ("commands") from PC to atmega
and want to receive a reply separately to every command
if possible immediately. The test program is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ftdi.h>

unsigned char buf [1000];
struct ftdi_context ftdi;

void test(char* str){ int i,rsiz;
        printf("in: %s\n",str); fflush(stdout);
        ftdi_write_data(&ftdi, (unsigned char*)str, strlen(str)+1);        
        rsiz=ftdi_read_data(&ftdi, buf, 1000);
        printf("out(%d): ",rsiz);
        printf("\n"); fflush(stdout);

int main(int count, char *argv[])
{ int i;

        ftdi_usb_open(&ftdi, 0x0403, 0x6001);

     test("one"); test("two"); test("three");


On atmega side there is a loop
for(;;){ ugets(str,1000); uputs(str); }

where ugets reads incoming data until termination character '\0' is
encountered (and '\0' also goes to str), uputs sends the data until '\0'
('\0' itself is not sent).

The typical output is
in: one
in: two
in: three
in: one
out(11): twothreeone
in: two
in: three
out(8): twothree

The question is how to force the immediate reply to have
in: one
out(3): one
in: two
out(3): two
in: three
out(5): three

is there a kind of fflush?

The other question is why does the very first message disappear?
Restarting test program again and again, I see that the first
"one" is almost always missed, not appearing in further output.
Therefore attempt to wait until data are coming
for(rsiz=0;rsiz==0;)rsiz=ftdi_read_data(&ftdi, buf, 1000);

will end in an infinite loop. I have tested that microcontroller
in this situation is still awaiting for incoming string ("ugets"),
i.e. it has not received the first message. Must be there
an initial reset or something else? Should there be some
additional connections between atmega and FTDI chip
except of TXD, RXD, GND ?

I am a beginner in serial communication and will be grateful
for your help.

Cheers, Igor.


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.