D
dsqyytmf
Unregistered / Unconfirmed
GUEST, unregistred user!
要不如何把下面C++Builder的代码转换成DELPHI的
//
//
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Manager.h"
#include "RDWForm.h"
#include "CallForm.h"
#include "Protocol.h"
#include "rece.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma resource "*.dfm"
TMainForm *MainForm;
void * DiHandle;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
// Timer1->Enabled=false;//true;
StatusLabel->Caption="状态: 正在连接网络介面器";
Edit1->Text="";
ReceText->Text="";
ComboBox1->ItemIndex=0;
B_link=true;
CommTestCnt=CONST_500MS;
SetQueuePt(0,0,ReceiverQueue);
if(MSComm1->PortOpen==false)
MSComm1->PortOpen=true;
Status=IDEL;
ControlData=CLOSEALL;
Unlock->Enabled=false;
}
//---------------------------------------------------------------------------
unsigned char crc8(unsigned char *ptr, int len)
{
unsigned char crc;
//0 1 2 3 4 5 6 7 8 9 a b c d e f
const unsigned char crc_ta[256]={
0x00,0x07,0x0e,0x09,0x1c,0x1b,0x12,0x15,0x38,0x3f,0x36,0x31,0x24,0x23,0x2a,0x2d, //0
0x70,0x77,0x7e,0x79,0x6c,0x6b,0x62,0x65,0x48,0x4f,0x46,0x41,0x54,0x53,0x5a,0x5d, //1
0xe0,0xe7,0xee,0xe9,0xfc,0xfb,0xf2,0xf5,0xd8,0xdf,0xd6,0xd1,0xc4,0xc3,0xca,0xcd, //2
0x90,0x97,0x9e,0x99,0x8c,0x8b,0x82,0x85,0xa8,0xaf,0xa6,0xa1,0xb4,0xb3,0xba,0xbd, //3
0xc7,0xc0,0xc9,0xce,0xdb,0xdc,0xd5,0xd2,0xff,0xf8,0xf1,0xf6,0xe3,0xe4,0xed,0xea, //4
0xb7,0xb0,0xb9,0xbe,0xab,0xac,0xa5,0xa2,0x8f,0x88,0x81,0x86,0x93,0x94,0x9d,0x9a, //5
0x27,0x20,0x29,0x2e,0x3b,0x3c,0x35,0x32,0x1f,0x18,0x11,0x16,0x03,0x04,0x0d,0x0a, //6
0x57,0x50,0x59,0x5e,0x4b,0x4c,0x45,0x42,0x6f,0x68,0x61,0x66,0x73,0x74,0x7d,0x7a, //7
0x89,0x8e,0x87,0x80,0x95,0x92,0x9b,0x9c,0xb1,0xb6,0xbf,0xb8,0xad,0xaa,0xa3,0xa4, //8
0xf9,0xfe,0xf7,0xf0,0xe5,0xe2,0xeb,0xec,0xc1,0xc6,0xcf,0xc8,0xdd,0xda,0xd3,0xd4, //9
0x69,0x6e,0x67,0x60,0x75,0x72,0x7b,0x7c,0x51,0x56,0x5f,0x58,0x4d,0x4a,0x43,0x44, //a
0x19,0x1e,0x17,0x10,0x05,0x02,0x0b,0x0c,0x21,0x26,0x2f,0x28,0x3d,0x3a,0x33,0x34, //b
0x4e,0x49,0x40,0x47,0x52,0x55,0x5c,0x5b,0x76,0x71,0x78,0x7f,0x6a,0x6d,0x64,0x63, //c
0x3e,0x39,0x30,0x37,0x22,0x25,0x2c,0x2b,0x06,0x01,0x08,0x0f,0x1a,0x1d,0x14,0x13, //d
0xae,0xa9,0xa0,0xa7,0xb2,0xb5,0xbc,0xbb,0x96,0x91,0x98,0x9f,0x8a,0x8d,0x84,0x83, //e
0xde,0xd9,0xd0,0xd7,0xc2,0xc5,0xcc,0xcb,0xe6,0xe1,0xe8,0xef,0xfa,0xfd,0xf4,0xf3};//f
//0 1 2 3 4 5 6 7 8 9 a b c d e f
crc=0;
while(len!=0)
{
crc=crc_ta[crc^*ptr];
ptr++;
len--;
}
return(crc);
}
//========================================================================
/*
//功能: 队列进队1个字节,同时调整头尾指针。
//
//参数:队列首地址指针QueueHeadPt,源数据指针Source,最大队列容量(字节个数)
// QueueCapacity。
//返回:无。
//说明:* QueueHeadPt=队列头指针,*(QueueHeadPt+1)=队列尾指针,如头指针=尾
// 指针=0时,队列空。
*/
void TMainForm::QueueInto(unsigned char *QueueHeadPt, /
unsigned char *source, unsigned char QueueCapacity)
{
unsigned char *Last;
if(*QueueHeadPt >
QueueCapacity)
*QueueHeadPt = 0;
if(*(QueueHeadPt+1) >
QueueCapacity)
*(QueueHeadPt+1) = 0;
if(*(QueueHeadPt+1) == 0)
{
*QueueHeadPt = 0;
}
if(*QueueHeadPt == 0)
{
if(++(*(QueueHeadPt+1)) >
QueueCapacity)
{//队列从空到满。
*QueueHeadPt = 1;
*(QueueHeadPt+1)=1;
}
}
else
if(*QueueHeadPt == *(QueueHeadPt+1))
{ //队列满
if(++(*(QueueHeadPt+1)) >
QueueCapacity)
*(QueueHeadPt+1)=1;
*QueueHeadPt=*(QueueHeadPt+1);
}
else
if(++(*(QueueHeadPt+1))>QueueCapacity)
{
*(QueueHeadPt+1)=1;
}
Last=QueueHeadPt+(*(QueueHeadPt+1))+1;
* Last=* source;
}
//========================================================================
/*
//功能:队列出队1个字节,从队列复制字节到指定的内存,同时调整头尾指针。
//
//参数:队列首地址指针QueueHeadPt,目的数据指针target,最大队列容量(字节
// 个数)QueueCapacity。
//
//返回:如成功则返回true,失败则返回false.
*/
bool TMainForm::QueueOut(unsigned char *QueueHeadPt, /
unsigned char * target, unsigned char QueueCapacity)
{
unsigned char *Head;
if(* QueueHeadPt>QueueCapacity)
* QueueHeadPt=0;
if(* (QueueHeadPt+1)>QueueCapacity)
* (QueueHeadPt+1)=0;
if(* (QueueHeadPt+1)==0)
{
* QueueHeadPt=0;
return false;
}
if(++(* QueueHeadPt)>QueueCapacity)
* QueueHeadPt=1;
Head=QueueHeadPt+(* QueueHeadPt)+1;
if(*QueueHeadPt == *(QueueHeadPt+1))
{
*QueueHeadPt = 0;
*(QueueHeadPt+1) = 0;
}
*target = *Head;
return true;
}
//========================================================================
//功能:查询一个队列是否空。
//参数:队列(一维数组)首地址指针pointer。
//返回:如空则返回true,不空则返回false.
bool TMainForm::Ask_QueueNull(unsigned char *QueueHeadPt)
{
if(((*QueueHeadPt) | (*(QueueHeadPt+1))) == 0)
return true;
return false;
}
//========================================================================
//功能:初始化一个队列的头尾指针。
//参数:队列头指针QueueHead,指针值QueueEnd,首地址指针QueueHeadPt。
//返回:无。
void TMainForm::SetQueuePt(unsigned char QueueHead, /
unsigned char QueueEnd, unsigned char * QueueHeadPt)
{
*QueueHeadPt++ = QueueHead;
*QueueHeadPt = Queueend;
}
//---------------------------------------------------------------------------
//功能: 把一个队列的头尾指针分别存到HeadPt和EndPt
void TMainForm::GetQueuePt(unsigned char &HeadPt, unsigned char &
endPt, /
unsigned char *QueueHeadPt)
{
HeadPt=* QueueHeadPt++;
EndPt=* QueueHeadPt;
}
//---------------------------------------------------------------------------
//函数功能: 从ReceiverQueue队列提取出一帧完整的信息,信息存在参数Buf所指向
// 的内存里.
//返回: 如提取出一帧完整的信息,返回true,否则返回false.
bool TMainForm::GetWholeFrame(unsigned char *Buf,unsigned char *ReceiverQueue)
{
unsigned char HeadPt, EndPt, temp;
int i;
for(;
{
//检查接收队列里是否有同步头存在,并去掉同步头之前的数据.
while(1)
{
GetQueuePt(HeadPt, EndPt, ReceiverQueue);
if(QueueOut(ReceiverQueue, &temp, RECEQUEUELEN)==false)
{
return false;
}
if(temp==0xcc)
{
break;
}
}
//检查是否收到完整一帧数据
int ByteCount;
if(QueueOut(ReceiverQueue, &temp, RECEQUEUELEN)==false)
{
SetQueuePt(HeadPt, EndPt, ReceiverQueue);
return false;
}
ByteCount=temp;
if((ByteCount<MINFRAMELEN)||(ByteCount>MAXFRAMELEN))
continue;
Buf[0]=0xcc;
Buf[1]=temp;
for(i=2;
i<ByteCount;
i++)
{
if(QueueOut(ReceiverQueue, &temp, RECEQUEUELEN)==false)
{
SetQueuePt(HeadPt, EndPt, ReceiverQueue);
return false;
}
Buf=temp;
}
i--;
temp=crc8(Buf, ByteCount-1);
if(Buf==temp)
return true;
//到此已收到完整一帧信息.
}
}
//---------------------------------------------------------------------------
//将收到的门口机密码转换成数字显示的字符,如是正常的密码则按十进制显示,
//否则按十六进制显示.
String TMainForm::ConvertPassWordDisData(const unsigned char *Buf,String
ansichar, String obj)
{
String pwchar;
String tempstr;
unsigned char temp;
if(((Buf[4])>99)||(Buf[5]>99))
{
pwchar="读到异常的"+obj+ansichar+"密码: "+"0x"+IntToHex(Buf[4],2) /
+"
"+"0x"+IntToHex(Buf[5],2);
}
else
{
temp=Buf[4]/10;
tempstr=IntToStr(temp);
temp=Buf[4]%10;
tempstr=tempstr+IntToStr(temp);
temp=Buf[5]/10;
tempstr=tempstr+IntToStr(temp);
temp=Buf[5]%10;
tempstr=tempstr+IntToStr(temp);
pwchar="读到"+obj+ansichar+"密码: "+tempstr;
}
return pwchar;
}
//---------------------------------------------------------------------------
void TMainForm::Transmit(const unsigned char *Buf, int ByteNum)
{
Variant OutBuf(OPENARRAY(int,(0,0)),varByte);
if(MSComm1->PortOpen==false)
MSComm1->PortOpen=true;
for(int i=0;
i<ByteNum;
i++)
{
OutBuf.PutElement(Buf, 0);
MSComm1->Output=OutBuf;
}
}
//---------------------------------------------------------------------------
//显示发送的数据.
void TMainForm:isSendData(const unsigned char *Buf, int ByteNum)
{
String str;
str="";
for(int i=0;
i<ByteNum;
i++)
{
str=str+IntToHex(Buf,2)+"
";
}
Edit1->Text=str;
}
//---------------------------------------------------------------------------
//在标签Label中显示呼叫动作
void TMainForm:isSendAction(const unsigned char *Tbuf)
{
switch(Tbuf[2])
{
case MANAGER_CALLROOM:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[4])+"栋"
/
+IntToStr(Tbuf[5])+"层"+IntToStr(Tbuf[6])+"号房"+"室内机";
break;
case MANAGER_CALLDOOR:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[4])+"栋"+ /
IntToStr(Tbuf[5]&0x0f)+ "号门口机";
break;
case MANAGER_CALLSMALLDOOR:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[4]&0x0f)+ /
"号小门口机";
break;
case CALL_OTHERMANAGER:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[3]&0x0f)+"号管理机";
break;
default:
break;
}
}
//---------------------------------------------------------------------------
//在标签Label中显示通话动作
void TMainForm:isTalkLabel(const unsigned char *Tbuf)
{
switch(Tbuf[2])
{
case MANAGER_CALLROOM:
StatusLabel->Caption="正和"+IntToStr(Tbuf[4])+"栋"
/
+IntToStr(Tbuf[5])+"层"+IntToStr(Tbuf[6])+"号房室内机通话中";
break;
case MANAGER_CALLDOOR:
StatusLabel->Caption="正和"+IntToStr(Tbuf[4])+"栋"+ /
IntToStr(Tbuf[5]&0x0f)+ "号门口机通话中";
break;
case MANAGER_CALLSMALLDOOR:
StatusLabel->Caption="正和"+IntToStr(Tbuf[4]&0x0f)+ /
"号小门口机通话中";
break;
case CALL_OTHERMANAGER:
StatusLabel->Caption="正和"+IntToStr(Tbuf[3]&0x0f)+"号管理机通话中";
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void TMainForm:isReceData(const unsigned char *Buf, int ByteNum)
{
String str;
str="";
for(int i=0;
i<ByteNum;
i++)
{
str=str+IntToHex(Buf,2)+"
";
}
ReceText->Lines->Add(str);
}
//---------------------------------------------------------------------------
//功能:管理机收到呼叫,根据收到呼叫数据整合回传的数据,若数据整合成功返回true,
//否则返回false,整合后的数据在Buf所指向的内存里.
bool TMainForm::SpellAckData(const unsigned char *Buf, unsigned char *Stb)
{
unsigned char MyAddress;
bool flag;
flag=false;
MyAddress=ComboBox1->ItemIndex;
if(++MyAddress>9)
return flag;
MyAddress+=0xe0;
switch(Buf[2])
{
case ROOM_CALLMANAGER:
Stb[0]=0xcc;
Stb[1]=0x08;
Stb[2]=MANAGER_CALLROOM;
Stb[3]=MyAddress;
Stb[4]=Buf[3];
Stb[5]=Buf[4];
Stb[6]=Buf[5];
flag=true;
break;
casedo
OR_CALLMANAGER:
Stb[0]=0xcc;
Stb[1]=0x07;
Stb[2]=MANAGER_CALLDOOR;
Stb[3]=MyAddress;
Stb[4]=Buf[3];
Stb[5]=Buf[4];
flag=true;
break;
case SMALLDOOR_CALLMANAGER:
Stb[0]=0xcc;
Stb[1]= 0x06;
Stb[2]=MANAGER_CALLSMALLDOOR;
Stb[3]= MyAddress;
Stb[4]= Buf[3];
flag=true;
break;
case CALL_OTHERMANAGER:
Stb[0]=0xcc;
Stb[1]= 0x06;
Stb[2]=CALL_OTHERMANAGER;
Stb[3]=Buf[4];
Stb[4]=MyAddress;
flag=true;
break;
default:
break;
}
if(flag==true)
{
Stb[Stb[1]-1]=crc8(Stb, Stb[1]-1);
}
return flag;
}
//---------------------------------------------------------------------------
//功能:拼装管理机清除命令数据
void TMainForm::SpellManClrFrame(unsigned char *Buf)
{
Buf[0]=0xcc;
Buf[1]=0x05;
Buf[2]=MANAGERCLEAR;
Buf[3]=ComboBox1->ItemIndex+0xe1;
Buf[4]=crc8(Buf, 4);
}
//---------------------------------------------------------------------------
//发送清除
void TMainForm::ClearAndIdel(void)
{
SpellManClrFrame(Tbuf);
Transmit(Tbuf,Tbuf[1]);
//发送数据.
DisSendData(Tbuf,Tbuf[1]);
//显示发送的数据.
CallCnt=0;
Cbuf[1]=0;
Tbuf[2]=0;
Status=IDEL;
StatusLabel->Caption="待机";
}
//---------------------------------------------------------------------------
//发送忙
void TMainForm::SendBusy(void)
{
unsigned char buf[5];
buf[0]=0xcc;
buf[1]=0x05;
buf[2]=SYS_USING;
buf[3]=ComboBox1->ItemIndex+0xe1;
buf[4]=crc8(buf, 4);
Transmit(buf, 5);
DisSendData(buf,5);
//在Edit文本框显示发送的数
}
//============================================================================
//发送数据控制网络介面器继电器386电源.
void TMainForm::TransmitControlData(const signed char ControlData)
{
unsigned char buf[5];
buf[0]=0xcc;
buf[1]=0x05;
buf[2]=CONTROL_COM;
buf[3]=ControlData;
buf[4]=crc8(buf,4);
Transmit(buf, 5);
}
//---------------------------------------------------------------------------
//串口接收的一完整帧数据处理
void TMainForm::ReDataAnalysis(const unsigned char *Rbuf, /
unsigned char *Tbuf)
{
unsigned char Oldbuf[15];
String s;
int i;
switch(Rbuf[2])
{
case CONTROL_ACK:
if(B_link==true)
StatusLabel->Caption="已连接网络介面器";
B_link=false;
break;
//其它管理机呼室内机
case MANAGER_CALLROOM:
//其它管理机呼门口机
case MANAGER_CALLDOOR:
//其它管理机呼小区门口机
case MANAGER_CALLSMALLDOOR:
if((Status==ASKSEND)) //等待延时到传送命令
{
SendBusy();
break;
}
if((Status==R_MANCALL)|| //室内机正在呼叫
(Status==D_MANCALL)|| //门口机正在呼叫
(Status==SD_MANCALL)|| //小区门口机正在呼叫
(Status==OTHERMAN_CALL)) //其它管理机正在呼叫
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status=IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
B_Clear=false;
}
else
if((Status==MAN_RCALL)|| //管理机正在呼叫室内机
(Status==MAN_DCALL)|| //管理机正在呼叫门口机
(Status==MAN_SDCALL)|| //管理机正在呼叫小区门口机
(Status==MAN_OTHERMANCALL)|| //管理机正在呼叫其它管理机
(Status==SDTALK)|| //和小区门口机通话状态
(Status==RTALK)|| //和室内机通话状态
(Status==DTALK)|| //和门口机通话状态
(Status==MANTALK)) //和其它管理机机通话状态
{ //发系统忙信息
if(Tbuf[3]!=Rbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status=IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
B_Clear=false;
}
}
else
B_Clear=false;
break;
//收到呼叫命令
case ROOM_CALLMANAGER:
casedo
OR_CALLMANAGER:
case SMALLDOOR_CALLMANAGER:
case CALL_OTHERMANAGER:
if(Status!=IDEL)
{
if((Status==ASKSEND)&&
((Rbuf[2]==SMALLDOOR_CALLMANAGER)||
(Rbuf[2]==CALL_OTHERMANAGER)))
{
SendBusy();
break;
}
if((Status==MAN_RCALL)|| //原来的状态是管理机正在呼室内机
(Status==MAN_DCALL)|| //原来的状态是管理机正在呼叫门口机
(Status==MAN_SDCALL)|| //原来的状态是管理机正在呼叫小区门口机
(Status==MAN_OTHERMANCALL)) //原来的状态是管理机正在呼叫其它管理机
{
if(Rbuf[2]==SMALLDOOR_CALLMANAGER)
{
if((Tbuf[2]!=MANAGER_CALLSMALLDOOR)||
(Tbuf[4]!=Rbuf[3]))
SendBusy();
break;
}
else
if(Rbuf[2]==CALL_OTHERMANAGER)
{
if((Tbuf[2]!=CALL_OTHERMANAGER)||
(Tbuf[4]!=Rbuf[3]))
SendBusy();
break;
}
}
}
else
{//设置状态变量Stauts
if(Rbuf[2]==ROOM_CALLMANAGER)
Status=R_MANCALL;
//室内机正在呼叫
else
if(Rbuf[2]==DOOR_CALLMANAGER)
Status=D_MANCALL;
//门口机正在呼叫
else
if(Rbuf[2]==SMALLDOOR_CALLMANAGER)
Status=SD_MANCALL;
//小区门口机正在呼叫
else
{
if(Rbuf[3]!=ComboBox1->ItemIndex+0xe1)
break;
//收到其它管理机呼,但不是呼本机.
Status=OTHERMAN_CALL;
//其它管理机正在呼叫
}
}
if(FindWindow("TManCallForm","管理机呼叫操作"!=NULL)
{
PostMessage(ManCallForm->Handle,CloseMessage1,0,0);
}
if(FindWindow("TWDForm","读取密码操作"!=NULL)
{
PostMessage(WDForm->Handle,CloseMessage1,0,0);
}
LimitCnt=CALLTIMECONST;
Receive->DisMsg(Rbuf);
//在Receive窗体中显示接收信息,并把接收呼叫
//信息存在Receive窗体中.
Receive->Show();
break;
//询问系统是否在使用中
case ASK_SYS_FREE:
if(Status!=IDEL)
{ //发系统忙信息
SendBusy();
}
break;
//室内分机防区1-防区5报警
case ROOM_ONEAREA_ALARM:
case ROOM_TWOAREA_ALARM:
case ROOM_THREEAREA_ALARM:
case ROOM_FOURAREA_ALARM:
case ROOM_FIVEAREA_ALARM:
s="收到"+IntToStr(Rbuf[3])+"栋"+IntToStr(Rbuf[4])+"层"+
IntToStr(Rbuf[5])+"号室内机"+IntToStr(Rbuf[2]-0xbf)+"防区报警";
MessageBox(NULL,s.c_str(), "报警信息",MB_OK);
break;
//收到忙命令
case SYSBUSY_FROMSWITCH:
B_Clear=false;
if(Rbuf[3]!=ComboBox1->ItemIndex+0xe1)
break;
case SYS_USING:
B_Clear=false;
if(Status!=IDEL)
{
Status=IDEL;
Tbuf[2]=0;
//清除所有要发送的数据
Cbuf[1]=0;
Call->Enabled=true;
StatusLabel->Caption="系统忙";
ShowMessage("系统忙,请稍后再试"
}
break;
//收到门口机传来管理员密码
case TRANS_DOOR_MANAGERPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"管理员", /
"门口机".c_str()), "信息",MB_OK);
}
break;
//收到门口机传来开锁密码
case TRANS_DOOR_UNLOCKPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"开锁", /
"门口机".c_str()), "信息",MB_OK);
}
break;
//收到小区门口机传来管理员密码
case TRANS_SMALLDOOR_MANAGERPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"管理员", /
"小区门口机".c_str()), "信息",MB_OK);
}
break;
//收到小区门口机传来开锁密码
case TRANS_SMALLDOOR_UNLOCKPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"开锁", /
"小区门口机".c_str()), "信息",MB_OK);
}
break;
//管理机清除
case MANAGERCLEAR:
//小区门口机清除
case SMALLDOOR_CLEAR:
if(Status!=IDEL)
{//发系统忙信息
if((Status==MAN_OTHERMANCALL)|| //管理机正在呼叫其它管理机
(Status==MANTALK)) //和其它管理机机通话状态
{
if(Tbuf[3]!=Rbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
if((Status==MAN_SDCALL)|| //管理机正在呼叫小区门口机
(Status==SDTALK)) //和小区门口机通话状态
{
if(Tbuf[4]!=Rbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
if(Status==SD_MANCALL) //小区门口机正在呼叫
{
Receive->GetCallData(Oldbuf);
//得到上次存在Receive窗口的接收数据
if(Rbuf[3]!=Oldbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
if(Status==OTHERMAN_CALL) //其它管理机正在呼叫
{
Receive->GetCallData(Oldbuf);
//得到上次存在Receive窗口的接收数据
if(Rbuf[3]!=Oldbuf[4])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
SendBusy();
}
B_Clear=true;
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
unsigned char Buf[15],temp;
int i;
if(GetWholeFrame(Buf,ReceiverQueue)==true)
{
if(Buf[2]!=CONTROL_ACK)
DisReceData(Buf, Buf[1]);
ReDataAnalysis(Buf, Tbuf);
State=0;
}
switch(Status)
{
case IDEL:
ControlData=CLOSEALL;
break;
//等待延时到发呼叫命令
case ASKSEND:
i=Cbuf[1];
if((i>MAXFRAMELEN)||(i==0)||(LimitCnt==0)||(LimitCnt>30))
{
ClearAndIdel();
break;
}
if(--LimitCnt!=0)
{
break;
}
if(crc8(Cbuf, Cbuf[1]-1)==Cbuf[Cbuf[1]-1])
{
for(i=0;
i<Cbuf[1];
i++)
Tbuf=Cbuf;
Transmit(Tbuf, Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
Cbuf[1]=0;
if((Tbuf[2]==MANAGER_CALLDOOR)||(Tbuf[2]==MANAGER_CALLSMALLDOOR))
Unlock->Enabled=true;
//unlock->enabled=true;
//允许开锁按钮
if(B_Talk==false)
{
DisSendAction(Tbuf);
//在标签中显示呼叫对象
ControlData=CLOSERELAYOPEN386;
//设定转移状态
if(Tbuf[2]==MANAGER_CALLROOM)
Status=MAN_RCALL;
//管理机正在呼叫室内机
else
if(Tbuf[2]==MANAGER_CALLDOOR)
Status=MAN_DCALL;
//管理机正在呼叫门口机
else
if(Tbuf[2]==MANAGER_CALLSMALLDOOR)
Status=MAN_SDCALL;
//管理机正在呼叫小区门口机
else
if(Tbuf[2]==CALL_OTHERMANAGER)
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MAN_OTHERMANCALL;
//管理机正在呼叫其它管理机
}
else
{
ControlData=CLOSEALL;
Status=IDEL;
}
}
else
{
DisTalkLabel(Tbuf);
//在标签Label中显示通话动作
ControlData=CLOSERELAYOPEN386;
if(Tbuf[2]==MANAGER_CALLROOM)
Status=RTALK;
//和室内机通话状态 //管理机正在呼叫室内机
else
if(Tbuf[2]==MANAGER_CALLDOOR)
Status=DTALK;
//和门口机通话状态
else
if(Tbuf[2]==MANAGER_CALLSMALLDOOR)
Status=SDTALK;
//和小区门口机通话状态
else
if(Tbuf[2]==CALL_OTHERMANAGER)
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MANTALK;
//和其它管理机机通话状态
}
else
{
ControlData=CLOSEALL;
Status=IDEL;
}
}
State=0;
//延时打开网络介面器音频电源,以免通信噪音
LimitCnt=CALLTIMECONST;
}
else
ClearAndIdel();
break;
case SDTALK: //和小区门口机通话状态
case RTALK: //和室内机通话状态
case DTALK: //和门口机通话状态
case MANTALK: //和其它管理机机通话状态
case MAN_RCALL: //管理机正在呼叫室内机
case MAN_DCALL: //管理机正在呼叫门口机
case MAN_SDCALL: //管理机正在呼叫小区门口机
case MAN_OTHERMANCALL: //管理机正在呼叫其它管理机
if((LimitCnt==0)||(LimitCnt>CALLTIMECONST))
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
break;
}
if(--LimitCnt==0)
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
Unlock->Enabled=false;
Call->Enabled=true;
}
break;
case R_MANCALL: //室内机正在呼叫
case D_MANCALL: //门口机正在呼叫
case SD_MANCALL: //小区门口机正在呼叫
case OTHERMAN_CALL: //其它管理机正在呼叫
if((LimitCnt==0)||(LimitCnt>CALLTIMECONST))
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
break;
}
if(--LimitCnt==0)
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
}
break;
default:
ClearAndIdel();
Unlock->Enabled=false;
Call->Enabled=true;
break;
}
if(RdLimit!=0)
{
if(RdLimit>50)
RdLimit=0;
else
if(--RdLimit==0)
{
MessageBox(NULL,"读密码失败", "信息",MB_OK);
StatusLabel->Caption="";
}
}
static unsigned int LostCnt;
switch(State)
{
case 0:
CommTestCnt=CONST_500MS;
LostCnt=0;
State=1;
break;
case 1:
if(--CommTestCnt==0)
{
TransmitControlData(ControlData);
CommTestCnt=CONST_500MS;
State=2;
}
break;
case 2:
if(--CommTestCnt==0)
{
if(++LostCnt>=3)
{
StatusLabel->Caption="网络介面器不在线!!!";
B_link=true;
Status=IDEL;
Tbuf[2]=0;
Unlock->Enabled=false;
Call->Enabled=true;
State=0;
}
else
{
TransmitControlData(ControlData);
CommTestCnt=CONST_500MS;
}
}
break;
default:
State=0;
break;
}
}
//---------------------------------------------------------------------------
void _fastcall TMainForm::ReCallBack(TMessage &Message)
{
unsigned char Buf[15];
int i;
Receive->GetCallData(Buf);
//得到上次存在Receive窗口的接收数据
if(Buf[1]>15)
return;
if(crc8(Buf, Buf[1]-1)!=Buf[Buf[1]-1])
return;
if(SpellAckData(Buf,Cbuf)==false)
return;
Call->Enabled=false;
//禁止呼叫按钮
if((Status==R_MANCALL)|| //原先室内机正在呼叫
(Status==D_MANCALL)|| //原先门口机正在呼叫
(Status==SD_MANCALL)|| //原先小区门口机正在呼叫
(Status==OTHERMAN_CALL)) //原先其它管理机正在呼叫
{
B_Talk=true;
if((B_Clear==false)&&(Buf[2]!=CALL_OTHERMANAGER))
{
SpellManAskCom();
LimitCnt=20;
//1s
Status=ASKSend;
}
else
{
for(i=0;
i<Cbuf[1];
i++)
Tbuf=Cbuf;
ControlData=CLOSERELAYOPEN386;
if(Status==R_MANCALL)
Status=RTALK;
else
if(Status==D_MANCALL)
Status=DTALK;
else
if(Status==SD_MANCALL)
Status=SDTALK;
else
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MANTALK;
}
LimitCnt=CALLTIMECONST;
DisTalkLabel(Tbuf);
//在标签Label中显示通话动作
}
Transmit(Tbuf, Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
if((Tbuf[2]==MANAGER_CALLDOOR)||(Tbuf[2]==MANAGER_CALLSMALLDOOR))
Unlock->Enabled=true;
//允许开锁按钮
}
else
{
B_Talk=false;
if(B_Clear==false)
{
SpellManAskCom();
LimitCnt=20;
//1s
Status=ASKSend;
}
else
{
for(i=0;
i<Cbuf[1];
i++)
Tbuf=Cbuf;
ControlData=CLOSERELAYOPEN386;
if(Buf[2]==ROOM_CALLMANAGER)
Status=MAN_RCALL;
//管理机正在呼叫室内机
else
if(Buf[2]==DOOR_CALLMANAGER)
Status=MAN_DCALL;
//管理机正在呼叫门口机
else
if(Buf[2]==SMALLDOOR_CALLMANAGER)
Status=MAN_SDCALL;
//管理机正在呼叫小区门口机
else
if(Buf[2]==CALL_OTHERMANAGER)
{
if(Buf[4]>Buf[3])
ControlData=OPENRELAYOPEN386;
Status=MAN_OTHERMANCALL;
//管理机正在呼叫其它管理机
}
else
{
ControlData=CLOSEALL;
return;
}
DisSendAction(Tbuf);
//在标签Label中显示呼叫动作
LimitCnt=CALLTIMECONST;
}
Transmit(Tbuf, Tbuf[1]);
//发送查询系统忙命令
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
if((Tbuf[2]==MANAGER_CALLDOOR)||(Tbuf[2]==MANAGER_CALLSMALLDOOR))
Unlock->Enabled=true;
//允许开锁按钮
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MSComm1Comm(TObject *Sender)
{
int InfoLength,i;
unsigned char temp;
OleVariant InBuf(OPENARRAY(int,(0,50)),varByte);
switch(MSComm1->CommEvent)
{
case comEvSend:
break;
//以下接收到信息
case comEvReceive:
InfoLength=MSComm1->InBufferCount;
InBuf=MSComm1->Input;
for(int i=0;i<InfoLength;
i++)
{
temp=InBuf.GetElement(i);
QueueInto(ReceiverQueue,&temp,RECEQUEUELEN);
}
break;
default:
break;
}
}
//---------------------------------------------------------------------------
//拼装管理机查询系统是否忙命令
void TMainForm::SpellManAskCom(void)
{
Tbuf[0]=0xcc;
Tbuf[1]=0x05;
Tbuf[2]=ASK_SYS_FREE;
Tbuf[3]=ComboBox1->ItemIndex+0xe1;
Tbuf[4]=crc8(Tbuf, 4);
}
//---------------------------------------------------------------------------
//拼装管理机呼叫数据
void TMainForm::SpellManCallData(unsigned char *Buf,unsigned char *Abuf)
{
unsigned char frame[8];
int i;
frame[0]=0xcc;
frame[2]=Buf[0];
switch(Buf[0])
{
case MANAGER_CALLROOM:
frame[1]=0x08;
frame[3]=ComboBox1->ItemIndex+0xe1;
frame[4]=Buf[1];
frame[5]=Buf[2];
frame[6]=Buf[3];
break;
case MANAGER_CALLDOOR:
frame[1]=0x07;
frame[3]=ComboBox1->ItemIndex+0xe1;
frame[4]=Buf[1];
frame[5]=Buf[2];
break;
case MANAGER_CALLSMALLDOOR:
frame[1]=0x06;
frame[3]=ComboBox1->ItemIndex+0xe1;
frame[4]=Buf[1];
break;
default:
frame[1]=0x06;
frame[4]=ComboBox1->ItemIndex+0xe1;
frame[3]=Buf[1];
break;
}
frame[frame[1]-1]=crc8(frame, frame[1]-1);
for(i=0;
i<frame[1];
i++)
{
Abuf=frame;
//QueueInto(SendQueue, frame+i, RECEQUEUELEN);
}
}
//--------------------------------------7-------------------------------------
//主窗口按下呼叫按钮
void __fastcall TMainForm::CallClick(TObject *Sender)
{
unsigned char Buf[5],myaddr;
if(FindWindow("TManCallForm","管理机呼叫操作"==NULL)
{
ManCallForm=new TManCallForm(Buf,this);
}
if(ManCallForm->ShowModal()==mrOk)
{
if(B_link==true)
{
ShowMessage("网络介面器不在线,请检查电缆是否正确连接."
}
else
if((Buf[0]==CALL_OTHERMANAGER)&&(Buf[1]==(ComboBox1->ItemIndex+0xe1)))
ShowMessage("管理机不能自己呼叫自己"
else
{
B_Talk=false;
if(B_Clear==false)
{
SpellManAskCom();
SpellManCallData(Buf,Cbuf);
Transmit(Tbuf, Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
Status=ASKSend;
LimitCnt=30;
//1.5ss
}
else
{
SpellManCallData(Buf,Tbuf);
Transmit(Tbuf,Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
DisSendAction(Tbuf);
LimitCnt=CALLTIMECONST;
if((Tbuf[2]==MANAGER_CALLSMALLDOOR)|| /
(Tbuf[2]==MANAGER_CALLDOOR))
Unlock->Enabled=true;
ControlData=CLOSERELAYOPEN386;
if(Tbuf[2]==MANAGER_CALLROOM)
Status=MAN_RCALL;
//管理机正在呼叫室内机
else
if(Tbuf[2]==MANAGER_CALLDOOR)
Status=MAN_DCALL;
//管理机正在呼叫门口机
else
if(Tbuf[2]==MANAGER_CALLSMALLDOOR)
Status=MAN_SDCALL;
//管理机正在呼叫小区门口机
else
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MAN_OTHERMANCALL;
//管理机正在呼叫其它管理机
}
}
Call->Enabled=false;
}
}
if(FindWindow("TManCallForm","管理机呼叫操作"!=NULL)
{
delete ManCallForm;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClearScreenClick(TObject *Sender)
{
Edit1->Text="";
ReceText->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ComboBox1Change(TObject *Sender)
{
//MyAddress=ComboBox1->ItemIndex;
}
//---------------------------------------------------------------------------
void TMainForm::SpellRdPWData(unsigned char *Buf, unsigned char *txbuf)
{
int i;
txbuf[0]=0xcc;
txbuf[2]=Buf[0];
txbuf[3]=ComboBox1->ItemIndex+0xe1;
txbuf[4]=Buf[1];
if((txbuf[2]==READ_DOOR_MANAGERPASSWORD)|| /
(txbuf[2]==READ_DOOR_UNLOCKPASSWORD))
{
txbuf[1]=0x07;
txbuf[5]=Buf[2];
}
else
{
txbuf[1]=0x06;
}
txbuf[txbuf[1]-1]=crc8(txbuf, txbuf[1]-1);
}
//---------------------------------------------------------------------------
//主窗口按下读密码按钮
void __fastcall TMainForm::ReadWordsClick(TObject *Sender)
{
unsigned char Buf[3], txbuf[15];
if(FindWindow("TWDForm","读取密码操作"==NULL)
{
WDForm=new TWDForm(Buf,this);
}
if(WDForm->ShowModal()==mrOk)
{
if(B_link==true)
{
ShowMessage("网络介面器不在线,请检查电缆是否正确连接."
}
else
{
SpellRdPWData(Buf, txbuf);
Transmit(txbuf, txbuf[1]);
DisSendData(txbuf,txbuf[1]);
//在Edit文本框显示发送的数据.
StatusLabel->Caption="状态: 正在读密码";
RdLimit=50;
//设置读密码超时时间
}
}
if(FindWindow("TWDForm","读取密码操作"!=NULL)
{
delete WDForm;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if(MSComm1->PortOpen==true)
MSComm1->PortOpen=false;
}
//---------------------------------------------------------------------------
//按下了清除键
void __fastcall TMainForm::ClearBottonClick(TObject *Sender)
{
SendMessage(Receive->Handle,CloseMessage1,0,0);
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
Call->Enabled=true;
//允许呼叫按钮
Unlock->Enabled=false;
//禁止开锁按钮
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::UnlockClick(TObject *Sender)
{
unsigned char buf[8];
if((Tbuf[2]==MANAGER_CALLDOOR)|| /
(Tbuf[2]==MANAGER_CALLSMALLDOOR))
{
if(Tbuf[1]>14)
return;
if(crc8(Tbuf, Tbuf[1]-1)==Tbuf[Tbuf[1]-1])
{
buf[0]=0xcc;
if(Tbuf[2]==MANAGER_CALLDOOR)
{
buf[1]=0x06;
buf[2]=DOOR_UNLOCK;
buf[3]=Tbuf[4];
buf[4]=Tbuf[5];
buf[5]=crc8(buf, 5);
}
else
{
buf[1]=0x05;
buf[2]=SMALLDOOR_UNLOCK;
buf[3]=Tbuf[4];
buf[4]=crc8(buf, 4);
}
Transmit(buf, buf[1]);
//发送数据.
}
}
}
//---------------------------------------------------------------------------
//
//
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Manager.h"
#include "RDWForm.h"
#include "CallForm.h"
#include "Protocol.h"
#include "rece.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma resource "*.dfm"
TMainForm *MainForm;
void * DiHandle;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
// Timer1->Enabled=false;//true;
StatusLabel->Caption="状态: 正在连接网络介面器";
Edit1->Text="";
ReceText->Text="";
ComboBox1->ItemIndex=0;
B_link=true;
CommTestCnt=CONST_500MS;
SetQueuePt(0,0,ReceiverQueue);
if(MSComm1->PortOpen==false)
MSComm1->PortOpen=true;
Status=IDEL;
ControlData=CLOSEALL;
Unlock->Enabled=false;
}
//---------------------------------------------------------------------------
unsigned char crc8(unsigned char *ptr, int len)
{
unsigned char crc;
//0 1 2 3 4 5 6 7 8 9 a b c d e f
const unsigned char crc_ta[256]={
0x00,0x07,0x0e,0x09,0x1c,0x1b,0x12,0x15,0x38,0x3f,0x36,0x31,0x24,0x23,0x2a,0x2d, //0
0x70,0x77,0x7e,0x79,0x6c,0x6b,0x62,0x65,0x48,0x4f,0x46,0x41,0x54,0x53,0x5a,0x5d, //1
0xe0,0xe7,0xee,0xe9,0xfc,0xfb,0xf2,0xf5,0xd8,0xdf,0xd6,0xd1,0xc4,0xc3,0xca,0xcd, //2
0x90,0x97,0x9e,0x99,0x8c,0x8b,0x82,0x85,0xa8,0xaf,0xa6,0xa1,0xb4,0xb3,0xba,0xbd, //3
0xc7,0xc0,0xc9,0xce,0xdb,0xdc,0xd5,0xd2,0xff,0xf8,0xf1,0xf6,0xe3,0xe4,0xed,0xea, //4
0xb7,0xb0,0xb9,0xbe,0xab,0xac,0xa5,0xa2,0x8f,0x88,0x81,0x86,0x93,0x94,0x9d,0x9a, //5
0x27,0x20,0x29,0x2e,0x3b,0x3c,0x35,0x32,0x1f,0x18,0x11,0x16,0x03,0x04,0x0d,0x0a, //6
0x57,0x50,0x59,0x5e,0x4b,0x4c,0x45,0x42,0x6f,0x68,0x61,0x66,0x73,0x74,0x7d,0x7a, //7
0x89,0x8e,0x87,0x80,0x95,0x92,0x9b,0x9c,0xb1,0xb6,0xbf,0xb8,0xad,0xaa,0xa3,0xa4, //8
0xf9,0xfe,0xf7,0xf0,0xe5,0xe2,0xeb,0xec,0xc1,0xc6,0xcf,0xc8,0xdd,0xda,0xd3,0xd4, //9
0x69,0x6e,0x67,0x60,0x75,0x72,0x7b,0x7c,0x51,0x56,0x5f,0x58,0x4d,0x4a,0x43,0x44, //a
0x19,0x1e,0x17,0x10,0x05,0x02,0x0b,0x0c,0x21,0x26,0x2f,0x28,0x3d,0x3a,0x33,0x34, //b
0x4e,0x49,0x40,0x47,0x52,0x55,0x5c,0x5b,0x76,0x71,0x78,0x7f,0x6a,0x6d,0x64,0x63, //c
0x3e,0x39,0x30,0x37,0x22,0x25,0x2c,0x2b,0x06,0x01,0x08,0x0f,0x1a,0x1d,0x14,0x13, //d
0xae,0xa9,0xa0,0xa7,0xb2,0xb5,0xbc,0xbb,0x96,0x91,0x98,0x9f,0x8a,0x8d,0x84,0x83, //e
0xde,0xd9,0xd0,0xd7,0xc2,0xc5,0xcc,0xcb,0xe6,0xe1,0xe8,0xef,0xfa,0xfd,0xf4,0xf3};//f
//0 1 2 3 4 5 6 7 8 9 a b c d e f
crc=0;
while(len!=0)
{
crc=crc_ta[crc^*ptr];
ptr++;
len--;
}
return(crc);
}
//========================================================================
/*
//功能: 队列进队1个字节,同时调整头尾指针。
//
//参数:队列首地址指针QueueHeadPt,源数据指针Source,最大队列容量(字节个数)
// QueueCapacity。
//返回:无。
//说明:* QueueHeadPt=队列头指针,*(QueueHeadPt+1)=队列尾指针,如头指针=尾
// 指针=0时,队列空。
*/
void TMainForm::QueueInto(unsigned char *QueueHeadPt, /
unsigned char *source, unsigned char QueueCapacity)
{
unsigned char *Last;
if(*QueueHeadPt >
QueueCapacity)
*QueueHeadPt = 0;
if(*(QueueHeadPt+1) >
QueueCapacity)
*(QueueHeadPt+1) = 0;
if(*(QueueHeadPt+1) == 0)
{
*QueueHeadPt = 0;
}
if(*QueueHeadPt == 0)
{
if(++(*(QueueHeadPt+1)) >
QueueCapacity)
{//队列从空到满。
*QueueHeadPt = 1;
*(QueueHeadPt+1)=1;
}
}
else
if(*QueueHeadPt == *(QueueHeadPt+1))
{ //队列满
if(++(*(QueueHeadPt+1)) >
QueueCapacity)
*(QueueHeadPt+1)=1;
*QueueHeadPt=*(QueueHeadPt+1);
}
else
if(++(*(QueueHeadPt+1))>QueueCapacity)
{
*(QueueHeadPt+1)=1;
}
Last=QueueHeadPt+(*(QueueHeadPt+1))+1;
* Last=* source;
}
//========================================================================
/*
//功能:队列出队1个字节,从队列复制字节到指定的内存,同时调整头尾指针。
//
//参数:队列首地址指针QueueHeadPt,目的数据指针target,最大队列容量(字节
// 个数)QueueCapacity。
//
//返回:如成功则返回true,失败则返回false.
*/
bool TMainForm::QueueOut(unsigned char *QueueHeadPt, /
unsigned char * target, unsigned char QueueCapacity)
{
unsigned char *Head;
if(* QueueHeadPt>QueueCapacity)
* QueueHeadPt=0;
if(* (QueueHeadPt+1)>QueueCapacity)
* (QueueHeadPt+1)=0;
if(* (QueueHeadPt+1)==0)
{
* QueueHeadPt=0;
return false;
}
if(++(* QueueHeadPt)>QueueCapacity)
* QueueHeadPt=1;
Head=QueueHeadPt+(* QueueHeadPt)+1;
if(*QueueHeadPt == *(QueueHeadPt+1))
{
*QueueHeadPt = 0;
*(QueueHeadPt+1) = 0;
}
*target = *Head;
return true;
}
//========================================================================
//功能:查询一个队列是否空。
//参数:队列(一维数组)首地址指针pointer。
//返回:如空则返回true,不空则返回false.
bool TMainForm::Ask_QueueNull(unsigned char *QueueHeadPt)
{
if(((*QueueHeadPt) | (*(QueueHeadPt+1))) == 0)
return true;
return false;
}
//========================================================================
//功能:初始化一个队列的头尾指针。
//参数:队列头指针QueueHead,指针值QueueEnd,首地址指针QueueHeadPt。
//返回:无。
void TMainForm::SetQueuePt(unsigned char QueueHead, /
unsigned char QueueEnd, unsigned char * QueueHeadPt)
{
*QueueHeadPt++ = QueueHead;
*QueueHeadPt = Queueend;
}
//---------------------------------------------------------------------------
//功能: 把一个队列的头尾指针分别存到HeadPt和EndPt
void TMainForm::GetQueuePt(unsigned char &HeadPt, unsigned char &
endPt, /
unsigned char *QueueHeadPt)
{
HeadPt=* QueueHeadPt++;
EndPt=* QueueHeadPt;
}
//---------------------------------------------------------------------------
//函数功能: 从ReceiverQueue队列提取出一帧完整的信息,信息存在参数Buf所指向
// 的内存里.
//返回: 如提取出一帧完整的信息,返回true,否则返回false.
bool TMainForm::GetWholeFrame(unsigned char *Buf,unsigned char *ReceiverQueue)
{
unsigned char HeadPt, EndPt, temp;
int i;
for(;
{
//检查接收队列里是否有同步头存在,并去掉同步头之前的数据.
while(1)
{
GetQueuePt(HeadPt, EndPt, ReceiverQueue);
if(QueueOut(ReceiverQueue, &temp, RECEQUEUELEN)==false)
{
return false;
}
if(temp==0xcc)
{
break;
}
}
//检查是否收到完整一帧数据
int ByteCount;
if(QueueOut(ReceiverQueue, &temp, RECEQUEUELEN)==false)
{
SetQueuePt(HeadPt, EndPt, ReceiverQueue);
return false;
}
ByteCount=temp;
if((ByteCount<MINFRAMELEN)||(ByteCount>MAXFRAMELEN))
continue;
Buf[0]=0xcc;
Buf[1]=temp;
for(i=2;
i<ByteCount;
i++)
{
if(QueueOut(ReceiverQueue, &temp, RECEQUEUELEN)==false)
{
SetQueuePt(HeadPt, EndPt, ReceiverQueue);
return false;
}
Buf=temp;
}
i--;
temp=crc8(Buf, ByteCount-1);
if(Buf==temp)
return true;
//到此已收到完整一帧信息.
}
}
//---------------------------------------------------------------------------
//将收到的门口机密码转换成数字显示的字符,如是正常的密码则按十进制显示,
//否则按十六进制显示.
String TMainForm::ConvertPassWordDisData(const unsigned char *Buf,String
ansichar, String obj)
{
String pwchar;
String tempstr;
unsigned char temp;
if(((Buf[4])>99)||(Buf[5]>99))
{
pwchar="读到异常的"+obj+ansichar+"密码: "+"0x"+IntToHex(Buf[4],2) /
+"
"+"0x"+IntToHex(Buf[5],2);
}
else
{
temp=Buf[4]/10;
tempstr=IntToStr(temp);
temp=Buf[4]%10;
tempstr=tempstr+IntToStr(temp);
temp=Buf[5]/10;
tempstr=tempstr+IntToStr(temp);
temp=Buf[5]%10;
tempstr=tempstr+IntToStr(temp);
pwchar="读到"+obj+ansichar+"密码: "+tempstr;
}
return pwchar;
}
//---------------------------------------------------------------------------
void TMainForm::Transmit(const unsigned char *Buf, int ByteNum)
{
Variant OutBuf(OPENARRAY(int,(0,0)),varByte);
if(MSComm1->PortOpen==false)
MSComm1->PortOpen=true;
for(int i=0;
i<ByteNum;
i++)
{
OutBuf.PutElement(Buf, 0);
MSComm1->Output=OutBuf;
}
}
//---------------------------------------------------------------------------
//显示发送的数据.
void TMainForm:isSendData(const unsigned char *Buf, int ByteNum)
{
String str;
str="";
for(int i=0;
i<ByteNum;
i++)
{
str=str+IntToHex(Buf,2)+"
";
}
Edit1->Text=str;
}
//---------------------------------------------------------------------------
//在标签Label中显示呼叫动作
void TMainForm:isSendAction(const unsigned char *Tbuf)
{
switch(Tbuf[2])
{
case MANAGER_CALLROOM:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[4])+"栋"
/
+IntToStr(Tbuf[5])+"层"+IntToStr(Tbuf[6])+"号房"+"室内机";
break;
case MANAGER_CALLDOOR:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[4])+"栋"+ /
IntToStr(Tbuf[5]&0x0f)+ "号门口机";
break;
case MANAGER_CALLSMALLDOOR:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[4]&0x0f)+ /
"号小门口机";
break;
case CALL_OTHERMANAGER:
StatusLabel->Caption="状态: 正在呼叫"+IntToStr(Tbuf[3]&0x0f)+"号管理机";
break;
default:
break;
}
}
//---------------------------------------------------------------------------
//在标签Label中显示通话动作
void TMainForm:isTalkLabel(const unsigned char *Tbuf)
{
switch(Tbuf[2])
{
case MANAGER_CALLROOM:
StatusLabel->Caption="正和"+IntToStr(Tbuf[4])+"栋"
/
+IntToStr(Tbuf[5])+"层"+IntToStr(Tbuf[6])+"号房室内机通话中";
break;
case MANAGER_CALLDOOR:
StatusLabel->Caption="正和"+IntToStr(Tbuf[4])+"栋"+ /
IntToStr(Tbuf[5]&0x0f)+ "号门口机通话中";
break;
case MANAGER_CALLSMALLDOOR:
StatusLabel->Caption="正和"+IntToStr(Tbuf[4]&0x0f)+ /
"号小门口机通话中";
break;
case CALL_OTHERMANAGER:
StatusLabel->Caption="正和"+IntToStr(Tbuf[3]&0x0f)+"号管理机通话中";
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void TMainForm:isReceData(const unsigned char *Buf, int ByteNum)
{
String str;
str="";
for(int i=0;
i<ByteNum;
i++)
{
str=str+IntToHex(Buf,2)+"
";
}
ReceText->Lines->Add(str);
}
//---------------------------------------------------------------------------
//功能:管理机收到呼叫,根据收到呼叫数据整合回传的数据,若数据整合成功返回true,
//否则返回false,整合后的数据在Buf所指向的内存里.
bool TMainForm::SpellAckData(const unsigned char *Buf, unsigned char *Stb)
{
unsigned char MyAddress;
bool flag;
flag=false;
MyAddress=ComboBox1->ItemIndex;
if(++MyAddress>9)
return flag;
MyAddress+=0xe0;
switch(Buf[2])
{
case ROOM_CALLMANAGER:
Stb[0]=0xcc;
Stb[1]=0x08;
Stb[2]=MANAGER_CALLROOM;
Stb[3]=MyAddress;
Stb[4]=Buf[3];
Stb[5]=Buf[4];
Stb[6]=Buf[5];
flag=true;
break;
casedo
OR_CALLMANAGER:
Stb[0]=0xcc;
Stb[1]=0x07;
Stb[2]=MANAGER_CALLDOOR;
Stb[3]=MyAddress;
Stb[4]=Buf[3];
Stb[5]=Buf[4];
flag=true;
break;
case SMALLDOOR_CALLMANAGER:
Stb[0]=0xcc;
Stb[1]= 0x06;
Stb[2]=MANAGER_CALLSMALLDOOR;
Stb[3]= MyAddress;
Stb[4]= Buf[3];
flag=true;
break;
case CALL_OTHERMANAGER:
Stb[0]=0xcc;
Stb[1]= 0x06;
Stb[2]=CALL_OTHERMANAGER;
Stb[3]=Buf[4];
Stb[4]=MyAddress;
flag=true;
break;
default:
break;
}
if(flag==true)
{
Stb[Stb[1]-1]=crc8(Stb, Stb[1]-1);
}
return flag;
}
//---------------------------------------------------------------------------
//功能:拼装管理机清除命令数据
void TMainForm::SpellManClrFrame(unsigned char *Buf)
{
Buf[0]=0xcc;
Buf[1]=0x05;
Buf[2]=MANAGERCLEAR;
Buf[3]=ComboBox1->ItemIndex+0xe1;
Buf[4]=crc8(Buf, 4);
}
//---------------------------------------------------------------------------
//发送清除
void TMainForm::ClearAndIdel(void)
{
SpellManClrFrame(Tbuf);
Transmit(Tbuf,Tbuf[1]);
//发送数据.
DisSendData(Tbuf,Tbuf[1]);
//显示发送的数据.
CallCnt=0;
Cbuf[1]=0;
Tbuf[2]=0;
Status=IDEL;
StatusLabel->Caption="待机";
}
//---------------------------------------------------------------------------
//发送忙
void TMainForm::SendBusy(void)
{
unsigned char buf[5];
buf[0]=0xcc;
buf[1]=0x05;
buf[2]=SYS_USING;
buf[3]=ComboBox1->ItemIndex+0xe1;
buf[4]=crc8(buf, 4);
Transmit(buf, 5);
DisSendData(buf,5);
//在Edit文本框显示发送的数
}
//============================================================================
//发送数据控制网络介面器继电器386电源.
void TMainForm::TransmitControlData(const signed char ControlData)
{
unsigned char buf[5];
buf[0]=0xcc;
buf[1]=0x05;
buf[2]=CONTROL_COM;
buf[3]=ControlData;
buf[4]=crc8(buf,4);
Transmit(buf, 5);
}
//---------------------------------------------------------------------------
//串口接收的一完整帧数据处理
void TMainForm::ReDataAnalysis(const unsigned char *Rbuf, /
unsigned char *Tbuf)
{
unsigned char Oldbuf[15];
String s;
int i;
switch(Rbuf[2])
{
case CONTROL_ACK:
if(B_link==true)
StatusLabel->Caption="已连接网络介面器";
B_link=false;
break;
//其它管理机呼室内机
case MANAGER_CALLROOM:
//其它管理机呼门口机
case MANAGER_CALLDOOR:
//其它管理机呼小区门口机
case MANAGER_CALLSMALLDOOR:
if((Status==ASKSEND)) //等待延时到传送命令
{
SendBusy();
break;
}
if((Status==R_MANCALL)|| //室内机正在呼叫
(Status==D_MANCALL)|| //门口机正在呼叫
(Status==SD_MANCALL)|| //小区门口机正在呼叫
(Status==OTHERMAN_CALL)) //其它管理机正在呼叫
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status=IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
B_Clear=false;
}
else
if((Status==MAN_RCALL)|| //管理机正在呼叫室内机
(Status==MAN_DCALL)|| //管理机正在呼叫门口机
(Status==MAN_SDCALL)|| //管理机正在呼叫小区门口机
(Status==MAN_OTHERMANCALL)|| //管理机正在呼叫其它管理机
(Status==SDTALK)|| //和小区门口机通话状态
(Status==RTALK)|| //和室内机通话状态
(Status==DTALK)|| //和门口机通话状态
(Status==MANTALK)) //和其它管理机机通话状态
{ //发系统忙信息
if(Tbuf[3]!=Rbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status=IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
B_Clear=false;
}
}
else
B_Clear=false;
break;
//收到呼叫命令
case ROOM_CALLMANAGER:
casedo
OR_CALLMANAGER:
case SMALLDOOR_CALLMANAGER:
case CALL_OTHERMANAGER:
if(Status!=IDEL)
{
if((Status==ASKSEND)&&
((Rbuf[2]==SMALLDOOR_CALLMANAGER)||
(Rbuf[2]==CALL_OTHERMANAGER)))
{
SendBusy();
break;
}
if((Status==MAN_RCALL)|| //原来的状态是管理机正在呼室内机
(Status==MAN_DCALL)|| //原来的状态是管理机正在呼叫门口机
(Status==MAN_SDCALL)|| //原来的状态是管理机正在呼叫小区门口机
(Status==MAN_OTHERMANCALL)) //原来的状态是管理机正在呼叫其它管理机
{
if(Rbuf[2]==SMALLDOOR_CALLMANAGER)
{
if((Tbuf[2]!=MANAGER_CALLSMALLDOOR)||
(Tbuf[4]!=Rbuf[3]))
SendBusy();
break;
}
else
if(Rbuf[2]==CALL_OTHERMANAGER)
{
if((Tbuf[2]!=CALL_OTHERMANAGER)||
(Tbuf[4]!=Rbuf[3]))
SendBusy();
break;
}
}
}
else
{//设置状态变量Stauts
if(Rbuf[2]==ROOM_CALLMANAGER)
Status=R_MANCALL;
//室内机正在呼叫
else
if(Rbuf[2]==DOOR_CALLMANAGER)
Status=D_MANCALL;
//门口机正在呼叫
else
if(Rbuf[2]==SMALLDOOR_CALLMANAGER)
Status=SD_MANCALL;
//小区门口机正在呼叫
else
{
if(Rbuf[3]!=ComboBox1->ItemIndex+0xe1)
break;
//收到其它管理机呼,但不是呼本机.
Status=OTHERMAN_CALL;
//其它管理机正在呼叫
}
}
if(FindWindow("TManCallForm","管理机呼叫操作"!=NULL)
{
PostMessage(ManCallForm->Handle,CloseMessage1,0,0);
}
if(FindWindow("TWDForm","读取密码操作"!=NULL)
{
PostMessage(WDForm->Handle,CloseMessage1,0,0);
}
LimitCnt=CALLTIMECONST;
Receive->DisMsg(Rbuf);
//在Receive窗体中显示接收信息,并把接收呼叫
//信息存在Receive窗体中.
Receive->Show();
break;
//询问系统是否在使用中
case ASK_SYS_FREE:
if(Status!=IDEL)
{ //发系统忙信息
SendBusy();
}
break;
//室内分机防区1-防区5报警
case ROOM_ONEAREA_ALARM:
case ROOM_TWOAREA_ALARM:
case ROOM_THREEAREA_ALARM:
case ROOM_FOURAREA_ALARM:
case ROOM_FIVEAREA_ALARM:
s="收到"+IntToStr(Rbuf[3])+"栋"+IntToStr(Rbuf[4])+"层"+
IntToStr(Rbuf[5])+"号室内机"+IntToStr(Rbuf[2]-0xbf)+"防区报警";
MessageBox(NULL,s.c_str(), "报警信息",MB_OK);
break;
//收到忙命令
case SYSBUSY_FROMSWITCH:
B_Clear=false;
if(Rbuf[3]!=ComboBox1->ItemIndex+0xe1)
break;
case SYS_USING:
B_Clear=false;
if(Status!=IDEL)
{
Status=IDEL;
Tbuf[2]=0;
//清除所有要发送的数据
Cbuf[1]=0;
Call->Enabled=true;
StatusLabel->Caption="系统忙";
ShowMessage("系统忙,请稍后再试"
}
break;
//收到门口机传来管理员密码
case TRANS_DOOR_MANAGERPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"管理员", /
"门口机".c_str()), "信息",MB_OK);
}
break;
//收到门口机传来开锁密码
case TRANS_DOOR_UNLOCKPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"开锁", /
"门口机".c_str()), "信息",MB_OK);
}
break;
//收到小区门口机传来管理员密码
case TRANS_SMALLDOOR_MANAGERPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"管理员", /
"小区门口机".c_str()), "信息",MB_OK);
}
break;
//收到小区门口机传来开锁密码
case TRANS_SMALLDOOR_UNLOCKPASSWORD:
if(Rbuf[3]==ComboBox1->ItemIndex+0xe1)
{
RdLimit=0;
StatusLabel->Caption="";
MessageBox(NULL,(ConvertPassWordDisData(Rbuf,"开锁", /
"小区门口机".c_str()), "信息",MB_OK);
}
break;
//管理机清除
case MANAGERCLEAR:
//小区门口机清除
case SMALLDOOR_CLEAR:
if(Status!=IDEL)
{//发系统忙信息
if((Status==MAN_OTHERMANCALL)|| //管理机正在呼叫其它管理机
(Status==MANTALK)) //和其它管理机机通话状态
{
if(Tbuf[3]!=Rbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
if((Status==MAN_SDCALL)|| //管理机正在呼叫小区门口机
(Status==SDTALK)) //和小区门口机通话状态
{
if(Tbuf[4]!=Rbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
if(Status==SD_MANCALL) //小区门口机正在呼叫
{
Receive->GetCallData(Oldbuf);
//得到上次存在Receive窗口的接收数据
if(Rbuf[3]!=Oldbuf[3])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
if(Status==OTHERMAN_CALL) //其它管理机正在呼叫
{
Receive->GetCallData(Oldbuf);
//得到上次存在Receive窗口的接收数据
if(Rbuf[3]!=Oldbuf[4])
SendBusy();
else
{
PostMessage(Receive->Handle,CloseReceForm,0,0);
Status==IDEL;
Tbuf[2]=0;
StatusLabel->Caption="待机";
Call->Enabled=true;
}
}
else
SendBusy();
}
B_Clear=true;
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
unsigned char Buf[15],temp;
int i;
if(GetWholeFrame(Buf,ReceiverQueue)==true)
{
if(Buf[2]!=CONTROL_ACK)
DisReceData(Buf, Buf[1]);
ReDataAnalysis(Buf, Tbuf);
State=0;
}
switch(Status)
{
case IDEL:
ControlData=CLOSEALL;
break;
//等待延时到发呼叫命令
case ASKSEND:
i=Cbuf[1];
if((i>MAXFRAMELEN)||(i==0)||(LimitCnt==0)||(LimitCnt>30))
{
ClearAndIdel();
break;
}
if(--LimitCnt!=0)
{
break;
}
if(crc8(Cbuf, Cbuf[1]-1)==Cbuf[Cbuf[1]-1])
{
for(i=0;
i<Cbuf[1];
i++)
Tbuf=Cbuf;
Transmit(Tbuf, Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
Cbuf[1]=0;
if((Tbuf[2]==MANAGER_CALLDOOR)||(Tbuf[2]==MANAGER_CALLSMALLDOOR))
Unlock->Enabled=true;
//unlock->enabled=true;
//允许开锁按钮
if(B_Talk==false)
{
DisSendAction(Tbuf);
//在标签中显示呼叫对象
ControlData=CLOSERELAYOPEN386;
//设定转移状态
if(Tbuf[2]==MANAGER_CALLROOM)
Status=MAN_RCALL;
//管理机正在呼叫室内机
else
if(Tbuf[2]==MANAGER_CALLDOOR)
Status=MAN_DCALL;
//管理机正在呼叫门口机
else
if(Tbuf[2]==MANAGER_CALLSMALLDOOR)
Status=MAN_SDCALL;
//管理机正在呼叫小区门口机
else
if(Tbuf[2]==CALL_OTHERMANAGER)
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MAN_OTHERMANCALL;
//管理机正在呼叫其它管理机
}
else
{
ControlData=CLOSEALL;
Status=IDEL;
}
}
else
{
DisTalkLabel(Tbuf);
//在标签Label中显示通话动作
ControlData=CLOSERELAYOPEN386;
if(Tbuf[2]==MANAGER_CALLROOM)
Status=RTALK;
//和室内机通话状态 //管理机正在呼叫室内机
else
if(Tbuf[2]==MANAGER_CALLDOOR)
Status=DTALK;
//和门口机通话状态
else
if(Tbuf[2]==MANAGER_CALLSMALLDOOR)
Status=SDTALK;
//和小区门口机通话状态
else
if(Tbuf[2]==CALL_OTHERMANAGER)
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MANTALK;
//和其它管理机机通话状态
}
else
{
ControlData=CLOSEALL;
Status=IDEL;
}
}
State=0;
//延时打开网络介面器音频电源,以免通信噪音
LimitCnt=CALLTIMECONST;
}
else
ClearAndIdel();
break;
case SDTALK: //和小区门口机通话状态
case RTALK: //和室内机通话状态
case DTALK: //和门口机通话状态
case MANTALK: //和其它管理机机通话状态
case MAN_RCALL: //管理机正在呼叫室内机
case MAN_DCALL: //管理机正在呼叫门口机
case MAN_SDCALL: //管理机正在呼叫小区门口机
case MAN_OTHERMANCALL: //管理机正在呼叫其它管理机
if((LimitCnt==0)||(LimitCnt>CALLTIMECONST))
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
break;
}
if(--LimitCnt==0)
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
Unlock->Enabled=false;
Call->Enabled=true;
}
break;
case R_MANCALL: //室内机正在呼叫
case D_MANCALL: //门口机正在呼叫
case SD_MANCALL: //小区门口机正在呼叫
case OTHERMAN_CALL: //其它管理机正在呼叫
if((LimitCnt==0)||(LimitCnt>CALLTIMECONST))
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
break;
}
if(--LimitCnt==0)
{
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
}
break;
default:
ClearAndIdel();
Unlock->Enabled=false;
Call->Enabled=true;
break;
}
if(RdLimit!=0)
{
if(RdLimit>50)
RdLimit=0;
else
if(--RdLimit==0)
{
MessageBox(NULL,"读密码失败", "信息",MB_OK);
StatusLabel->Caption="";
}
}
static unsigned int LostCnt;
switch(State)
{
case 0:
CommTestCnt=CONST_500MS;
LostCnt=0;
State=1;
break;
case 1:
if(--CommTestCnt==0)
{
TransmitControlData(ControlData);
CommTestCnt=CONST_500MS;
State=2;
}
break;
case 2:
if(--CommTestCnt==0)
{
if(++LostCnt>=3)
{
StatusLabel->Caption="网络介面器不在线!!!";
B_link=true;
Status=IDEL;
Tbuf[2]=0;
Unlock->Enabled=false;
Call->Enabled=true;
State=0;
}
else
{
TransmitControlData(ControlData);
CommTestCnt=CONST_500MS;
}
}
break;
default:
State=0;
break;
}
}
//---------------------------------------------------------------------------
void _fastcall TMainForm::ReCallBack(TMessage &Message)
{
unsigned char Buf[15];
int i;
Receive->GetCallData(Buf);
//得到上次存在Receive窗口的接收数据
if(Buf[1]>15)
return;
if(crc8(Buf, Buf[1]-1)!=Buf[Buf[1]-1])
return;
if(SpellAckData(Buf,Cbuf)==false)
return;
Call->Enabled=false;
//禁止呼叫按钮
if((Status==R_MANCALL)|| //原先室内机正在呼叫
(Status==D_MANCALL)|| //原先门口机正在呼叫
(Status==SD_MANCALL)|| //原先小区门口机正在呼叫
(Status==OTHERMAN_CALL)) //原先其它管理机正在呼叫
{
B_Talk=true;
if((B_Clear==false)&&(Buf[2]!=CALL_OTHERMANAGER))
{
SpellManAskCom();
LimitCnt=20;
//1s
Status=ASKSend;
}
else
{
for(i=0;
i<Cbuf[1];
i++)
Tbuf=Cbuf;
ControlData=CLOSERELAYOPEN386;
if(Status==R_MANCALL)
Status=RTALK;
else
if(Status==D_MANCALL)
Status=DTALK;
else
if(Status==SD_MANCALL)
Status=SDTALK;
else
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MANTALK;
}
LimitCnt=CALLTIMECONST;
DisTalkLabel(Tbuf);
//在标签Label中显示通话动作
}
Transmit(Tbuf, Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
if((Tbuf[2]==MANAGER_CALLDOOR)||(Tbuf[2]==MANAGER_CALLSMALLDOOR))
Unlock->Enabled=true;
//允许开锁按钮
}
else
{
B_Talk=false;
if(B_Clear==false)
{
SpellManAskCom();
LimitCnt=20;
//1s
Status=ASKSend;
}
else
{
for(i=0;
i<Cbuf[1];
i++)
Tbuf=Cbuf;
ControlData=CLOSERELAYOPEN386;
if(Buf[2]==ROOM_CALLMANAGER)
Status=MAN_RCALL;
//管理机正在呼叫室内机
else
if(Buf[2]==DOOR_CALLMANAGER)
Status=MAN_DCALL;
//管理机正在呼叫门口机
else
if(Buf[2]==SMALLDOOR_CALLMANAGER)
Status=MAN_SDCALL;
//管理机正在呼叫小区门口机
else
if(Buf[2]==CALL_OTHERMANAGER)
{
if(Buf[4]>Buf[3])
ControlData=OPENRELAYOPEN386;
Status=MAN_OTHERMANCALL;
//管理机正在呼叫其它管理机
}
else
{
ControlData=CLOSEALL;
return;
}
DisSendAction(Tbuf);
//在标签Label中显示呼叫动作
LimitCnt=CALLTIMECONST;
}
Transmit(Tbuf, Tbuf[1]);
//发送查询系统忙命令
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
if((Tbuf[2]==MANAGER_CALLDOOR)||(Tbuf[2]==MANAGER_CALLSMALLDOOR))
Unlock->Enabled=true;
//允许开锁按钮
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MSComm1Comm(TObject *Sender)
{
int InfoLength,i;
unsigned char temp;
OleVariant InBuf(OPENARRAY(int,(0,50)),varByte);
switch(MSComm1->CommEvent)
{
case comEvSend:
break;
//以下接收到信息
case comEvReceive:
InfoLength=MSComm1->InBufferCount;
InBuf=MSComm1->Input;
for(int i=0;i<InfoLength;
i++)
{
temp=InBuf.GetElement(i);
QueueInto(ReceiverQueue,&temp,RECEQUEUELEN);
}
break;
default:
break;
}
}
//---------------------------------------------------------------------------
//拼装管理机查询系统是否忙命令
void TMainForm::SpellManAskCom(void)
{
Tbuf[0]=0xcc;
Tbuf[1]=0x05;
Tbuf[2]=ASK_SYS_FREE;
Tbuf[3]=ComboBox1->ItemIndex+0xe1;
Tbuf[4]=crc8(Tbuf, 4);
}
//---------------------------------------------------------------------------
//拼装管理机呼叫数据
void TMainForm::SpellManCallData(unsigned char *Buf,unsigned char *Abuf)
{
unsigned char frame[8];
int i;
frame[0]=0xcc;
frame[2]=Buf[0];
switch(Buf[0])
{
case MANAGER_CALLROOM:
frame[1]=0x08;
frame[3]=ComboBox1->ItemIndex+0xe1;
frame[4]=Buf[1];
frame[5]=Buf[2];
frame[6]=Buf[3];
break;
case MANAGER_CALLDOOR:
frame[1]=0x07;
frame[3]=ComboBox1->ItemIndex+0xe1;
frame[4]=Buf[1];
frame[5]=Buf[2];
break;
case MANAGER_CALLSMALLDOOR:
frame[1]=0x06;
frame[3]=ComboBox1->ItemIndex+0xe1;
frame[4]=Buf[1];
break;
default:
frame[1]=0x06;
frame[4]=ComboBox1->ItemIndex+0xe1;
frame[3]=Buf[1];
break;
}
frame[frame[1]-1]=crc8(frame, frame[1]-1);
for(i=0;
i<frame[1];
i++)
{
Abuf=frame;
//QueueInto(SendQueue, frame+i, RECEQUEUELEN);
}
}
//--------------------------------------7-------------------------------------
//主窗口按下呼叫按钮
void __fastcall TMainForm::CallClick(TObject *Sender)
{
unsigned char Buf[5],myaddr;
if(FindWindow("TManCallForm","管理机呼叫操作"==NULL)
{
ManCallForm=new TManCallForm(Buf,this);
}
if(ManCallForm->ShowModal()==mrOk)
{
if(B_link==true)
{
ShowMessage("网络介面器不在线,请检查电缆是否正确连接."
}
else
if((Buf[0]==CALL_OTHERMANAGER)&&(Buf[1]==(ComboBox1->ItemIndex+0xe1)))
ShowMessage("管理机不能自己呼叫自己"
else
{
B_Talk=false;
if(B_Clear==false)
{
SpellManAskCom();
SpellManCallData(Buf,Cbuf);
Transmit(Tbuf, Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
//在Edit文本框显示发送的数据.
Status=ASKSend;
LimitCnt=30;
//1.5ss
}
else
{
SpellManCallData(Buf,Tbuf);
Transmit(Tbuf,Tbuf[1]);
DisSendData(Tbuf,Tbuf[1]);
DisSendAction(Tbuf);
LimitCnt=CALLTIMECONST;
if((Tbuf[2]==MANAGER_CALLSMALLDOOR)|| /
(Tbuf[2]==MANAGER_CALLDOOR))
Unlock->Enabled=true;
ControlData=CLOSERELAYOPEN386;
if(Tbuf[2]==MANAGER_CALLROOM)
Status=MAN_RCALL;
//管理机正在呼叫室内机
else
if(Tbuf[2]==MANAGER_CALLDOOR)
Status=MAN_DCALL;
//管理机正在呼叫门口机
else
if(Tbuf[2]==MANAGER_CALLSMALLDOOR)
Status=MAN_SDCALL;
//管理机正在呼叫小区门口机
else
{
if(Tbuf[3]>Tbuf[4])
ControlData=OPENRELAYOPEN386;
Status=MAN_OTHERMANCALL;
//管理机正在呼叫其它管理机
}
}
Call->Enabled=false;
}
}
if(FindWindow("TManCallForm","管理机呼叫操作"!=NULL)
{
delete ManCallForm;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClearScreenClick(TObject *Sender)
{
Edit1->Text="";
ReceText->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ComboBox1Change(TObject *Sender)
{
//MyAddress=ComboBox1->ItemIndex;
}
//---------------------------------------------------------------------------
void TMainForm::SpellRdPWData(unsigned char *Buf, unsigned char *txbuf)
{
int i;
txbuf[0]=0xcc;
txbuf[2]=Buf[0];
txbuf[3]=ComboBox1->ItemIndex+0xe1;
txbuf[4]=Buf[1];
if((txbuf[2]==READ_DOOR_MANAGERPASSWORD)|| /
(txbuf[2]==READ_DOOR_UNLOCKPASSWORD))
{
txbuf[1]=0x07;
txbuf[5]=Buf[2];
}
else
{
txbuf[1]=0x06;
}
txbuf[txbuf[1]-1]=crc8(txbuf, txbuf[1]-1);
}
//---------------------------------------------------------------------------
//主窗口按下读密码按钮
void __fastcall TMainForm::ReadWordsClick(TObject *Sender)
{
unsigned char Buf[3], txbuf[15];
if(FindWindow("TWDForm","读取密码操作"==NULL)
{
WDForm=new TWDForm(Buf,this);
}
if(WDForm->ShowModal()==mrOk)
{
if(B_link==true)
{
ShowMessage("网络介面器不在线,请检查电缆是否正确连接."
}
else
{
SpellRdPWData(Buf, txbuf);
Transmit(txbuf, txbuf[1]);
DisSendData(txbuf,txbuf[1]);
//在Edit文本框显示发送的数据.
StatusLabel->Caption="状态: 正在读密码";
RdLimit=50;
//设置读密码超时时间
}
}
if(FindWindow("TWDForm","读取密码操作"!=NULL)
{
delete WDForm;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if(MSComm1->PortOpen==true)
MSComm1->PortOpen=false;
}
//---------------------------------------------------------------------------
//按下了清除键
void __fastcall TMainForm::ClearBottonClick(TObject *Sender)
{
SendMessage(Receive->Handle,CloseMessage1,0,0);
ControlData=CLOSEALL;
TransmitControlData(ControlData);
ClearAndIdel();
Call->Enabled=true;
//允许呼叫按钮
Unlock->Enabled=false;
//禁止开锁按钮
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::UnlockClick(TObject *Sender)
{
unsigned char buf[8];
if((Tbuf[2]==MANAGER_CALLDOOR)|| /
(Tbuf[2]==MANAGER_CALLSMALLDOOR))
{
if(Tbuf[1]>14)
return;
if(crc8(Tbuf, Tbuf[1]-1)==Tbuf[Tbuf[1]-1])
{
buf[0]=0xcc;
if(Tbuf[2]==MANAGER_CALLDOOR)
{
buf[1]=0x06;
buf[2]=DOOR_UNLOCK;
buf[3]=Tbuf[4];
buf[4]=Tbuf[5];
buf[5]=crc8(buf, 5);
}
else
{
buf[1]=0x05;
buf[2]=SMALLDOOR_UNLOCK;
buf[3]=Tbuf[4];
buf[4]=crc8(buf, 4);
}
Transmit(buf, buf[1]);
//发送数据.
}
}
}
//---------------------------------------------------------------------------