#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
#include "ftd2xx.h"
#include <time.h>
using namespace std;
int * binary(int);
//void usleep(__int64);
int main(void)
{
FT_HANDLE ftHandle;
FT_STATUS ftstatus;
ftstatus = FT_Open(0, &ftHandle);
if (ftstatus != FT_OK){
cout << "Opening failed!" << endl;
int a;
cin >> a;
return 1;
}
else
cout << "FT Open sucssesfull" << endl;
//Set Baud Rate
ftstatus = FT_SetBaudRate(ftHandle, 49000);
if (ftstatus == FT_OK)
cout << "FT_SetBaudRate: OK!" << endl;
else
cout << "FT_SetBaudRate: FAILED!" << endl;
//Data Characteristics
FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0);
////////////////////////////////////////////////////////////////////
int number = 0;
int bit = 0;
unsigned char CntrlSig[1];
int FileNum = 0;
unsigned char TST[2];
unsigned int temp1 = 0;
unsigned char RX[7296];
/////////////////////////////////////////////////////////////////////////
while (number != -1 || bit != -1){
bool bitistrue = false;
cout << "Please enter 0 or 1: ";
while (bitistrue == false)
{
cin >> bit;
if (bit == 0 || bit == 1)
bitistrue = true;
else
cout << "Wrong number " << endl;
}
////////////////////////////////////////////////////////////////////
cout << "Please enter a positive integer between 0 and 127: ";
bool numberistrue = false;
while (numberistrue == false)
{
cin >> number;
if (number < 0 || number > 127)
cout << "That is not a valid number" << endl;
else
numberistrue = true;
}
/////////////////////////////////////////////////////////////////////
FileNum++;
string FileName = "TCD_Data" + to_string(FileNum) + ".dat";
cout << "File Name is : ";
cout << FileName << endl;
int tmp = bit * pow(2, 7) + binary(number)[6] * pow(2, 6)
+ binary(number)[5] * pow(2, 5) + binary(number)[4] * pow(2, 4)
+ binary(number)[3] * pow(2, 3) + binary(number)[2] * pow(2, 2)
+ binary(number)[1] * 2 + binary(number)[0];
CntrlSig[0] = (unsigned char)tmp;
cout << "Control Byte is :";
cout << tmp << endl;
////////////////////////////////////////////////////////////////
//Set Asynchronous Bit-bang Mode
UCHAR Mask = 0xff;//set all pins output
UCHAR mode = 0x1;//Set asynchronous bit-bang mode
ftstatus = FT_SetBitMode(ftHandle, Mask, mode);
if (ftstatus == FT_OK)
cout << "ALL Pins output : OK! " << endl;
else
cout << "ALL Pins output : FAILED!" << endl;
////////////////////////////////////////////////////////////////
DWORD BytesWritten;
DWORD EventDWord_wr;
DWORD Txbytes_wr;
DWORD RxBytes_wr;
FT_GetStatus(ftHandle, &RxBytes_wr, &Txbytes_wr, &EventDWord_wr);
cout << "RxBytes_wr is :";
cout << RxBytes_wr << endl;
cout << "TxBytes_wr is :";
cout << Txbytes_wr << endl;
ftstatus = FT_Write(ftHandle, CntrlSig, 1, &BytesWritten);
if (ftstatus == FT_OK)
cout << "FT_Write OK" << endl;
else
cout << "FT_Write Failed" << endl;
////////////////////////////////////////////////////////////////
//Set Asynchronous Bit-bang Mode
Mask = 0x00;//set all pins input
ftstatus = FT_SetBitMode(ftHandle, Mask, mode);
if (ftstatus == FT_OK)
cout << "ALL Pins input : OK! " << endl;
else
cout << "ALL Pins input : Failed!" << endl;
////////////////////////////////////////////////////////////////
DWORD EventDWord;
DWORD Txbytes;
DWORD RxBytes;
DWORD BytesReceived;
//int wait = (number + 1) * 125 * 2.4 + 800;
//usleep(wait);
bool chkRead = false;
bool chkFile = false;
//FOR WRITE TO FILE IF DATA IS VALID
if (bit == 1){
bool zeroisone = false;
bool oneisone = false;
while (zeroisone == false || oneisone == false){
FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
if (&RxBytes > 0){
ftstatus = FT_Read(ftHandle, TST, 2, &BytesReceived);
}
if ((unsigned int)TST[0] == 1)
zeroisone = true;
else
zeroisone = false;
if ((unsigned int)TST[1] == 1)
oneisone = true;
else
oneisone = false;
}
//////////////////---/-/-/-/--/-/-/-/--/////////////////////////////
///Event_Read
HANDLE hEvent;
DWORD EventMask;
hEvent = CreateEvent(NULL, false, false, NULL);
EventMask = FT_EVENT_RXCHAR;
DWORD EventDWord;
DWORD Status;
int y = 0;
while (1){
ftstatus = FT_SetEventNotification(ftHandle, EventMask, hEvent);
WaitForSingleObject(hEvent, INFINITE);
FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
if (RxBytes > 0) {
ftstatus = FT_Read(ftHandle, &RX[y], 1, &BytesReceived);
if (ftstatus == FT_OK)
y++;
cout << y << endl;
if (y == 200){
y = 0;
break;
}
}
}
///////////////-/-/--/-/-/////////////-------///////////////////////
cout << "Protocol Bytes Recived" << endl;
if (zeroisone == true && oneisone == true){
FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
cout << "RxBytes is :";
cout << RxBytes << endl;
cout << "TxBytes is :";
cout << Txbytes << endl;
/*if (&RxBytes > 0){
ftstatus = FT_Read(ftHandle, RX, 7296, &BytesReceived);
if (ftstatus == FT_OK){// && chkRead == false){
cout << "FT_Read OK" << endl;
//chkRead = true;
}
else if (ftstatus != FT_OK){
cout << "FT_Read Failed" << endl;
//chkRead = false;
}
}*/
// open a file in write mode.
ofstream outfile;
outfile.open(FileName, ios::out);
for (int i = 0; i < 7296; i++){
//if (i % 2 == 0){
temp1 = (unsigned int)RX[i];
//}
//else if (i % 2 == 1){
//temp1 = temp1 << 8;
//temp1 = temp1 + (unsigned int)RX[i];
outfile << temp1 << endl;
//}
}// FOR
outfile.close();
}//WHILE ONEISONE .. IS FALSE
}// if (bit==1)
else if (bit == 0){
unsigned char TST100[2];
unsigned char RX[100][7296];
unsigned int temp2 = 0;
bool zeroisone100 = false;
bool oneisone100 = false;
// open a file in write mode.
ofstream outfile;
outfile.open(FileName, ios::out);
for (int i = 0; i < 100; i++)
{
bool chkRead100 = false;
bool chkFile100 = false;
while (zeroisone100 == false || oneisone100 == false){
FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
if (&RxBytes > 0){
ftstatus = FT_Read(ftHandle, TST100, 2, &BytesReceived);
}
if ((unsigned int)TST100[0] == 254)
zeroisone100 = true;
else
zeroisone100 = false;
if ((unsigned int)TST100[1] == 254)
oneisone100 = true;
else
oneisone100 = false;
zeroisone100 = true;
oneisone100 = true;
}
if (zeroisone100 == true && oneisone100 == true){
cout << "Protocol Bytes Recived" << endl;
FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
if (&RxBytes > 0){
ftstatus = FT_Read(ftHandle, RX[i], 7296, &BytesReceived);
if (ftstatus == FT_OK){// && chkRead100 == false){
cout << "FT_Read OK" << endl;
//chkRead100 = true;
}
else if (ftstatus != FT_OK){
cout << "FT_Read Failed" << endl;
//chkRead100 = false;
}
}
for (int h = 0; h < 7296; h++){
// if (chkFile100 == false){
//cout << "Cannot Read! Data Not Received!!!" << endl;
//chkFile100 = true;
//}
if (h % 2 == 0){
temp1 = (unsigned int)RX[i][h];
}
else if (h % 2 == 1){
temp1 = temp1 << 8;
temp1 = temp1 + (unsigned int)RX[i][h];
outfile << temp1 << endl;
}
}// FOR
outfile << " ************** End of Data **************";
}//WHILE ONEISONE .. IS FALSE
}//for 100
outfile.close();
}//end if (bit = 0)
}//while!=-1
return 0;
}
int * binary(int number)
{
int remainder;
int bi[8];
for (int i = 0; i < 8; i++)
{
remainder = number % 2;
number = (number >> 1);
bi[i] = remainder;
}
return bi;
}
entity test1 is
port( Rxf,Txf : In std_logic;
Wr,Rd : out std_logic:='0';
data : inout std_logic_vector(7 downto 0):="00000000";
Clk : in std_logic;--50 mhz
lamp : out std_logic
);
end test1;
--/////////////////////////////////////////////
architecture Behavioral of test1 is
signal Counter : std_logic_vector(7 downto 0) :=(others => '0');
signal state : std_logic_vector(1 downto 0):="00";
begin
Counter <= Counter + '1' when clk'event and clk = '1';
Rd <= '1';
lamp <= '0';
process(Counter(7))
begin
If Counter(7)'event and Counter(7) = '1' then
Case State is
when "00" =>
If Txf = '0' then
state <= state + "01";
data <= data + "00000001";
end if;
when "01" =>
Wr <= '1' ;
state <= "10";
when "10" =>
Wr <= '0';
state <="00";
when others =>
state <= "00";
end case;
end if;
end process;
Nothing. Keep it simple. Start your transmission.so what mode i must use after i reset the Ft?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?