⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 GPIB 控制Agilent 34401A
💻 CPP
字号:
//---------------------------------------------------------------------------
#include "windows.h"
#include "ni488.h"
#include "stdio.h"
#include "dir.h"
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "BorlandC_gpib-32"


#define SIZEOFARRAY           1024  // Size of read buffer

#define BDINDEX               0     // Board Index
#define PRIMARY_ADDR_OF_DMM   22     // Primary address of device
#define NO_SECONDARY_ADDR     0     // Secondary address of device
#define TIMEOUT               T10s  // Timeout value = 10 seconds
#define EOTMODE               1     // Enable the END message
#define EOSMODE               0     // Disable the EOS mode

// Response byte after serial polling device
#define SERIALPOLLRESPONSE    0x60


int  Dev;
char ResByte,
     ValueStr[SIZEOFARRAY + 1],
     MsgBuffer[80];
char ErrorMnemonic[21][5] = {"EDVR", "ECIC", "ENOL", "EADR", "EARG",
                             "ESAC", "EABO", "ENEB", "EDMA", "",
                             "EOIP", "ECAP", "EFSO", "", "EBUS",
                             "ESTB", "ESRQ", "", "", "", "ETAB"};
/*
 *  After each GPIB call, the application checks whether the call
 *  succeeded. If an NI-488.2 call fails, the GPIB driver sets the
 *  corresponding bit in the global status variable. If the call
 *  failed, this procedure prints an error message, takes the
 *  device offline and exits.
*/
void GPIBCleanup(char* ErrorMsg)
{
   if (Dev != -1)
   {
      sprintf(MsgBuffer, "%s\nibsta = 0x%x\niberr = %d (%s)\n", ErrorMsg,
           ibsta, iberr, ErrorMnemonic[iberr]);
      Application->MessageBox(MsgBuffer, "Error", MB_OK + MB_ICONSTOP);
      ibonl (Dev, 0);
   }
}

TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
   Edit1->Clear();
   Edit2->Clear();
   Edit3->Clear();
   Edit4->Clear();
   Edit5->Clear();
   Edit6->Clear();
   Edit7->Clear();  
   Memo1->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
  Close();

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

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    Edit1->Text=Now().FormatString("yy-mm-dd");
    Edit2->Text=Now().FormatString("hh:mm:ss");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit3KeyPress(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
   if(Key==0x0d)
   {
    if(Edit3->Text.Length()!=8)
    {
         Application->MessageBoxA("Error WO","Error",MB_OK|MB_ICONINFORMATION);
         Edit3->Clear();
         Edit3->SetFocus();
         return ;
    }
    Edit4->SetFocus();
   }
           
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit4KeyPress(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
   if(Key==0x0d)
   {
      if(Edit4->Text.Length()!=5)
      {
         Application->MessageBoxA("Error Oparator","Error",MB_OK|MB_ICONINFORMATION);
         Edit4->Clear();
         Edit4->SetFocus();
         return;
      }

      Label11->Caption = "Ready";
      Label11->Font->Color = clGreen;
      Edit5->SetFocus();
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit5KeyPress(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    if(Key==0x0d)
    {
       BitBtn2->Click();
    }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
   if(Edit5->Text.Length()!=11)
   {
         Application->MessageBoxA("Error Barcode","Error",MB_OK|MB_ICONINFORMATION);
         Edit5->Clear();
         Edit5->SetFocus();
         return ;
    }

     //Edit5->Clear();
     Edit6->Clear();
     Edit7->Clear();

     /* '*RST' resets the 34401A.                                    */
    ibwrt(Dev, "*RST\n", 5L);
    if (ibsta & ERR)
    {
       GPIBCleanup("Unable to reset 34401AP");
    }

    // set the 34401A to DCI mode
    AnsiString sendData ="MEAS:CURR:DC?\n";
    ibwrt(Dev,sendData.c_str(),sendData.Length());
    if(ibsta & ERR)
    {
        GPIBCleanup("unable to set DCI mode");
    }


   /* The '*CLS' command clears the multimeter status data structures. */
    ibwrt(Dev, "*CLS\n", 5L);
    if (ibsta & ERR)
    {
       GPIBCleanup("Unable to clear Event Registers");
    }

   /*
    * The '*OPC' command instructs the multimeter to assert the
    * Operation Complete bit in the Standard Event Status Register.
    * When the multimeter is ready to supply a reading, it is this
    * command that causes it to assert the GPIB SRQ line.
    */
    ibwrt(Dev, "*OPC\n", 5L);
    if (ibsta & ERR)
    {
       GPIBCleanup("Unable to set Operation Complete Bit");
    }



   /*
    * The application writes the 'VAL1?' command to the multimeter. The
    * ':read?' command causes the multimeter to generate a string that
    * contains the primary display value.
    */
    AnsiString sendData1 = "READ?\n" ;
    ibwrt(Dev, sendData1.c_str(), sendData1.Length());
    if (ibsta & ERR)
    {
       GPIBCleanup("Unable to request data from 34401A");
    }

   /*
    * The application reads the ASCII string from the multimeter into
    * the variable ValueStr.
    */
    ibrd(Dev, ValueStr, SIZEOFARRAY);
    if (ibsta & ERR)
    {
       GPIBCleanup("Unable to read from 34401A");
    }

   /*
    *  The string returned by ibrd is a binary string whose length is
    *  specified by the byte count in ibcntl. However, the multimeter
    *  sends measurements in the form of ASCII strings. Because of
    *  this, it is possible to add a NULL character to the end of the
    *  data received and display the ASCII multimeter response. The
    *  following code illustrates that.
    */
    ValueStr[15] = '\0';
    AnsiString str=(AnsiString)ValueStr ;
    float current = StrToFloat(str)*1000000;
    Edit6->Text = FloatToStrF(current,ffFixed,5,1);
    float power = current/0.56;
    Edit7->Text = FloatToStrF(power,ffFixed,5,1);
    if(power>302&&power<696)
    {
        Label11->Caption="PASS" ;
        Label11->Font->Color=clGreen;
        //Label11->Height =Label11->Height-10;
        Memo1->Lines->Add(Edit5->Text+"\r\n"+Edit6->Text +"   "+Edit7->Text);
        Memo1->Lines->Add("-------------");
        Edit5->Clear();
    }
    else
    {
        Label11->Caption="FAIL" ;
        Label11->Font->Color=clRed;
        //Label11->Height=Label11->Height-10;
    }

    Edit5->SetFocus();
    SetCurrentDir("D:\\Vcsel Power Test\\");
    AnsiString FileName =Edit3->Text+".xls";
    if(Edit3->Text == "")
     {
         MessageBox(Handle,"Please input Work order","information",MB_ICONINFORMATION|MB_OK);
         Edit3->SetFocus();
         return;
     }
     if(!FileExists(FileName))
     {

          int ihandle = FileCreate(FileName);
          AnsiString temp;
          temp = "Date" +AnsiString(char(0x09));
          temp += "Time" +AnsiString(char(0x09));
          temp += "Operator" +AnsiString(char(0x09));
          temp += "SFC SN" +AnsiString(char(0x09));
          temp += "Current" +AnsiString(char(0x09));
          temp += "Power"+AnsiString(char(0x09));
          temp += "Status\r\n" ;
          FileWrite(ihandle,temp.c_str(),temp.Length());
          FileClose(ihandle);

     }
     int myhandle = FileOpen(FileName,fmOpenWrite);
     FileSeek(myhandle,0,2);
     AnsiString temp;
     temp = Edit1->Text +AnsiString(char(0x09));
     temp += Edit2->Text +AnsiString(char(0x09));
     temp += Edit4->Text +AnsiString(char(0x09));
     temp += Edit5->Text +AnsiString(char(0x09));
     temp += Edit6->Text +AnsiString(char(0x09));
     temp += Edit7->Text +AnsiString(char(0x09));
     temp += Label11->Caption +"\r\n";
     FileWrite(myhandle,temp.c_str(),temp.Length());
     FileClose(myhandle);


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


void __fastcall TForm1::FormCreate(TObject *Sender)
{
    /*
    * The application brings the multimeter online using ibdev. A
    * device handle, Dev, is returned and is used in all subsequent
    * calls to the device.
    */
    Dev = ibdev(BDINDEX, PRIMARY_ADDR_OF_DMM, NO_SECONDARY_ADDR,
                TIMEOUT, EOTMODE, EOSMODE);
    if (ibsta & ERR)
    {
       sprintf(MsgBuffer, "Unable to open 2015p\nibsta = 0x%x\niberr = %d (%s)\n",
               ibsta, iberr, ErrorMnemonic[iberr]);
       Application->MessageBox(MsgBuffer, "Error", MB_OK + MB_ICONSTOP);
       //Form1->Close();
    }

    ibclr(Dev);
    if (ibsta & ERR)
    {
       GPIBCleanup("Unable to clear device");
    }
    if(!DirectoryExists("D:\\Vcsel Power Test\\" ))
    {

        CreateDir("D:\\Vcsel Power Test\\");
     }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Delay(DWORD DT)
{
    long tt;
    tt =GetTickCount();
    while((GetTickCount()-tt)<DT)
    {
         if((GetTickCount()-tt)<=0)
          tt =GetTickCount();
    }
}
//----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -