static int bitforce_autodetect_ftdi(void) { char devpath[] = "\\\\.\\COMnnnnn"; char *devpathnum = &devpath[7]; char **bufptrs; char *buf; int found = 0; int i; FT_STATUS ftStatus; DWORD numDevs; HMODULE dll = LoadLibrary("FTD2XX.DLL"); if (!dll) { applog(LOG_DEBUG, "FTD2XX.DLL failed to load, not using FTDI bitforce autodetect"); return 0; } LOAD_SYM(FT_ListDevices); LOAD_SYM(FT_Open); LOAD_SYM(FT_GetComPortNumber); LOAD_SYM(FT_Close); ftStatus = FT_ListDevices(&numDevs, NULL, FT_LIST_NUMBER_ONLY); if (ftStatus != FT_OK) { applog(LOG_DEBUG, "FTDI device count failed, not using FTDI bitforce autodetect"); goto out; } applog(LOG_DEBUG, "FTDI reports %u devices", (unsigned)numDevs); buf = alloca(65 * numDevs); bufptrs = alloca(numDevs + 1); for (i = 0; i < numDevs; ++i) bufptrs[i] = &buf[i * 65]; bufptrs[numDevs] = NULL; ftStatus = FT_ListDevices(bufptrs, &numDevs, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); if (ftStatus != FT_OK) { applog(LOG_DEBUG, "FTDI device list failed, not using FTDI bitforce autodetect"); goto out; } for (i = numDevs; i > 0; ) { --i; bufptrs[i][64] = '\0'; if (!(strstr(bufptrs[i], "BitFORCE") && strstr(bufptrs[i], "SHA256"))) continue; FT_HANDLE ftHandle; if (FT_OK != FT_Open(i, &ftHandle)) continue; LONG lComPortNumber; ftStatus = FT_GetComPortNumber(ftHandle, &lComPortNumber); FT_Close(ftHandle); if (FT_OK != ftStatus || lComPortNumber < 0) continue; sprintf(devpathnum, "%d", (int)lComPortNumber); if (bitforce_detect_one(devpath)) ++found; } out: dlclose(dll); return found; }
FT_STATUS GetComPort( LONG * comPort ) { //FTD2xx: // FT_GetDriverVersion , FT_GetLibraryVersion // FT_GetDeviceInfo , FT_CreateDeviceInfoList , FT_GetDeviceInfoDetail // FT_GetComPortNumber // FT_CyclePort , FT_ResetPort , FT_ResetDevice , FT_GetStatus FT_HANDLE ftHandle; FT_STATUS ftStatus; uint8_t * version; #define VER_BUILD 0 #define VER_MINOR 1 #define VER_MAJOR 2 #define VER_UNUSED 3 DWORD dwLibraryVer; // Get DLL version ftStatus = FT_GetLibraryVersion(&dwLibraryVer); if (ftStatus == FT_OK) { version = (uint8_t *)&dwLibraryVer; //ASSUMPTION: dwLibraryVer is little-endian std::cout << "FTD2xx Library " << int(version[VER_MAJOR]) << "." << int(version[VER_MINOR]) << " Build " << int(version[VER_BUILD]) << std::endl; } else { std::cerr << "ERROR: FT_GetLibraryVersion()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } DWORD dwDriverVer; // Get driver version ftStatus = FT_Open(0, &ftHandle); if (ftStatus == FT_OK) { ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer); if (ftStatus == FT_OK) { version = (uint8_t *)&dwDriverVer; //ASSUMPTION: dwLibraryVer is little-endian std::cout << "FTD2xx Driver " << int(version[VER_MAJOR]) << "." << int(version[VER_MINOR]) << " Build " << int(version[VER_BUILD]) << std::dec << std::endl; } else { std::cerr << "ERROR: FT_GetDriverVersion()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } FT_Close(ftHandle); } else { std::cerr << "ERROR: FT_Open()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } DWORD numDevs; ftStatus = FT_ListDevices(&numDevs, NULL, FT_LIST_NUMBER_ONLY); if (ftStatus == FT_OK) { // FT_ListDevices OK, number of devices connected is in numDevs } else { // FT_ListDevices failed std::cerr << "ERROR: FT_ListDevices()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } DWORD numDevs2; // create the device information list ftStatus = FT_CreateDeviceInfoList(&numDevs2); if (ftStatus == FT_OK) { } else { // FT_CreateDeviceInfoList failed std::cerr << "ERROR: FT_CreateDeviceInfoList()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } if (numDevs != numDevs2) { std::cerr << "ERROR: number of devices from FT_ListDevices() and FT_CreateDeviceInfoList() are different" << std::endl; return ftStatus; } if (numDevs == 0) { std::cerr << "ERROR: numDevs==0 ; ftStatus==FT_DEVICE_NOT_FOUND (2) ???" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } if (numDevs > 1) { std::cerr << "ERROR: numDevs > 1" << std::endl; std::cerr << "ERROR: returning numDevs" << std::endl; return numDevs; } std::cout << "1 FTDI device found" << std::endl; /* DWORD devIndex = 0; // first device char buffer[64]; // more than enough room! ftStatus = FT_ListDevices((PVOID)devIndex, buffer, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); if (ftStatus == FT_OK) { // FT_ListDevices OK, serial number is in Buffer } else { // FT_ListDevices failed std::cerr << "ERROR: FT_ListDevices()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } std::cout << buffer << std::endl; */ //FT_DEVICE_LIST_INFO_NODE * devInfo; // allocate storage for list based on numDevs //devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs); FT_DEVICE_LIST_INFO_NODE devInfo[1]; // get the device information list ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs); if (ftStatus == FT_OK) { //for (unsigned int i = 0; i < numDevs; i++) //{ unsigned int i = 0; //std::cout << "Dev " << i << ":" << std::endl; std::cout << std::hex; std::cout << " Flags=0x" << devInfo[i].Flags << std::endl; std::cout << " Type=0x" << devInfo[i].Type << std::endl; std::cout << " ID=0x" << devInfo[i].ID << std::endl; std::cout << " LocId=0x" << devInfo[i].LocId << std::endl; std::cout << " ftHandle=0x" << devInfo[i].ftHandle << std::endl; std::cout << std::dec; std::cout << " SerialNumber=" << devInfo[i].SerialNumber << std::endl; std::cout << " Desc=" << devInfo[i].Description << std::endl; //} } else { std::cerr << "ERROR: FT_GetDeviceInfoList()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } ftStatus = FT_Open(0, &ftHandle); if (ftStatus == FT_OK) { ftStatus = FT_GetComPortNumber(ftHandle, comPort); if (ftStatus == FT_OK) { if(*comPort == -1) { // No COM port assigned } else { // COM port assigned with number held in *comPort } } else { // FT_GetComPortNumber FAILED! std::cerr << "ERROR: FT_GetComPortNumber()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } FT_Close(ftHandle); if (ftStatus == FT_OK) { } else { std::cerr << "ERROR: FT_Close()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } } else { std::cerr << "ERROR: FT_Open()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } return FT_OK; }
int main(int argc, char* argv[]) { //for com reader FT_HANDLE fthandle; FT_STATUS res; LONG COMPORT; char COMx[5]; int n; DCB dcb; HANDLE hCommPort; BOOL fSuccess; /*********************************************************************** //Find the com port that has been assigned to your device. /***********************************************************************/ res = FT_Open(0, &fthandle); if(res != FT_OK){ printf("opening failed! with error %d\n", res); return 1; } res = FT_GetComPortNumber(fthandle,&COMPORT); if(res != FT_OK){ printf("get com port failed %d\n", res); return 1; } if (COMPORT == -1){ printf("no com port installed \n"); } else{ printf("com port number is %d\n", COMPORT); } FT_Close(fthandle); /********************************************************/ // Open the com port assigned to your device /********************************************************/ n = sprintf_s(COMx, "COM%d",COMPORT); hCommPort = CreateFile( COMx, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hCommPort == INVALID_HANDLE_VALUE) { printf("Help - failed to open\n"); return(1); } printf("Hello World!\n"); /********************************************************/ // Configure the UART interface parameters /********************************************************/ fSuccess = GetCommState(hCommPort, &dcb); if (!fSuccess) { printf("GetCommStateFailed \n", GetLastError()); return (2); } //set parameters. dcb.BaudRate = 2400; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; //dcb.fDsrSensitivity = 1; dcb.fDtrControl = DTR_CONTROL_ENABLE; fSuccess = SetCommState(hCommPort, &dcb); //set TimeOuts COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 100; fSuccess = SetCommTimeouts(hCommPort, &timeouts); if (!fSuccess) { printf("SetCommStateFailed \n", GetLastError()); return (3); } printf("Port configured \n"); /********************************************************/ // Writing data to the USB to UART converter /********************************************************/ DWORD data_len = 12; /* DWORD dwwritten = 0, dwErr; char data_out[12] = "HELLO WORLD"; fSuccess = WriteFile(hCommPort, &data_out, data_len, &dwwritten, NULL); if (!fSuccess) { dwErr = GetLastError(); printf("Write Failed \n", GetLastError()); return (4); } printf("bytes written = %d\n", dwwritten); */ /********************************************************/ //Reading data from the USB to UART converter /********************************************************/ Sleep(200); char buf[256]; DWORD dwRead; while(hCommPort != INVALID_HANDLE_VALUE) { memset(buf,0,256); EscapeCommFunction(hCommPort,SETDTR); if (ReadFile(hCommPort, buf, data_len, &dwRead, NULL)) { printf("data read = %s\n", buf); } EscapeCommFunction(hCommPort,CLRDTR); //Sleep(2000); } /********************************************************/ //Closing the device at the end of the program /********************************************************/ CloseHandle(hCommPort); getchar(); return 0; }
int main(int argc, char* argv[]) { FT_HANDLE fthandle; FT_STATUS res; LONG COMPORT; char COMx[5]; int n; DCB dcb; HANDLE hCommPort; BOOL fSuccess; /*********************************************************************** //Find the com port that has been assigned to your device. /***********************************************************************/ res = FT_Open(0, &fthandle); if(res != FT_OK){ printf("opening failed! with error %d\n", res); return 1; } res = FT_GetComPortNumber(fthandle,&COMPORT); if(res != FT_OK){ printf("get com port failed %d\n", res); return 1; } if (COMPORT == -1){ printf("no com port installed \n"); } else{ printf("com port number is %d\n", COMPORT); } FT_Close(fthandle); /********************************************************/ // Open the com port assigned to your device /********************************************************/ n = sprintf(COMx, "COM%d",COMPORT); hCommPort = CreateFile( COMx, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hCommPort == INVALID_HANDLE_VALUE) { printf("Help - failed to open\n"); return(1); } printf("Hello World!\n"); /********************************************************/ // Configure the UART interface parameters /********************************************************/ fSuccess = GetCommState(hCommPort, &dcb); if (!fSuccess) { printf("GetCommStateFailed \n", GetLastError()); return (2); } //set parameters. dcb.BaudRate = 115200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; fSuccess = SetCommState(hCommPort, &dcb); if (!fSuccess) { printf("SetCommStateFailed \n", GetLastError()); return (3); } printf("Port configured \n"); /********************************************************/ // Writing data to the USB to UART converter /********************************************************/ /*DWORD dwwritten = 0, dwErr; char data_out[12] = "HELLO WORLD"; DWORD w_data_len = 12; fSuccess = WriteFile(hCommPort, &data_out, w_data_len, &dwwritten, NULL); if (!fSuccess) { dwErr = GetLastError(); printf("Write Failed \n", GetLastError()); return (4); } printf("bytes written = %d\n", dwwritten);*/ /********************************************************/ //Reading data from the USB to UART converter /********************************************************/ bool running = true; //while(hCommPort != INVALID_HANDLE_VALUE) //{ char buf[256]; DWORD dwActuallyRead; DWORD dwToRead = 12; bool result = false; memset(buf,0,256); result = ReadFile(hCommPort, buf, dwToRead , &dwActuallyRead, NULL); if(dwActuallyRead != 0) { printf("data read = %s\n", &buf); } else { printf("read not what we want %s - error=%s\n",buf,GetLastError()); } //} /********************************************************/ //Closing the device at the end of the program /********************************************************/ CloseHandle(hCommPort); getchar(); return 0; }
void get_info(void) { UCHAR Mask = 0xff; UCHAR Mode = 1; // Set asynchronous bit-bang mode LONG lComPortNumber; DWORD numDevs; //UCHAR BitMode; char ManufacturerBuf[32]; char ManufacturerIdBuf[16]; char DescriptionBuf[64]; char SerialNumberBuf[16]; ftData.Signature1 = 0x00000000; ftData.Signature2 = 0xffffffff; ftData.Version = 0x00000002; ftData.Manufacturer = ManufacturerBuf; ftData.ManufacturerId = ManufacturerIdBuf; ftData.Description = DescriptionBuf; ftData.SerialNumber = SerialNumberBuf; ftStatus = FT_CreateDeviceInfoList(&numDevs); if (ftStatus == FT_OK) { printf("Number of devices is %d\n",numDevs); } if (numDevs > 0) { // allocate storage for list based on numDevs devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs); // get the device information list ftStatus = FT_GetDeviceInfoList(devInfo,&numDevs); if (ftStatus == FT_OK) { for (i = 0; i < numDevs; i++) { printf("Dev %d:\n",i); printf(" Flags=0x%x\n",devInfo[i].Flags); printf(" Type=%s\n",devices[devInfo[i].Type]); printf(" ID=0x%x\n",devInfo[i].ID); printf(" LocId=0x%x\n",devInfo[i].LocId); printf(" SerialNumber=%s\n",devInfo[i].SerialNumber); printf(" Description=%s\n",devInfo[i].Description); // printf(" ftHandle=0x%x\n",devInfo[i].ftHandle); } } i=0; //open first device ftStatus = FT_Open(i,&ftHandle); if (ftStatus != FT_OK) { printf("Can't open %s device! \n",devInfo[i].Description); } else { printf("Successfully open %s device! \n",devInfo[i].Description); ftStatus = FT_GetComPortNumber(ftHandle,&lComPortNumber); if (ftStatus == FT_OK) { if (lComPortNumber == -1) { printf(" NO com port Assigned!\n"); } else { // COM port assigned with number held in lComPortNumber printf(" Current assigned COM Port: %d \n",lComPortNumber); } } else { printf(" Failed to get the COM Port!\n"); } ftStatus = FT_EE_Read(ftHandle, &ftData); if (ftStatus == FT_OK) { // FT_EE_Read OK, data is available in ftData printf(" EEPROM READ OK\n"); printf("Signature1 = 0x%04x\n", ftData.Signature1); printf("Signature2 = 0x%04x\n", ftData.Signature2); printf("Version = 0x%04x\n", ftData.Version); printf("VendorID = 0x%04x\n", ftData.VendorId); printf("ProductID = 0x%04x\n", ftData.ProductId); printf("Manufacturer = %s\n", ftData.Manufacturer); printf("ManufacturerID = %s\n", ftData.ManufacturerId); printf("Description = %s\n", ftData.Description); printf("SerialNumber = %s\n", ftData.SerialNumber); printf("MaxPower = %d\n", ftData.MaxPower); printf("PnP = %x\n", ftData.PnP); printf("SelfPowered = %x\n", ftData.SelfPowered); printf("RemoteWakeup = %x\n", ftData.RemoteWakeup); printf("Use Ext Osc = %x\n", ftData.UseExtOsc); printf("High Drives = %x\n", ftData.HighDriveIOs); printf("Endpoint Size = %x\n", ftData.EndpointSize); printf("Pull Down Enabled = %x\n", ftData.PullDownEnableR); printf("Serial Number Enabled = %x\n", ftData.SerNumEnableR); printf("Invert TXD = %x\n", ftData.InvertTXD); printf("Invert RXD = %x\n", ftData.InvertRXD); printf("Invert RTS = %x\n", ftData.InvertRTS); printf("Invert CTS = %x\n", ftData.InvertCTS); printf("Invert DTR = %x\n", ftData.InvertDTR); printf("Invert DSR = %x\n", ftData.InvertDSR); printf("Invert DCD = %x\n", ftData.InvertDCD); printf("Invert RI = %x\n", ftData.InvertRI); printf("CBUS0 = 0X%02X\n", ftData.Cbus0); printf("CBUS1 = 0X%02X\n", ftData.Cbus1); printf("CBUS2 = 0X%02X\n", ftData.Cbus2); printf("CBUS3 = 0X%02X\n", ftData.Cbus3); printf("CBUS4 = 0X%02X\n", ftData.Cbus4); } else { // FT_EE_Read FAILED! printf(" EEPROM READ FAILED\n"); } FT_Close(ftHandle); } } else { printf("No FT232 Device found! \n"); } }