//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <Registry.hpp>
#include "Usetcom.h"
#include "Uchar.h"
#include "pendlg.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGRID"
#pragma resource "*.dfm"
Tfmain *fmain;
Word h,m,s,ms; //时间分解量
int timeperpage = dtFiveSeconds; //每屏示宽
HANDLE hCom ; //串口句柄
int Running = 0; //线程状态
bool Dec = false ,Hex = true; //接收模式
bool bRnew ; //接收最新标志
long Rcount,Tcount; //接收计数用
//---------------------------------------------------------------------------
__fastcall Tfmain::Tfmain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::TimerTimer(TObject *Sender)
{
TTime dt=Now();
DecodeTime( dt, h, m, s, ms);
stime->Caption = " " + AnsiString(h)+":"+ AnsiString(m)+":"+AnsiString(s)+":"+AnsiString(ms);
}
//==================================================================
void DataToMap(int data )
{
fmain->Series1->AddXY(Now(),data,AnsiString(m) + "'" + AnsiString(s) + "\"" + AnsiString(ms), clRed);
fmain->Chart->BottomAxis->Maximum = fmain->Series1->XValues->Last();
fmain->Chart->BottomAxis->Minimum = fmain->Chart->BottomAxis->Maximum - DateTimeStep[ timeperpage ]; // we want to see the last 100 points only
}
//---------------------------------------------------------------------------
// This is the event we need to arrange Axis scale as new points are added->
void AfterAdd(void)
{
/*/ In this example, we will set the Axis Minimum and Maximum values to
// show One Hour of data}ing at last point Time plus 5 minutes
Sender->GetHorizAxis->Maximum = Now() ;
//Sender->GetHorizAxis->Maximum = Sender->XValues->MaxValue + DateTimeStep[ dtOneSecond ];
Sender->GetHorizAxis->Minimum = Sender->GetHorizAxis->Maximum - DateTimeStep[ dtOneMinute ] ;*/
fmain->Chart->BottomAxis->Automatic = false;
fmain->Chart->BottomAxis->Maximum = fmain->Series1->XValues->Last();
fmain->Chart->BottomAxis->Minimum = fmain->Chart->BottomAxis->Maximum - DateTimeStep[ dtFiveSeconds ]; // we want to see the last 100 points only
}
//------------------------------------------------------------------------
void __fastcall Tfmain::CheckBox2Click(TObject *Sender)
{
Chart->View3D = CheckBox2->Checked;
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::FormCreate(TObject *Sender)
{
Memo1->Clear();
Memo2->Clear();
xp->ItemIndex = 1;
StaticText1->Caption = " " + Now().CurrentDate() + " ";
fmain->Chart->BottomAxis->Automatic = false;
TIniFile *iniFile;
iniFile = new TIniFile("SunView.ini");
String port,baud,data,stop,parity;
//读INI文件
port=iniFile->ReadString("COM", "COM", "COM1");
baud=iniFile->ReadString("COM","BaudRate",9600);
data=iniFile->ReadString("COM","DataBit",8);
stop=iniFile->ReadString("COM","StopBit",0);
parity=iniFile->ReadString("COM","Parity",0);
delete iniFile;
//赋值
String tmp1,tmp2;
tmp1 = "1";
if(stop=="1") tmp1 = "1.5";
if(stop=="2") tmp1 = "2";
tmp2 = "E";
if(parity=="0") tmp2 ="N";
if (parity=="1") tmp2 ="O";
comtext->Caption = " " + port + "," + baud + "," + tmp2+ "," + data + "," + tmp1 ;
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::seekClick(TObject *Sender)
{
//开辟串口线程
if(seek->Caption=="监视串口&W")
{ if (hCom != 0)
{
//开辟串口线程
mycomm = new ThreadComm;
Running = 1 ;
seek->Caption = "停止监视&P";
}
else
ShowMessage("请先打开串口!");
}
else
{
if (mycomm)
{
mycomm->Terminate();
Running = 0 ;
seek->Caption = "监视串口&W";
}
}
seek->Down = Running ;
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::ComboBox1Change(TObject *Sender)
{
tdata->Enabled = false;
tdata->Interval = ComboBox1->Text.ToInt();
tdata->Enabled = true;
//send->Down = tdata->Enabled ;
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::bautosendClick(TObject *Sender)
{
if(tdata->Enabled) tdata->Enabled = false ;
send->Caption = bautosend->Checked ? "自动发送&D" : "手动发送&D" ;
ComboBox1->Enabled = bautosend->Checked ;
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::sendClick(TObject *Sender)
{
if(send->Caption == "自动发送&D")
{
tdata->Enabled = true ;
send->Caption = "停止发送&D";
send->Down = true;
}
else if(send->Caption == "手动发送&D")
OutData();
else
{
send->Caption = "自动发送&D" ;
tdata->Enabled = false;
send->Down = false;
}
}
//---------------------------------------------------------------------------
void __fastcall Tfmain::OutData(void)
{
if(Memo2->Text.Length()<2)
{
if(tdata->Enabled) send->Click() ;
ShowMessage("无发送数据!");
return;
}
if(!hCom) comk->Click();
String str = (Hex||Dec) ? Memo2->Text.Trim()+" ":Memo2->Text;
int len=str.Length();
DWORD ByteCount=0;
int i=1,j=0;
int itmp = 0 ; //长度计数用
BYTE *pbyOut=new BYTE[512];//开辟512个字符空间
if(Hex)
{
str.UpperCase(); //变成大写
for( j=0,i=1;i<len;i+=3)
{
if( str[i] >= 'A' && str[i] <= 'F' || str[i] >= '0' && str[i] <= '9' ) //0-F
{
pbyOut[j] = (str[i]-'0') * 16 + str[i+1];
j++;
}
else
{
Memo2->SetFocus();
Memo2->SelStart = i-1;
Memo2->SelLength = 1;
if(tdata->Enabled) send->Click() ;
ShowMessage("发送有误(十六进制)!");
return;
}
}
WriteFile(hCom,pbyOut,j,&ByteCount,NULL); //mode
}
else if(Dec)
{
do
{
if( str[i] >= '0' && str[i] <= '9' ) //0-9
itmp ++;
else
{ //遇到非法字符则跳过,并且取子串后清除长度计数
try {
String st = str.SubString( i-itmp,itmp);
itmp = 0;
if( st.IsEmpty()) continue ;
pbyOut[j] = st.ToInt();//此句将限定值在0-256之间
j++ ;
}
catch(...){ShowMessage("发送数据有误!");return;}
}
}
while(i++ < len);
WriteFile(hCom,pbyOut,j,&ByteCount,NULL);
}
else
WriteFile(hCom,str.c_str(),len,&ByteCount,NULL);
Tcount += ByteCount ;
textT->Caption = "发送:" + AnsiString(Tcount);
delete pbyOut ;
}
//------------------------------------------------------------------------------
/*bool __fastcall Tfmain::CheckChar(char *ch)
{
if( *ch >= 'A' && *ch <= 'F' || *ch >= '0' && *ch <= '9' )
return true;
else
return false;
} */
//------------