Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by em ( 15 years ago )
/*
 * This software is supplied on an as-is basis and no warranty as to their suitability for any particular purpose is either 
 * made or implied. Future Technology Devices International Ltd. will not accept any claim for damages howsoever arising as 
 * a result of use or failure of this software. Your statutory rights are not affected. This software or any variant of it 
 * is not intended for use in any medical appliance, device or system in which the failure of the product might reasonably be 
 * expected to result in personal injury. This document provides preliminary information that may be subject to change 
 * without notice.
 * 
 */

/*
 * The following code assumes a FT232R chip is connected to a PC with the RXD pin and the TXD pin shorted together.
 * The FTDI VCP driver must be installed on the PC.
 * The first part of the code determines the com port assigned to the device automatically, so the user does not have to
 * check in device manager.
 *
 * This com port number is then then opened with the standard W32 Serial COM API calls.
 * Data is then sent to the device which will loop back on the wire used to short RXD to TXD and the application reads the
 * data to prove it looped round.
 */


#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "ftd2xx.h"
#include "commands.h"

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&#40;
   COMx,
            GENERIC_READ | GENERIC_WRITE,
            0,
            0,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL
            &#41;;

  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[11] = {TAG_CMD, 0x04, CMD_RUN, 0xCD, TAG_CMD,0x07,CMD_SET_PARAM_VALUE,PARAM_TARGET_POS,0x40,0x21,0x7E};
 DWORD w_data_len = 11;
   

fSuccess = WriteFile&#40;hCommPort, &data;_out, w_data_len, &dwwritten;, NULL&#41;;
 
  
  if (!fSuccess) 

  {
   dwErr = GetLastError();
   printf("Write Failed %d \n", FT_W32_GetLastError(hCommPort));
   //return (4);

  }

  
  printf("bytes written = %d\n", dwwritten);
  int d;
  //scanf("%d",&d);
   
/********************************************************/
//Reading data from the USB to UART converter
/********************************************************/
  
 char buf[256];
 DWORD dwRead;


 memset(buf,0,256);


 if (ReadFile&#40;hCommPort, buf, 8, &dwRead;,NULL&#41;)

   {

    printf("ile: %d data read = %x\n", dwRead,buf[0]);
    for(int i=0;i<dwRead;i++)
     printf("0xX ", (unsigned char)buf[i]);


   }

/********************************************************/
//Closing the device at the end of the program
/********************************************************/


    CloseHandle(hCommPort);

 getchar();
 return 0;
}

 

Revise this Paste

Your Name: Code Language: