Society of Robots - Robot Forum

Software => Software => Topic started by: reefat on April 01, 2011, 11:01:26 AM

Title: How to program to communicate between FTDI & Arduino?
Post by: reefat on April 01, 2011, 11:01:26 AM
I want to read the serial data transmitted by arduino on my computer using FTDI API (Visual C++).

Arduino uses Serial.print or Serial.write to send data to it's serial port. I am trying to capture this transmitted data on my computer using FTDI API.
Note: I have connected my Arduino to my computer using the FTDI breakout (from SparkFun (http://www.sparkfun.com/products/9716)).

(http://lh3.googleusercontent.com/public/JqIEswOcUJy6DR2R7b5bnvlY_q2Iau7Jy1ymz4AU3puydIjsR-ixAo86XISJtA--AN7PWnwdtTCmNGR3MXtXdnYUVNaPXRdMiS4yzNyi-Lm0p6QJScTo1bsCYVgsMhwK-YeBtGvNDIlTXI9r2y6AhYy1TxkJr51TDqO3VewGo9jrsTaR5yMzKCcarvm9WKLSWzhezqQ=s90)

Please see the following tutorial:
http://www.codeproject.com/KB/system/ftdi-chip.aspx (http://www.codeproject.com/KB/system/ftdi-chip.aspx)

It worked until establishing connection to FTDI. But somehow I am getting reading error. My Arduino is using Serial.print to print 0..1.. ... ... 9 every 300ms. Meaning it prints 1 then after 300ms prints 2 and so on.

Any suggestion will be appreciated.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: Ro-Bot-X on April 01, 2011, 06:32:38 PM
I have no idea how the FTDI API works. But, here is what you can do:

- in your microcontroller code set the serial port baud is set to 9600 (you can set it latter to a different value)
- connect the breakout board to your microcontroller, make sure the Tx and Rx pins from the breakout board are connected to the Rx and Tx respectively on the microcontroller, in other words, they are swapped
- in Arduino, open the Serial Monitor window and set the baud rate to 9600.

If it's all good, you should see the numbers appear on the screen. Then try out your API, and make sure the baud rate is set at 9600. If it works, try to set the baud rate to the value you need, but make sure you set it in the mocrocontroller code AND in the API.

Good luck!
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: reefat on April 01, 2011, 08:06:05 PM
@Ro-Bot-X: I already tried that. By setting up the safe baud rate 9600. It shows up on the arduino console. But my Visual C++ program couldn't read the serial data from arduino. Did you ever make any serial programmer to burn program into your microcontroller? If so, you will probably understand what I'm trying to do.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: Ro-Bot-X on April 01, 2011, 09:01:45 PM
Nope, never done that. I am mostly a hardware guy, not a software guy. I wish I had the knowledge in th e software department as well...
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: rbtying on April 01, 2011, 09:05:07 PM
Try using POSIX C to interface with whatever Serial Port (\\.\COM*) is being emulated by the FTDI cable.  Remember to use a baud rate of 9600, 8 bit packets, no parity bits, one stop bit, and no flow control.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: joe61 on April 01, 2011, 09:47:45 PM
@Ro-Bot-X: I already tried that. By setting up the safe baud rate 9600. It shows up on the arduino console. But my Visual C++ program couldn't read the serial data from arduino. Did you ever make any serial programmer to burn program into your microcontroller? If so, you will probably understand what I'm trying to do.

Does your C++ program on the PC try to open the same serial interface that the Arduino IDE uses? If so, does it try to do this while the Arduino IDE is still using it?

Joe
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: reefat on April 01, 2011, 11:11:16 PM
@Joe: Yes it's the same COM port 4. And when I run my Visual C++ program, Arduino IDE is not using it.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: reefat on April 01, 2011, 11:18:17 PM
Try using POSIX C to interface with whatever Serial Port (\\.\COM*) is being emulated by the FTDI cable.  Remember to use a baud rate of 9600, 8 bit packets, no parity bits, one stop bit, and no flow control.

@rbtying: You sound promising. Yes, I'm using the same settings you mentioned.

Here is the complete code. (Don't forget to add ftd2xx.lib to the linker.)
Code: [Select]
/*
 * http://www.codeproject.com/KB/system/ftdi-chip.aspx
 */
#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <exception>
#include "ftd2xx.h"

int _tmain(int argc, _TCHAR* argv[])
{
FT_STATUS ftStatus;
FT_HANDLE ftHandle;

DWORD TotalRead=0;
_TCHAR Buf[64];

OVERLAPPED osReader = {0};

ftStatus = FT_ListDevices(0, Buf, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER);

if (ftStatus!=FT_OK)
{
printf("Couldn't find FTDI device!\n");
return 0;
}

ftHandle = FT_W32_CreateFile(Buf,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FT_OPEN_BY_SERIAL_NUMBER,
0);

if (ftHandle != INVALID_HANDLE_VALUE)
{
try
{
//setting up the port
ftStatus=FT_SetBaudRate(ftHandle, FT_BAUD_9600);
if (ftStatus!=FT_OK)
throw std::exception("Can't set baud rate");

ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);

if (ftStatus!=FT_OK)
throw std::exception("Can't set data characteristics");

// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (osReader.hEvent == NULL)
{
printf("Error creating overlapped event.");
return 0;
}

char Buf[256];

FT_W32_ReadFile(ftHandle, Buf, sizeof(Buf), &TotalRead, &osReader);
if (WaitForSingleObject(osReader.hEvent, 1000) != WAIT_OBJECT_0)
throw std::exception("Error waiting read operation");
}
catch (std::exception & e)
{
printf("%d\n", TotalRead);
printf(e.what());
}

FT_W32_CloseHandle(ftHandle);
}


return 0;
}

Do you have any code snippet that I could use. It would be a lot helpful.
Thanks.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: kl22 on April 12, 2011, 12:44:01 AM
Just curious on why you are using the FTDI API rather than just a serial connection?

I have the same breakout board, it provides me with a serial port (Com Port) when i connect it to my PC.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: reefat on April 12, 2011, 08:41:39 AM
Just curious on why you are using the FTDI API rather than just a serial connection?

I have the same breakout board, it provides me with a serial port (Com Port) when i connect it to my PC.

You mean, instead of using FTDI API, using windows serial communication API? They look identical. And I got it running using the windows default COM API, where the FTDI API failed.
Title: Re: How to program to communicate between FTDI & Arduino?
Post by: kl22 on April 12, 2011, 10:58:17 AM
Yep thats wat i meant :D