Crea sito
Stampa

Codice Sorgente Semplificato Per UM245R

Scritto da Primok_V. Postato in Cat_Schemi Elettronici

Valutazione attuale:  / 1
ScarsoOttimo 






Il codice è stato richiesto da un utente.Questo è quello che ha scritto:

Good morning,
 
I'm writing you because I have a module UM245R and I am doing a project and I would like to use it to switch on and off some LEDs. So, I found really useful your application done. Maybe I don't need an interface so big, with to much modes of operation, and so big electronic circuit. I think it could be ok with a transistor in each output to amplify the current and a resistor with each LED to control the current on it.
I downloaded the trial version, and execute it and find really interesting. Does exist any possibility to get the code to have an idea how do you do to comunicate the application with the electronic circuit? It would be great... 
 
Any suggestion or recommendation wil be welcome.
 
Thank you very much.

You need to know both C language and Borland C++ 6.0 compiler to understand the source code.
The source code I wrote allows you to control singly 8 outputs using the buttons.
The button number 9 "Device_List" , allows you to scan the PC and find all connected devices.
It's necessary to scan the PC before checking the outputs.  if no  device is connected , you can't control output.
For each button a function is associated that is actived when you pressed it.
Within each function, a Boolean variable changes its state after pressing the corresponding button.
For example, if the variable is true, after pushing it, it becomes false and averse....

 

The function body associated with each button is:

void __fastcall TForm1::Ch1Click(TObject *Sender)

{
if(devive_found) // If the device is found execute the body

                       // else don't do anything

{ //beginning of the body

      ch1=!ch1;  // change its state if the button is pressed.
  if(ch1==true)   // if ch1 is true then

  {   
Open_Close_Write(TxBuffer[0]|0x01);// send data

  }
else // if ch1 is false

  {
  Open_Close_Write(TxBuffer[0] &0xFE);// send data

  }

  }// end of body

}

That's All.


//---------------------------------------------------------------------------
         The source code
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <iostream.h>
#include "Lluis.h"
#include <stdlib.h>
#include "FTD2XX.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

  DWORD    Flags;
  DWORD    ID;
  DWORD    Type;
  DWORD    LocId;
  UCHAR    MASK=0xFF;
  UCHAR    MODE=0x1;
  FT_HANDLE   ftHandleTemp;
  DWORD       numDevs;
  FT_STATUS   ftStatus;
  FT_DEVICE_LIST_INFO_NODE *devInfo;
  FT_HANDLE ftHandle;
  DWORD BytesWritten;

   boolean  ch1=false,ch2=false,ch3=false,ch4=false,ch5=false,ch6=false,
   ch7=false,ch8=false, devive_found=false;
     char TxBuffer[1] ;
   int val_ch;

   void  Open_Close_Write(int buff);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Device_ListClick(TObject *Sender)
{

       numDevs=0;
        ftStatus=FT_CreateDeviceInfoList(&numDevs);

if (ftStatus == FT_OK)

       if(IntToStr(numDevs)==0)
   ListBox1->Items->Add("No Device Found" )  ;
     else     {
     ListBox1->Items->Add("N_Devices="+ IntToStr(numDevs) )  ;
              devive_found=true;
              }
if (numDevs > 0)
  {


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 (int i = 0; i < (int) numDevs; i++)
        {
//printf("Dev %d:\n",i);
   ListBox1->Items->Add("Dev "+ IntToStr(i) )  ;
   ListBox1->Items->Add("Flags="+ IntToStr(devInfo[i].Flags) )  ;
   ListBox1->Items->Add("Type="+ AnsiString(devInfo[i].Type))  ;
   ListBox1->Items->Add("ID="+String((devInfo[i].ID)) )  ;
   ListBox1->Items->Add("SerialNumber="+ AnsiString(devInfo[i].SerialNumber ) ) ;
   ListBox1->Items->Add("Description="+ AnsiString(devInfo[i].Description ) ) ;
   ListBox1->Items->Add("ftHandle="+ IntToStr(devInfo[i].ftHandle )) ;

         }  //ciclo for
     } //if status ok

 }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch1Click(TObject *Sender)
{
 if(devive_found)
   {
      ch1=!ch1;
  if(ch1==true)
  {
    Open_Close_Write(TxBuffer[0]|0x01);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0xFE);
  }

    }

}
//---------------------------------------------------------------------------

void  Open_Close_Write(int buff)
 {
   ftStatus = FT_Open(0, &ftHandle);

if(ftHandle!=NULL)
   FT_SetBitMode(ftHandle,MASK,MODE);

     TxBuffer[0]=buff;
     val_ch=buff;
     ftStatus = FT_Write(ftHandle, TxBuffer, sizeof(TxBuffer), &BytesWritten);

  FT_Close(ftHandle);
 }
//---------------------------------------------------------------------------
void __fastcall TForm1::Ch2Click(TObject *Sender)
{
 if(devive_found)
 {
         ch2=!ch2;
  if(ch2==true)
  {
    Open_Close_Write(TxBuffer[0]|0x02);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0xFD);
  }

    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
      Open_Close_Write(0x00);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch3Click(TObject *Sender)
{
  if(devive_found)
   {
      ch3=!ch3;
  if(ch3==true)
  {
    Open_Close_Write(TxBuffer[0]|0x04);
  }
  else
  {
     Open_Close_Write(TxBuffer[0]&0xFB);
  }

    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch4Click(TObject *Sender)
{
   if(devive_found)
   {
      ch4=!ch4;
  if(ch4==true)
  {
    Open_Close_Write(TxBuffer[0]|0x08);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0xF7);
  }

    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch5Click(TObject *Sender)
{
   if(devive_found)
   {
      ch5=!ch5;
  if(ch5==true)
  {
    Open_Close_Write(TxBuffer[0]|0x10);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0xEF);
  }

    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch6Click(TObject *Sender)
{
  if(devive_found)
   {
      ch6=!ch6;
  if(ch6==true)
  {
    Open_Close_Write(TxBuffer[0]|0x20);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0xDF);
  }

    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch7Click(TObject *Sender)
{
   if(devive_found)
   {
      ch7=!ch7;
  if(ch7==true)
  {
    Open_Close_Write(TxBuffer[0]|0x40);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0xBF);
  }

    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ch8Click(TObject *Sender)
{
   if(devive_found)
   {
      ch8=!ch8;
  if(ch8==true)
  {
    Open_Close_Write(TxBuffer[0]|0x80);
  }
  else
  {
     Open_Close_Write(TxBuffer[0] &0x7E);
  }

    }
}
//---------------------------------------------------------------------------

Saluti

Primok_V

Aggiungi commento


Codice di sicurezza
Aggiorna

privacy



Messages