libftdi Archives

Subject: write/read syncronization

From: "Dr. Igor Nikitin" <igor.nikitin@xxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 4 Feb 2010 12:09:57 +0100 (CET)
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.

libftdi - see for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx

Current Thread