?? unit1.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "math.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int rec_count;//記錄數
int p;//當前記錄指針
// 發送的數據結構
struct READSTRU
{
BYTE head; // 起 始 位
unsigned short sn; // 板地址, 序列號
short func; //功能位
BYTE m[26];
unsigned short xy;//校驗和
BYTE end;
}readst;
//發送命令
void sendcmd(AnsiString sn,AnsiString fc,READSTRU readst,TNMUDP *udp);
//功能:輸入一雙字節數據,返回年月日
AnsiString DateChang(unsigned short x)
{
char yy,mm,dd;
unsigned short temp;
//將輸入的高低字節對調
temp=(x&0x00ff)<<8 ;
x=temp+ (x>>8);
//將數向右移9位即為年
yy=x>>9;
//屏蔽高7位,并向右移5位即為月
mm=(x&0x01ff)>>5;
//屏蔽高11位,即為日
dd=x&0x001f;
return (IntToStr(yy+2000)+"年"+IntToStr(mm)+"月"+IntToStr(dd)+"日");
}
//功能:輸入一雙字節數據,返回時分秒
AnsiString TimeChang(unsigned short x)
{
char hh,mm,ss;
unsigned short temp;
//將輸入的高低字節對調
temp=(x&0x00ff)<<8 ;
x=temp+ (x>>8);
//將數向右移11位即為時
hh=x>>11;
//屏蔽高5位,并向右移5位即為分
mm=(x&0x07ff)>>5;
//屏蔽高11位,乘以2(每兩秒采一次)即為秒
ss=x&0x001f;
return (IntToStr(hh)+"時"+IntToStr(mm)+"分"+IntToStr(ss*2)+"秒");
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//----------------
int HexToS(AnsiString c) //十六進制轉為十進制
{
int x=0;
char *s;
for(int i=1;i<=c.Length() ;i++)
{
s=c.SubString(i,1).LowerCase().c_str() ;
switch(s[0])
{
case '0':s="0";break;
case '1':s="1";break;
case '2':s="2";break;
case '3':s="3";break;
case '4':s="4";break;
case '5':s="5";break;
case '6':s="6";break;
case '7':s="7";break;
case '8':s="8";break;
case '9':s="9";break;
case 'a':s="10";break;
case 'b':s="11";break;
case 'c':s="12";break;
case 'd':s="13";break;
case 'e':s="14";break;
case 'f':s="15";break;
default: s="0";break;
}
x=x+StrToInt(s)*pow(16,c.Length() -i);
}
return x ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString s,t;
unsigned char c[40];
s=Trim(Edit1->Text);
if(s.Length()!=68)return;
for(int i=0;i<s.Length()/2;i++)
{
t=s.SubString(i*2+1,2);
c[i]=HexToS(t);
}
NMUDP1->SendBuffer(c,34,34);
}
void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
BYTE buff[1024];
int len=NumberBytes;
AnsiString s,cell[12];
NMUDP1->ReadBuffer(buff,len,len);
Memo1->Clear();
static cell_p=0;//表格指針
for(int i=0;i<len;i++)
{
s=IntToStr(i);
s=s+"=";
s=s+IntToHex(buff[i],2);
Memo1->Lines->Add(s);
}
if(buff[0]!=0x7e)return;
if((buff[4]*256+buff[3])==4225)//狀態讀數返回
{
//Memo2->Lines->Add("序列號:"+IntToStr(buff[2]*256+buff[1]));
cell[0]= IntToStr(buff[2]*256+buff[1]);
// Memo2->Lines->Add("功能號:"+IntToStr(buff[4]*256+buff[3]));
cell[1]=IntToStr(buff[4]*256+buff[3]);
//Memo2->Lines->Add(IntToStr(2000+buff[5])+" 年"+IntToHex(buff[6],2)+" 月"+IntToHex(buff[7],2)+" 日 星期"+IntToStr(buff[8]));
cell[2]=IntToStr(2000+buff[5])+" 年"+IntToHex(buff[6],2)+" 月"+IntToHex(buff[7],2)+" 日 星期"+IntToStr(buff[8]);
// Memo2->Lines->Add(IntToHex(buff[6],2)+" 月");
// Memo2->Lines->Add(IntToHex(buff[7],2)+" 日");
//Memo2->Lines->Add("星期"+IntToStr(buff[8]));
//Memo2->Lines->Add(IntToHex(buff[9],2)+" 時"+IntToHex(buff[10],2)+" 分"+IntToHex(buff[11],2)+" 秒");
cell[3]=IntToHex(buff[9],2)+" 時"+IntToHex(buff[10],2)+" 分"+IntToHex(buff[11],2)+" 秒";
// Memo2->Lines->Add(IntToHex(buff[10],2)+" 分");
// Memo2->Lines->Add(IntToHex(buff[11],2)+" 秒");
rec_count=buff[12]+buff[13]*16+buff[14]*256;
//Memo2->Lines->Add("記錄數:"+IntToStr(rec_count));
cell[4]=IntToStr(rec_count);
//Memo2->Lines->Add("權限數:"+IntToStr(buff[16]*16+buff[15]));
cell[5]=IntToStr(buff[16]*16+buff[15]);
//Memo2->Lines->Add("卡號:"+IntToStr(buff[18]*256+buff[17]));
cell[6]=IntToStr(buff[18]*256+buff[17]);
//Memo2->Lines->Add("區號:"+IntToStr(buff[19]));
cell[7]=IntToStr(buff[19]);
//Memo2->Lines->Add("讀卡器狀態(0成功,1失?。?quot;+IntToStr((buff[20]&0x80)>>7));
cell[8]=IntToStr((buff[20]&0x80)>>7);
//Memo2->Lines->Add("刷卡日期:"+DateChang(buff[21]*256+buff[22]));
cell[9]=DateChang(buff[21]*256+buff[22]);
// Memo2->Lines->Add("刷卡時間:"+TimeChang(buff[23]*256+buff[24]));
cell[10]=TimeChang(buff[23]*256+buff[24]);
//Memo2->Lines->Add("------------------------------");
//--------------------
if(StrToInt(cell[6])!=65535)
{
for(int i=0;i<12;i++)
StringGrid1->Cells[i+1][cell_p+1]=cell[i];
cell_p++;
StringGrid1->RowCount++;
}
}
if((buff[4]*256+buff[3])==4245)//狀態權限返回
{
//5 6 ID 號
Memo2->Lines->Add("ID:"+IntToStr(buff[5]+buff[6]*256));
//7 商家號
Memo2->Lines->Add("商家號:" +IntToStr(buff[7]));
//8 門號
Memo2->Lines->Add("門號:" +IntToStr(buff[8]));
//9 10起始年月日
Memo2->Lines->Add("起始年月日:" +DateChang(buff[9]*256+buff[10]));
//1112終止年月日
Memo2->Lines->Add("終止年月日:" +DateChang(buff[11]*256+buff[12]));
//13時段
Memo2->Lines->Add("時段:" +IntToStr(buff[13]));
//141516密碼
Memo2->Lines->Add("密碼:" +IntToStr(buff[14]+buff[15]*256+buff[16]*65536));
}
if((buff[4]*256+buff[3])==4353)//搜索TCPIP設備返回
{
s="MAC地址:"+buff[5];//5678910 Mac地址
s=s+"."+IntToHex(buff[6],2);
s=s+"."+IntToHex(buff[7],2);
s=s+"."+IntToHex(buff[8],2);
s=s+"."+IntToHex(buff[9],2);
s=s+"."+IntToHex(buff[10],2);
Memo2->Lines->Add(s);
//11 12 1314 IP地址
s="IP地址:"+IntToStr(buff[11]);
s=s+"."+IntToStr(buff[12]);
s=s+"."+IntToStr( buff[13]);
s=s+"."+IntToStr(buff[14]);
Memo2->Lines->Add(s);
//15 16 17 18掩碼
s="掩碼:"+IntToStr(buff[15]);
s=s+"."+IntToStr(buff[16]);
s=s+"."+IntToStr( buff[17]);
s=s+"."+IntToStr(buff[18]);
Memo2->Lines->Add(s);
//19-22 默認網關
s="默認網關:"+IntToStr(buff[19]);
s=s+"."+IntToStr(buff[20]);
s=s+"."+IntToStr( buff[21]);
s=s+"."+IntToStr(buff[22]);
Memo2->Lines->Add(s);
//23 24 TCP端口
s="TCP端口:"+IntToStr(buff[24]*256+buff[23]);
Memo2->Lines->Add(s);
}
if((buff[4]*256+buff[3])==4235) //設置時間返回
{
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
AnsiString s,t;
unsigned char c[40];
s="7ED69081100000000000000000000000000000000000000000000000000000F7010D";
if(s.Length()!=68)return;
for(int i=0;i<s.Length()/2;i++)
{
t=s.SubString(i*2+1,2);
c[i]=HexToS(t);
}
NMUDP1->SendBuffer(c,34,34);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString s,t;
unsigned char c[40];
s="7ED6909D10010000000000000000000000000000000000000000000000000014020D";
if(s.Length()!=68)return;
for(int i=0;i<s.Length()/2;i++)
{
t=s.SubString(i*2+1,2);
c[i]=HexToS(t);
}
NMUDP1->SendBuffer(c,34,34);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
AnsiString s,t;
unsigned char c[40];
s="7ED6900111000000000000000000000000000000000000000000000000000078010D";
if(s.Length()!=68)return;
for(int i=0;i<s.Length()/2;i++)
{
t=s.SubString(i*2+1,2);
c[i]=HexToS(t);
}
NMUDP1->SendBuffer(c,34,34);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
memset(&readst,0,sizeof(readst));
AnsiString s,sn,fc;//sn-序列號,fc-功能碼
sn=Edit2->Text;
fc=ComboBox1->Text;
//--起始
s=IntToHex(readst.head,2);
//--序列號
if(sn=="")return;
readst.sn=StrToInt(sn);
s=s+IntToHex(readst.sn&0x00ff,2);
s=s+IntToHex((readst.sn>>8)&0x00ff,2);
//--功能碼
readst.func=StrToInt(fc);
s=s+IntToHex(readst.func&0x00ff,2);
s=s+IntToHex((readst.func>>8)&0x00ff,2);
//--其他位
for(int t=0;t<26;t++)
s=s+IntToHex(readst.m[t],2);
//---校驗和
BYTE buff[34];
memcpy(buff,&readst,sizeof(buff));
for(int i=1;i<31;i++)
readst.xy=readst.xy+buff[i];
s=s+IntToHex(readst.xy&0x00ff,2);
s=s+IntToHex((readst.xy>>8)&0x00ff,2);
//--結束符--
s=s+IntToHex(readst.end,2);
ShowMessage(s);
unsigned char c[70];
for(int i=0;i<s.Length()/2;i++)
{
s=s.SubString(i*2+1,2);
c[i]=HexToS(s);
}
NMUDP1->SendBuffer(c,34,34);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
readst.m[0]=0x01;//門鎖一
sendcmd(Edit2->Text,"4253",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void sendcmd(AnsiString sn,AnsiString fc,READSTRU readst,TNMUDP *udp)
{
//sn-序列號,fc-功能碼
AnsiString s,t;
readst.head=0x7e;
readst.end=0x0d;
//--起始
s=IntToHex(readst.head,2);
//--序列號
if(sn=="")return;
readst.sn=StrToInt(sn);
s=s+IntToHex(readst.sn&0x00ff,2);
s=s+IntToHex((readst.sn>>8)&0x00ff,2);
//--功能碼
readst.func=StrToInt(fc);
s=s+IntToHex(readst.func&0x00ff,2);
s=s+IntToHex((readst.func>>8)&0x00ff,2);
//--其他位
for(int t=0;t<26;t++)
s=s+IntToHex(readst.m[t],2);
//---校驗和
BYTE buff[34];
memcpy(buff,&readst,sizeof(buff));
for(int i=1;i<31;i++)
readst.xy=readst.xy+buff[i];
s=s+IntToHex(readst.xy&0x00ff,2);
s=s+IntToHex((readst.xy>>8)&0x00ff,2);
//--結束符--
s=s+IntToHex(readst.end,2);
// ShowMessage(s);
unsigned char c[40];
for(int i=0;i<s.Length()/2;i++)
{
t=s.SubString(i*2+1,2);
c[i]=HexToS(t);
}
Form1->NMUDP1->SendBuffer(c,34,34);
}
void __fastcall TForm1::Button7Click(TObject *Sender)
{
readst.m[0]=0x02;//門鎖二
sendcmd(Edit2->Text,"4253",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
readst.m[0]=0;
sendcmd(Edit2->Text,"4225",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{
sendcmd(Edit2->Text,"4353",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
//設置時間:2004年10月13日,星期一,10:00:01
readst.m[0]=8;
readst.m[1]=0x10;
readst.m[2]=0x13;
readst.m[3]=1;
readst.m[4]=0x10;
readst.m[5]=0;
readst.m[6]=1;
sendcmd(Edit2->Text,"4235",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button11Click(TObject *Sender)
{
readst.m[0]=3;//權限索引號
readst.m[1]=0;
sendcmd(Edit2->Text,"4245",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button12Click(TObject *Sender)
{
readst.m[0]=0;
readst.m[1]=0;
//卡號
readst.m[2]=0x3d;
readst.m[3]=0x7b;
//區號
readst.m[4]=1;
//門號
readst.m[5]=1;
//年月日起始
readst.m[6]=0x21;
readst.m[7]=0;
//年月日終止
readst.m[8]=0x9f;
readst.m[9]=0x29;
//控制時段索引號
readst.m[10]=1;
//密碼
readst.m[11]=0x40;
readst.m[12]=0xe2;
readst.m[13]=0x01;
sendcmd(Edit2->Text,"4359",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button13Click(TObject *Sender)
{
memset(&readst,0,sizeof(readst));
readst.m[0]=1; //1號門
readst.m[1]=0; //0-不啟用鍵盤密碼,1-啟用鍵盤密碼
sendcmd(Edit2->Text,"4241",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button14Click(TObject *Sender)
{
readst.m[0]=rec_count+1;//-p;
sendcmd(Edit2->Text,"4225",readst,NMUDP1);
// p++;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button16Click(TObject *Sender)
{
Memo1->Clear();
Memo2->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
readst.m[0]=rec_count+1;
sendcmd(Edit2->Text,"4225",readst,NMUDP1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StringGrid1->Cells[1][0]="序列號";
StringGrid1->Cells[2][0]="指令功能碼";
StringGrid1->Cells[3][0]="采集日期";
StringGrid1->Cells[4][0]="采集時間";
StringGrid1->Cells[5][0]="記錄數";
StringGrid1->Cells[6][0]="權限數";
StringGrid1->Cells[7][0]="卡號";
StringGrid1->Cells[8][0]="區號";
StringGrid1->Cells[9][0]="讀卡器狀態";
StringGrid1->Cells[10][0]="刷卡日期";
StringGrid1->Cells[11][0]=" 刷卡時間";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
/*TRect tRect;
if(ACol== 1)
{
StringGrid1->Canvas->Brush->Color= clRed;
StringGrid1->Canvas->Font->Color=clBlue;
StringGrid1->Canvas->FillRect(tRect);
StringGrid1->Canvas->TextOut(tRect.Left,tRect.Top,StringGrid1->Cells[1][2]);
} */
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -