DE1のFT245向けD2XXドライバ使用通信テスト

  • タグ:
  • タグはありません
#include<windows.h>

#include<stdio.h>
#include<time.h>

#include "ftd2xx.h"
#pragma comment(lib, "ftd2xx.lib")

int main(void)
{
	char dbuf[64];
	FT_ListDevices(0, dbuf, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION);
	printf("%s\n", dbuf);

	FT_STATUS ftStatus;
	FT_HANDLE ftHandle;

	ftStatus = FT_OpenEx(dbuf, FT_OPEN_BY_DESCRIPTION, &ftHandle);
	if(ftStatus!=FT_OK){
		printf("Failed to open port\n");
		return 0;
	}

	FT_ResetDevice(ftHandle);
	FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);

	FT_SetTimeouts(ftHandle, NULL, NULL);

	ftStatus = FT_SetBaudRate(ftHandle, FT_BAUD_921600);
	if(ftStatus!=FT_OK){
		printf("Failed to set BaudRate\n");
	}

	ftStatus = FT_SetBitMode(ftHandle, 0xFF/*1=out 0=in*/, 1/*bit-bang mode on*/);
	if(ftStatus!=FT_OK){
		printf("Failed to set bit mode\n");
	}

	int i;
	int total = 0;
	const int size = 62*16;
	unsigned char buf[size];
	DWORD send_size;
	FT_STATUS ftTxStatus;

	for(i=0; i<size; i++) buf[i] = rand();
	buf[size-1] = 0;

clock_t st, et;
st = clock();

	for(i=0; i<10; i++){
		ftTxStatus = FT_Write(ftHandle, buf, size, &send_size);
		if(ftTxStatus!=FT_OK){
			printf("send err\n");
		}
		if(send_size!=size){
			printf("send err\n");
		}
		total += size;
	}

et = clock();
clock_t u = et - st;
double s = u / 1000.0;
printf("%0.1f Kbps\n", total/s/1024);

	FT_Close(ftHandle);

	return 0;
}