📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "gprs_ppp.h"
#include "gprs_ip.h"
#include "gprs_udp.h"
#include "gprs_net.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CPort"
#pragma resource "*.dfm"
TForm1 *Form1;
extern NODE locnode;
//---------------------------------------------------------------------------
int TForm1::HexToInt(AnsiString S)
{
int i;
char *cp=new char[3];
strcpy( cp, S.c_str() );
if (*cp>='A')
i=10+(*cp-'A');
else
i=*cp-0x30;
cp++;
i<<=4;
if (*cp>='A')
i+=10+(*cp-'A');
else
i+=*cp-0x30;
cp=NULL;
delete[] cp;
return i;
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ComPort1->ShowSetupDialog();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Edit1->Text="+++";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if((Key >= '0' && Key <= '9') || (Key >= 'a' && Key <= 'f') || (Key >= 'A' && Key <= 'F') || Key == VK_BACK || Key == VK_DELETE)
{
}
else
{
Key = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Edit1->Text="AT\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TickCount=0;
UartInBytes=0;
Edit1->Clear();
Edit2->Clear();
IsReceiving=false;
IsConnecting=false;
BeingConnecting=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if (ComPort1->Connected==False) return;
if (UartInBytes==ComPort1->InputCount())
{
if (UartInBytes==0)
{
if ((GetTickCount()-TickCount>2000) && IsReceiving)
{
RichEdit1->Lines->Add("MC35模块 没有应答");
IsReceiving=false;
}
}
else
{
ComPort1->Read(ReceiveBuf,UartInBytes);
Edit2->Text=AnsiString(ReceiveBuf).SubString(1,UartInBytes);
int i;
AnsiString MyHexStr;
AnsiString TempString;
if(IsReceiving) IsReceiving=false;
if (BeingConnecting)
{
if (StrPos(ReceiveBuf, AnsiString("CONNECT").c_str()))
{
BeingConnecting=false;
IsConnecting=true;
PPPSendVoidLCP();
}
}
else
if (IsConnecting)
{
for (i=0;i<UartInBytes;i++)
{
if ((ReceiveBuf[i]==0x7e) && (ReceiveBuf[i+1]!=0x7e))
break;
}
if (i==UartInBytes) return;
int j=0;
i++;
bool IsEsc=false;
for (;i<UartInBytes;i++)
{
if (ReceiveBuf[i]==0x7E) break;
if (ReceiveBuf[i]==0x7D)
IsEsc=true;
else
{
if (IsEsc)
{
InBuffer[j++]=ReceiveBuf[i]^0x20;
IsEsc=false;
}
else
InBuffer[j++]=ReceiveBuf[i];
}
}
MyHexStr="接收数据:";
TempString=IntToHex(0x7e,2);
MyHexStr+=TempString.SubString(TempString.Length()-1,2);
MyHexStr+=" ";
for (i=0;i<j;i++)
{
TempString=IntToHex(InBuffer[i],2);
MyHexStr+=TempString.SubString(TempString.Length()-1,2);
MyHexStr+=" ";
}
TempString=IntToHex(0x7e,2);
MyHexStr+=TempString.SubString(TempString.Length()-1,2);
MyHexStr+=" ";
Uart_Receive_Count=j-6;
RichEdit1->Lines->Add(MyHexStr);
PPPEntry();
}
}
}
else
UartInBytes=ComPort1->InputCount();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
if (ComPort1->Connected==false)
ComPort1->Open();
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ComPort1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{
Edit1->Text="AT+CGATT=1\r\n";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
Edit2->Clear();
if (Button7->Caption=="打开回显")
{
Button7->Caption="关闭回显";
Edit1->Text="ATE1\r";
ComPort1->WriteStr(Edit1->Text);
}
else
{
Button7->Caption="打开回显";
Edit1->Text="ATE0\r";
ComPort1->WriteStr(Edit1->Text);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
Edit1->Text="AT+CGDCONT=1,\"IP\",\"CMNET\"\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
RichEdit1->Clear();
RichEdit2->Clear();
Edit1->Clear();
Edit2->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit1->Text="ATD*99***1#\r";
Edit2->Clear();
Edit4->Clear();
ComPort1->WriteStr(Edit1->Text);
BeingConnecting=true;
// IsConnecting=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComPort1RLSDChange(TObject *Sender, bool OnOff)
{
if (OnOff)
ShowMessage("CD is True");
else
ShowMessage("CD is false");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComPort1Ring(TObject *Sender)
{
ShowMessage("Ring is on call");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
Edit2->Clear();
if (Button6->Caption=="关闭来电显示")
{
Button6->Caption="打开来电显示";
Edit1->Text="AT+CLIP=0\r";
ComPort1->WriteStr(Edit1->Text);
}
else
{
Button6->Caption="关闭来电显示";
Edit1->Text="AT+CLIP=1\r";
ComPort1->WriteStr(Edit1->Text);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Edit1->Text="ATD"+Edit3->Text+";\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button11Click(TObject *Sender)
{
Edit1->Text="ATO\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button12Click(TObject *Sender)
{
Edit1->Text="ATH\r";
Edit2->Clear();
Edit4->Clear();
ComPort1->WriteStr(Edit1->Text);
IsConnecting=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button13Click(TObject *Sender)
{
unsigned char buffer[20];
strcpy(buffer,"1234567890abcdef");
UdpSocket1->RemoteHost=Edit4->Text;
UdpSocket1->RemotePort=Edit5->Text;
UdpSocket1->LocalHost=Edit6->Text;
UdpSocket1->LocalPort=Edit7->Text;
UdpSocket1->Active =true;
UdpSocket1->SendBuf((void *)buffer,16);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button14Click(TObject *Sender)
{
Edit1->Text="AT+CSCA=";
Edit1->Text=Edit1->Text+"\x22"+Edit8->Text+"\x22\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button15Click(TObject *Sender)
{
Edit1->Text="AT+CMGS=";
Edit1->Text=Edit1->Text+"\x22"+Edit3->Text+"\x22\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button17Click(TObject *Sender)
{
Edit1->Text="AT+CMGF=1\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button16Click(TObject *Sender)
{
Edit1->Text="AT+CMGR=1\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button18Click(TObject *Sender)
{
Edit1->Text="this is sms\0x1a";
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button19Click(TObject *Sender)
{
Edit1->Text="AT+CPBR=1,1\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button20Click(TObject *Sender)
{
Edit1->Text="AT+CPBS=\"ON\"\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button21Click(TObject *Sender)
{
Edit1->Text="AT+CRC=1\r";
Edit2->Clear();
ComPort1->WriteStr(Edit1->Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::UdpSocket1Receive(TObject *Sender, PChar Buf,
int &DataLen)
{
ShowMessage("udp received is ok");
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -