📄 tcomm.cpp
字号:
//
//---------------------------------------------------------------------------
Byte __fastcall TReadThread::FWaveCom(void)
{
int smsflag = SMSBuff.Pos("\r\n");
if (smsflag > 0)
{
if (smsflag == 1)
{
// 结束标志符号在头位,则去掉结束标志符
SMSBuff = SMSBuff.Delete(1, 2);
}
else
{
String msg = SMSBuff.SubString(1, smsflag-1);
SMSBuff = SMSBuff.Delete(1, smsflag+1);
if (HaveMsg){
HaveMsg = false;
if (!PhoneNum.IsEmpty())
{
TCommBuff *ReadBuff = new TCommBuff();
ReadBuff->FSetMsg(msg.Trim().c_str(),msg.Trim().Length());
ReadBuff->Phone = PhoneNum;
ReadQueue->Push(ReadBuff);
}
//else{
//PDU
//}
PhoneNum = PhoneNum.Delete(1,PhoneNum.Length());
return 0x10;
}
smsflag = msg.Pos("+CMGS:");
if (smsflag > 0) {
return 0x01;
}
smsflag = msg.Pos("ERROR");
if (smsflag > 0) {
return 0x02;
}
smsflag = msg.Pos("+CMT:");
if (smsflag > 0 ) {
smsflag = msg.Pos("\"");
if (smsflag > 0)
PhoneNum = msg.SubString(msg.Pos("\"")+1, msg.Pos("\",")-msg.Pos("\"")-1);
HaveMsg = true;
return 0x00;
}
smsflag = msg.Pos("+CMGR:");
if (smsflag > 0){
msg = msg.SubString(msg.Pos(",\"")+2,msg.Length()-msg.Pos(",\"")+2);
PhoneNum = msg.SubString(0,msg.Pos("\",")-1);
HaveMsg = true;
return 0x00;
}
smsflag = msg.Pos("+CMTI:");
if (smsflag > 0){
int index = StrToInt(msg.SubString(msg.Pos(",")+1, msg.Length()));
sendControl = 0x40 | index;
return 0x00;
}
smsflag = msg.Pos("OK");
if (smsflag > 0)
{
return 0x03;
}
}
}
return 0x00;
}
//---------------------------------------------------------------------------
// FClearQueue::清除接收队列函数
//
//
//---------------------------------------------------------------------------
void __fastcall TReadThread::FClearQueue(void)
{
while (ReadQueue->Count()!= 0)
ReadQueue->Pop();
}
//---------------------------------------------------------------------------
// QueueIsNULL::获取接收队列是否为空
//
//
//---------------------------------------------------------------------------
bool __fastcall TReadThread::QueueIsNULL(void)
{
return (ReadQueue->Count() == 0)? true:false;
}
//---------------------------------------------------------------------------
//End of TReadThread
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//Start of TWriteThread
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
__fastcall TWriteThread::TWriteThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
WriteQueue = new TQueue();
}
//---------------------------------------------------------------------------
// ~TWriteThread::析构函数,释放建立的线程
//
//
//---------------------------------------------------------------------------
__fastcall TWriteThread::~TWriteThread(void)
{
WriteQueue->~TQueue();
}
//---------------------------------------------------------------------------
// Execute::线程Run函数
//
//
//---------------------------------------------------------------------------
void __fastcall TWriteThread::Execute()
{
char TxBuff[1024]={'\0'};
DWORD lrc;
long Size;
int reSendCount = 1;
while(!Terminated)
{
if (WriteQueue->Count() != 0 && sendControl < 64)
{
TCommBuff *SendBuff = (TCommBuff *)WriteQueue->Pop();
Size = SendBuff->Size;
if (SendBuff->Phone != NULL)
{
String SendData;
SendData = "AT+CMGS=\""+ SendBuff->Phone + "\"\015";
WriteFile(hComm,SendData.c_str(),SendData.Length(),&lrc,NULL);
TxBuff[Size] = '\032';
Size +=1;
Sleep(30);
}
//发送内容
SendBuff->FGetMsg(TxBuff,SendBuff->Size);
WriteFile(hComm,TxBuff,Size,&lrc,NULL);
//短信时判断发送状态
if (SendBuff->Phone != NULL)
{
ResetEvent(sendEvent);
if (WaitForSingleObject(sendEvent, 10000) == WAIT_TIMEOUT)
{
//TODO发送超时
PostMessage( FHWnd, WM_SENDCOMMSTATE,WPARAM(0x03),0);
}
}
//释放发送的实例
delete SendBuff;
}
else
{
if ((sendControl & 0xC0) == 0xC0)
{
String SendData;
SendData = "AT+CMGD=1,2\015";
WriteFile(hComm,SendData.c_str(),SendData.Length(),&lrc,NULL);
ResetEvent(okEvent);
if (WaitForSingleObject(okEvent, (4000*reSendCount)) == WAIT_TIMEOUT)
{
if (reSendCount < 4)
reSendCount++;
else{
reSendCount=1;
sendControl = 0x00;
}
}
else
{
reSendCount=1;
sendControl = 0x00;
}
}
else if ((sendControl & 0xC0) == 0x80)
{
String SendData;
SendData = "AT+CNMI=2,2,0,0,0\015";
WriteFile(hComm,SendData.c_str(),SendData.Length(),&lrc,NULL);
ResetEvent(okEvent);
if (WaitForSingleObject(okEvent, (1000*reSendCount)) == WAIT_TIMEOUT)
{
if (reSendCount < 4)
reSendCount++;
else{
reSendCount=1;
sendControl = 0x00;
}
}
else
{
reSendCount=1;
sendControl = 0xC0;
}
}
else if((sendControl & 0xC0) == 0x40)
{
String SendData;
int index = sendControl & 0x3F;
SendData = "AT+CMGR=" + IntToStr(index)+"\015";
WriteFile(hComm,SendData.c_str(),SendData.Length(),&lrc,NULL);
ResetEvent(okEvent);
if (WaitForSingleObject(okEvent, (2000*reSendCount)) == WAIT_TIMEOUT)
{
if (reSendCount < 4)
reSendCount++;
else{
reSendCount=1;
sendControl = 0x00;
}
}
else
{
reSendCount=1;
sendControl = 0x80;
}
}
Sleep(40);
}
}
}
//---------------------------------------------------------------------------
// SendMsg::发送提交函数
//
//
//---------------------------------------------------------------------------
void __fastcall TWriteThread::SendMsg(TCommBuff *NewSendBuff)
{
WriteQueue->Push(NewSendBuff);
}
//---------------------------------------------------------------------------
// FClearQueue::清除发送队列函数
//
//
//---------------------------------------------------------------------------
void __fastcall TWriteThread::FClearQueue(void)
{
while (WriteQueue->Count()!= 0)
WriteQueue->Pop();
}
//---------------------------------------------------------------------------
// QueueIsNULL::获取发送队列是否为空
//
//
//---------------------------------------------------------------------------
bool __fastcall TWriteThread::QueueIsNULL(void)
{
return (WriteQueue->Count() == 0)? true:false;
}
//---------------------------------------------------------------------------
//End of TWriteThread
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//Start of TCommBuff
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
TCommBuff::TCommBuff()
{
FPhone = NULL;
FSize = 0;
}
//---------------------------------------------------------------------------
// ~TCommBuff::析构函数,释放建立的队列
//
//
//---------------------------------------------------------------------------
TCommBuff::~TCommBuff()
{
}
//---------------------------------------------------------------------------
// FSetPhone::
//
//
//---------------------------------------------------------------------------
void TCommBuff::FSetPhone(String NewPhone)
{
FPhone = NewPhone;
}
//---------------------------------------------------------------------------
// FGetPhone::
//
//
//---------------------------------------------------------------------------
String TCommBuff::FGetPhone(void)
{
return FPhone;
}
//---------------------------------------------------------------------------
// FSetMsg::
//
//
//---------------------------------------------------------------------------
void TCommBuff::FSetMsg(char far *NewMsg,long Size)
{
memcpy(FMsg,NewMsg,Size);
FSize = Size;
}
//---------------------------------------------------------------------------
// FGetMsg::
//
//
//---------------------------------------------------------------------------
void TCommBuff::FGetMsg(char far *AimMsg,long Size)
{
memcpy(AimMsg,FMsg,Size);
}
//---------------------------------------------------------------------------
// FGetSize::
//
//
//---------------------------------------------------------------------------
long TCommBuff::FGetSize(void)
{
return FSize;
}
//---------------------------------------------------------------------------
//End of TCommBuff
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
namespace Tcomm
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TTComm)};
RegisterComponents("Telestone", classes, 0);
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -