在本附錄中收錄了用來作快速掃描以及 THz Imaging 的程式碼。
Unit1.h
//---
#ifndef Unit1H
#define Unit1H
//---
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "MULTIPLELib_OCX.h"
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
#include <OleCtrls.hpp>
#include <Dialogs.hpp>
//--- class TForm1 : public TForm
{
__published: // IDE-managed Components TBitBtn *BitBtn1;
TBitBtn *BitBtn2;
TImage *Image1;
TPci9812 *Pci98121;
TEdit *Edit1;
TEdit *Edit2;
TLabel *Label1;
TLabel *Label2;
TOpenDialog *OpenDialog1;
TPanel *Panel1;
TButton *Button1;
TEdit *Edit3;
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall Pci98121AiComplete(TObject *Sender,
TVariant *ScaledData, TVariant *BinaryCodes);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall BitBtn2Click(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private: // User declarations public:
int TimerCountDown; //剩下時間 bool ScanRunning; //正在掃描中??
TObject *Form1Sender; // User declarations __fastcall TForm1(TComponent* Owner);
void __fastcall averagedata(int Ndata, int * ch1, int * ch2, int blocksize,int NeededSize,float
*returndata,int averagecount);//平均 data
int FindMin(int * data, int Tsize, int Sindex, int blocksize);//尋找最小值之 index
};
//--- extern PACKAGE TForm1 *Form1;
//---
#endif
Unit1.cpp
//---
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include <stdio.h>
//---
#pragma package(smart_init)
#pragma link "MULTIPLELib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
int Total_Run = 0;
//--- __fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner) {
}
//---
void __fastcall TForm1::FormCreate(TObject *Sender) { Pci98121->AIRange = 0 ;
Pci98121->CardType = 4 ; Pci98121->channel = 1 ; Pci98121->ClockFreq = 0 ; Pci98121->ClockSource = 0 ; Pci98121->DoubleBufferMode = false ;
Pci98121->OpenMode = 0;
Pci98121->ReturnType = 0 ;
Pci98121->StreamToFile = true ; Pci98121->TriggerMode = 0 ; Pci98121->TriggerPolarity = 1 ; Pci98121->TriggerSource = 0 ;
Pci98121->DeviceName = "Damage" ;
Edit1->Text = "data";
Edit2->Text = "out.txt";
Edit3->Text = "32000";
Edit4->Text = "900";
Edit5->Text = "1900";
Edit6->Text = "9999";
Edit7->Text = "1900";
Edit8->Text = "190";
Edit9->Text = "aved2.txt";
Edit10->Text = "out\\aved.txt";
Edit11->Text = "1"; void __fastcall TForm1::Button3Click(TObject *Sender) { Pci98121->AIRange = 0 ;
Pci98121->CardType = 4 ; Pci98121->channel = 1 ; Pci98121->ClockFreq = 0 ; Pci98121->ClockSource = 0 ; Pci98121->DoubleBufferMode = false ; Pci98121->OpenMode = 0;
void __fastcall TForm1::BitBtn1Click(TObject *Sender) {
ScanRunning = true;
Pci98121->NumOfScan = StrToInt(Edit12->Text); //data 筆數 Pci98121->ScanRate = StrToInt(Edit13->Text); //掃描速度 Pci98121->StartContAI("out\\"+Edit1->Text);
Label15->Caption = "running...";
TimerCountDown = (int)(StrToInt(Edit12->Text)/StrToInt(Edit13->Text))+1;
Timer1->Enabled = true;
}
//--- void __fastcall TForm1::Pci98121AiComplete(TObject *Sender, TVariant *ScaledData, TVariant *BinaryCodes)
{ //量測完後的處理函式,自動被呼叫 Pci98121->StopContAI();
Timer1->Enabled = false;
// char *inFN=new char[256],outc1FN[256],outc2FN[256];
FILE *sp1 , *in, *sp2 , *out , *sp3;
int readin,n,readdata1,readdata2;
Label15->Caption = "finished...";
in = fopen(("out\\"+Edit1->Text).c_str(),"rb");
out = fopen(("out\\"+Edit2->Text).c_str(), "w");
fseek(in,60,SEEK_SET); //跳過檔頭資訊 while(!feof(in)) //將 binary data 轉為 ASCII { fread(&readin,2,1,in);
readdata1=(unsigned short)readin>>4;
if(readdata1>=2048) readdata1 -= 4096;
fread(&readin,2,1,in);
readdata2=(unsigned short)readin>>4;
if(readdata2>=2048) readdata2 -= 4096;
fprintf(out,"%d %d\n",readdata1,readdata2);
};
fclose(in);
fclose(out);
Button1Click(Sender);
/*
if(Total_Run < StrToInt(Edit11->Text) ) BitBtn1Click(Sender);
*/
ScanRunning = false;
};
//=====================================================
void __fastcall TForm1::averagedata(int Ndata, int * ch1, int * ch2, int blocksize,int NeededSize,float
*returndata, int averagecount)
{ //將所得數據平均用副程式,ch1 為 ODL 位置函數,ch2 為 THz 信號 int a,b,NumOfAve=0;
long RetMinIndex,MinIndexNow=0;
for(b=0;b<NeededSize;b++) *(returndata+b)=0;
for(a=0;a<Ndata;a+=blocksize) {
RetMinIndex=FindMin(ch1,Ndata,a,blocksize);
if( (*(ch1+RetMinIndex)+5<*(ch1+a)) && (*(ch1+RetMinIndex)+5<*(ch1+a+blocksize)) ) //此最小值比區間左右兩端值都小則成立
if(RetMinIndex+NeededSize < Ndata){
for(b=0;b<NeededSize;b++) //將 data 存入 buffer *(returndata+b)+=*(ch2+RetMinIndex+b);
NumOfAve++;
if(NumOfAve == averagecount) break;
};
};
if(NumOfAve != 0)
{ for(b=0;b<NeededSize;b++) *(returndata+b)/=NumOfAve;
Label7->Caption = NumOfAve;
} else
Label7->Caption = "No signal found";
}
int TForm1::FindMin(int * data, int Tsize, int Sindex, int blocksize) //尋找最小值所在 index
{ long a;
long IndexOfMin=Sindex,minV=*(data+Sindex);
for(a=0;Sindex+a<Tsize,a<blocksize;a++) { if(*(data+Sindex+a)<=minV)
{ minV=*(data+Sindex+a);
IndexOfMin=Sindex+a;
};
};
return IndexOfMin;
}
void __fastcall TForm1::Button1Click(TObject *Sender) { int a,b;
int *ch1 , *ch2 ; float *receive;
ch1 = new int[StrToInt(Edit3->Text)+5];
ch2 = new int[StrToInt(Edit3->Text)+5];
receive = new float[StrToInt(Edit5->Text)+5];
FILE *in,*out;
in = fopen(("out\\"+Edit2->Text).c_str() , "rb");
for(int n=0 ; n < StrToInt(Edit3->Text) ; n++) fscanf(in,"%d %d",&ch1[n],&ch2[n]);
fclose(in);
int ave_count = StrToInt(Edit6->Text) ;
averagedata(StrToInt(Edit3->Text),ch1,ch2,StrToInt(Edit4->Text),StrToInt(Edit5->Text),receive,ave_c ount);
for(a=0;a<Edit5->Text.ToInt();a++)//尋找 waveform 的最小值及最大值並記錄給作 Imaging 參考 { if(receive[a]>Form2->TDS_MAX)
Form2->TDS_MAX = receive[a];
if(receive[a]<Form2->TDS_MIN) Form2->TDS_MIN = receive[a];
}; // records MAX and MIN
out=fopen((Edit10->Text).c_str(),"w");
for(int n=0 ; n < StrToInt(Edit5->Text); n++)
fprintf(out,"%f %f \n",n*Edit14->Text.ToDouble(),receive[n]);
fclose(out);
if(CheckBox1->Checked == true){ //draw waveform Image1->Canvas->Brush->Color = 0x0000;
Image1->Canvas->FillRect(Rect(0,0,905,300));
Image1->Width = 905 +10 ;
Image1->Canvas->Pen->Color = clRed ; Image1->Canvas->Pen->Width = 2;
Image1->Canvas->MoveTo(9,125);
Image1->Canvas->LineTo(905,125);
Image1->Canvas->Pen->Width = 1;
Image1->Canvas->Pen->Color = clBlue ; Image1->Canvas->MoveTo(10,125);
Image1->Canvas->MoveTo(10,(int) 125 - 100*(receive[0]/2048));
for(int i = 1 ;i < StrToInt(Edit5->Text) ; i++)
Image1->Canvas->LineTo(10+i*905.0/Edit5->Text.ToDouble(), (int) 125 - 100*(receive[i]/2048));
}
delete ch1;
delete ch2;
delete receive;
}
//---
void __fastcall TForm1::Button2Click(TObject *Sender) {
FILE *in,*out;
float *Sdata , *Ddata;
Sdata = new float[StrToInt(Edit7->Text)+2];
Ddata = new float[StrToInt(Edit8->Text)+10];
float t;
for( int a=0 ; a <= StrToInt(Edit8->Text);a++) Ddata[a]=0.0;
if(OpenDialog1->Execute()){
in=fopen((char*)(OpenDialog1->FileName.c_str()),"r");
for(int a=0;a < StrToInt(Edit7->Text) ; a++) fscanf(in,"%f",&Sdata[a]);
fclose(in);
int Samp_rate;
Samp_rate = (int)( StrToInt(Edit7->Text) / StrToInt(Edit8->Text) );
for(int a=0 ; a<StrToInt(Edit8->Text) ; a++) { t=0;
for(int b=0 ; b<Samp_rate ; b++) t+=Sdata[a*Samp_rate+b];
Ddata[a]=t/Samp_rate;
};
out=fopen((Edit9->Text).c_str(),"w");
for(int a=0 ; a< StrToInt(Edit8->Text) ; a++) fprintf(out,"%f\n",Ddata[a]);
fclose(out);
};
}
//--- void __fastcall TForm1::BitBtn2Click(TObject *Sender)//停止掃描 { Pci98121->StopContAI();
Label15->Caption = "stopped...";
Timer1->Enabled = false;
Label17->Caption = 0;
}
//---
void __fastcall TForm1::Timer1Timer(TObject *Sender)//計算剩餘時間 { TimerCountDown--;
Label17->Caption = TimerCountDown;
}
//--- void __fastcall TForm1::Button4Click(TObject *Sender) { Form2->Visible = true;
}
//---
Unit2.h
//---
#ifndef Unit2H
#define Unit2H
//---
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
//--- class TForm2 : public TForm
{
__published: // IDE-managed Components TPanel *Panel1;
TLabel *Label5;
TPanel *Panel2;
TLabel *Label6;
TLabel *Label7;
TLabel *Label8;
TLabel *Label9;
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TEdit *Edit4;
TPanel *Panel3;
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall Timer2Timer(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations public:
};
//--- extern PACKAGE TForm2 *Form2;
//---
#endif
Unit2.cpp
//---
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include<D:\eo90\wu\RS232 專區\RS232Comm.h>
#include<stdio.h>
#include<stdlib.h>
//---
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
RS232Comm Com;
//--- __fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner) { Com.RS232Init("COM1");
IsTmProcRun = false;
}
//---
void __fastcall TForm2::BitBtn1Click(TObject *Sender)//THz Imaging 開始 { char temp[50],temp1[15],temp2[15];
AnsiString readtemp;
int a,b;
FILE *out;
Label15->Caption = "Running...";
Com.Send(",,@0SX0,,,,",10);
Sleep(500);
Com.Send(",,@0SY0,,,,",10);
Sleep(500);
Panel2->Enabled = false;
pstX=0;
pstY=0;
timerN=-1;
out=fopen((Edit5->Text+"Image.inf").c_str(),"w"); //儲存基本資訊供 Imaging 使用 fprintf(out,"%d %d %d %d %f %d\n",Edit1->Text.ToInt(),Edit2->Text.ToInt(),
Edit3->Text.ToInt(),Edit4->Text.ToInt(),
Form1->Edit14->Text.ToDouble(),Form1->Edit5->Text.ToInt());
fclose(out);
TDS_MAX = 0;
TDS_MIN = 0;
Form2ScanOver = false;
Timer1->Enabled = true;
Timer2->Enabled = true;
}
//---
TForm2::MoveTo(int X, int Y)//移動 2D 平移台 { AnsiString t;
t=",,@0PX" + t.CurrToStr((X-pstX)*Edit3->Text.ToInt());
t+= ",,";
Com.Send(t.c_str(),t.Length());
Sleep(500+ abs((X-pstX)*Edit3->Text.ToInt())*0.5);
t=",,@0PY" + t.CurrToStr((Y-pstY)*Edit4->Text.ToInt());
t+= ",,";
Com.Send(t.c_str(),t.Length());
Sleep(500+ abs((Y-pstY)*Edit3->Text.ToInt())*0.5);
pstX=X;
pstY=Y;
}
void __fastcall TForm2::Timer1Timer(TObject *Sender) //控制 waveform 之掃描 { if(IsTmProcRun)
return;
if(Form1->ScanRunning) return;
IsTmProcRun = true;
int a,b;
timerN++;
b= timerN % Edit2->Text.ToInt(); //計算 Y 軸座標 a= (timerN-b)/Edit1->Text.ToInt(); //計算 X 軸座標
MoveTo(a,b);
Label11->Caption = a;
Label12->Caption = b;
leftsecs =
(Edit1->Text.ToInt()*Edit2->Text.ToInt()-timerN)*(Form1->Edit12->Text.ToInt()/Form1->Edit13->Te xt.ToInt());
Edit7->Text = (timerN+1) % Edit2->Text.ToInt();
Edit6->Text = (timerN+1-Edit7->Text.ToInt())/Edit1->Text.ToInt();
Form1->Edit10->Text = Form2->Edit5->Text + a + " " + b + ".txt";
Form1->BitBtn1Click(Sender);
if(timerN >= (Edit1->Text.ToInt())*(Edit2->Text.ToInt())-1) { Form2ScanOver = true;
Timer1->Enabled = false;
};
IsTmProcRun = false;
}
//---
void __fastcall TForm2::Timer2Timer(TObject *Sender)//計算剩餘時間 { int h,m,s;
AnsiString t;
leftsecs--;
s=leftsecs%60;
m=(leftsecs-s)/60%60;
h=(leftsecs-s)/3600;
Label14->Caption = t.CurrToStr(h) + " hours " + t.CurrToStr(m) + " minutes " + t.CurrToStr(s) + " seconds ";
if(Form2ScanOver == true && Form1->ScanRunning == false) { Timer2->Enabled = false;
Label15->Caption = "finished...";
FILE *out =fopen((Edit5->Text+"Image.inf").c_str(),"a");
fprintf(out,"%f %f \ncompleted",TDS_MAX,TDS_MIN);
fclose(out);
};
}
//---
void __fastcall TForm2::Button1Click(TObject *Sender) //中斷功能 { Timer1->Enabled = false;
Timer2->Enabled = false;
Label15->Caption = "wait form1 to finish...";
}
//---
void __fastcall TForm2::Button2Click(TObject *Sender) //恢復掃描功能 {
timerN=Edit6->Text.ToInt()*Edit2->Text.ToInt()+Edit7->Text.ToInt()-1;
Timer1->Enabled = true;
Timer2->Enabled = true;
}
//---