📄 unit1.~cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "fcntl.h"
#include "io.h"
#include "string.h"
#include "alloc.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TRecvThread *ReadLine;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMCOMMNOTIFY(TMessage &Message)
{
COMSTAT CState ;
unsigned long ErrorF,NOBR,i;
unsigned char InBuf[1024];
if(!ClearCommError(hb,&ErrorF,&CState)) /*检测端口数据*/
{
PurgeComm(hb,PURGE_RXABORT|PURGE_RXCLEAR);
return;
}
if(CState.cbInQue>0)
{
memset(InBuf,'\0',CState.cbInQue);
if(!ReadFile(hb,InBuf,CState.cbInQue,&NOBR,&re)) /*接收数据*/
{
ErrorF=GetLastError();
if((ErrorF!=0)&&(ErrorF!=ERROR_IO_PENDING))
{
Receive=false;
ShowMessage("读串口数据出错");
return;
}
else
{
WaitForSingleObject(hb,INFINITE); /*等待操作完成*/
GetOverlappedResult(hb,&re,&NOBR,false);
}
}
if(NOBR>0) /*数据累加存放到缓冲区*/
{
re.Offset=re.Offset+NOBR;
if((hadlength+NOBR)>=(1024*k))
{
InputBuf=(unsigned char *)realloc(InputBuf,(k+1)*1024*sizeof(unsigned char));
k++;
}
for(i=0;i<NOBR;i++)
InputBuf[hadlength++]=InBuf[i];
}
if(hadlength>=12)
{
if(mlong==0)
mlong=StrToInt(StrPas(&InputBuf[1]));
if(hadlength>=mlong) /*输出数据*/
{
WaitForSingleObject(Form1->APost,INFINITE);
ResetEvent(Form1->APost);
PostMessage(Form1->Handle,WM_OVERCHECK,(unsigned int)(Form1->hb),0);
}
}
}
SetEvent(Form1->Post);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMOVERCHECK(TMessage &Message) /*输出数据*/
{
FILE *save;
InputBuf[hadlength]='\0';
if(InputBuf[0]=='0')
{
GetSystemTime(&SysTime);
Memo1->Lines->Add(IntToStr(SysTime.wHour)+":"+IntToStr(SysTime.wMinute)+":"+
IntToStr(SysTime.wSecond)+" 接收到"+IntToStr(hadlength-12)+"个字节数据");
Memo1->Lines->Add(" "+StrPas(&InputBuf[12]));
}
else
{
if(MessageDlg("对方发送文件"+StrPas(&InputBuf[12])+"过来,要保存吗?[大小:"+
IntToStr(hadlength-44)+"字节]",mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)==mrYes)
{
Form1->SaveDialog1->FileName=StrPas(&InputBuf[12]); /*保存文件*/
if(Form1->SaveDialog1->Execute())
{
if((save=fopen((Form1->SaveDialog1->FileName).c_str(),"wb"))==NULL)
{
ShowMessage("文件建立出错");return;}
fwrite(&InputBuf[44],hadlength-44,1,save); /*写文件*/
fclose(save);
}
GetSystemTime(&SysTime);
Memo1->Lines->Add(IntToStr(SysTime.wHour)+":"+IntToStr(SysTime.wMinute)+":"+
IntToStr(SysTime.wSecond)+" 已经保存了文件:"+StrPas(&InputBuf[12]));
}
else
{
GetSystemTime(&SysTime);
Memo1->Lines->Add(IntToStr(SysTime.wHour)+":"+IntToStr(SysTime.wMinute)+":"+
IntToStr(SysTime.wSecond)+" 已经拒绝了文件:"+StrPas(&InputBuf[12]));
}
}
free(InputBuf); /*释放缓冲区*/
InputBuf=(unsigned char *)malloc(1024*sizeof(unsigned char));
hadlength=0;mlong=0;
SetEvent(Form1->APost);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
k=0;
hadlength=0;
mlong=0;
WIND=Application->Handle;
InputBuf=(unsigned char *)malloc(1024*sizeof(unsigned char));
Form1->OpenDialog1->Title="请选择一个文本文件:";
Form1->OpenDialog1->InitialDir=GetCurrentDir();
Form1->OpenDialog1->Filter="All Files (*.*)|*.*|Text File(*.txt)|*.txt";
Form1->OpenDialog1->DefaultExt=String("TXT");
Form1->SaveDialog1->Title="请选择一个文本文件:";
Form1->SaveDialog1->InitialDir=GetCurrentDir();
Form1->SaveDialog1->Filter="All Files (*.*)|*.*|Text File(*.txt)|*.txt";
Form1->SaveDialog1->DefaultExt=String("TXT");
Form1->BitBtn2->Enabled=false;
Form1->BitBtn3->Enabled=false;
Form1->BitBtn5->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
hb=CreateFile((ComboBox4->Items->Strings[ComboBox4->ItemIndex]).c_str(),GENERIC_WRITE|GENERIC_READ,
0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0); /*打开串口*/
if(hb==INVALID_HANDLE_VALUE)
{
ShowMessage("打开"+ComboBox4->Items->Strings[ComboBox4->ItemIndex]+"端口错误");
return;
} /*打开出错,跳出该过程*/
SetupComm(hb,1024*2,1024*2); /*输入输出缓冲区大小位2K*/
GetCommState(hb,&dcb);
dcb.BaudRate=StrToInt(ComboBox5->Text);
dcb.ByteSize=StrToInt(ComboBox1->Items->Strings[ComboBox1->ItemIndex]);
dcb.StopBits=StrToFloat(ComboBox2->Items->Strings[ComboBox2->ItemIndex]);
dcb.Parity=ComboBox3->ItemIndex;
if(!SetCommState(hb,&dcb)) /*修改设备控制块DCB*/
{
ShowMessage("设置"+ComboBox4->Items->Strings[ComboBox4->ItemIndex]+"端口错误");
return;
} /*设置出错,跳出该过程*/
memset(&re,0,sizeof(re));
re.Offset=0;
re.OffsetHigh=0;
re.hEvent=CreateEvent(NULL,true,false,NULL); /*设置事件 */
if(re.hEvent==NULL){ CloseHandle(hb); return;}
Post=CreateEvent(NULL,true,true,NULL); /*设置Post事件*/
if(Post==NULL)
{
CloseHandle(hb);
CloseHandle(re.hEvent);
return;
}
APost=CreateEvent(NULL,true,true,NULL); /*设置APost事件*/
if(APost==NULL)
{
CloseHandle(hb);
CloseHandle(re.hEvent);
CloseHandle(APost);
return;
}
ReadLine=new TRecvThread(false);
EscapeCommFunction(hb,SETDTR);
StatusBar1->Panels->Items[0]->Text=ComboBox4->Items->Strings[ComboBox4->ItemIndex]+"端口已打开";
StatusBar1->Refresh();
Form1->BitBtn1->Enabled=false;
Form1->BitBtn2->Enabled=true;
Form1->BitBtn3->Enabled=true;
Form1->BitBtn5->Enabled=true;
ComboBox1->Enabled=false;
ComboBox2->Enabled=false;
ComboBox3->Enabled=false;
ComboBox4->Enabled=false;
ComboBox5->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Receive=false;
CloseHandle(re.hEvent);
CloseHandle(APost);
CloseHandle(Post);
CloseHandle(hb);
StatusBar1->Panels->Items[0]->Text=ComboBox4->Items->Strings[ComboBox4->ItemIndex]+"端口已关闭";
StatusBar1->Refresh();
Form1->BitBtn1->Enabled=true;
Form1->BitBtn2->Enabled=false;
Form1->BitBtn3->Enabled=false;
Form1->BitBtn5->Enabled=false;
ComboBox1->Enabled=true;
ComboBox2->Enabled=true;
ComboBox3->Enabled=true;
ComboBox4->Enabled=true;
ComboBox5->Enabled=true;
delete ReadLine;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
unsigned long wlength,rwlength=0,wtwr=0;
int Error;
unsigned char pDataWr[1024];
OVERLAPPED we;
wlength=Edit3->Text.Length();
if(wlength==0)
{
ShowMessage("发送缓冲区不能为空");
return;
}
wlength=wlength+12;
memset(pDataWr,'\0',12);
pDataWr[0]='0'; /*字符标志*/
strncpy(&pDataWr[1],IntToStr(wlength).c_str(),10);
strncpy(&pDataWr[12],(Edit3->Text).c_str(),(Edit3->Text).Length()); /*字符信息打包*/
memset(&we,'a',sizeof(we));
we.hEvent=CreateEvent(NULL,true,false,NULL); /*设置事件*/
SetCommMask(hb,EV_TXEMPTY);/*设置事件掩码,一直发送到最后一个字符*/
do
{
if(!WriteFile(hb,&pDataWr[wtwr],wlength,&rwlength,&we)) /*向端口写入信息*/
{
Error=GetLastError();
if(Error!=0)
if(Error==ERROR_IO_PENDING)
{
WaitForSingleObject(we.hEvent,INFINITE); /*读写同步*/
GetOverlappedResult(hb,&we,&rwlength,false);
}
else
{
StatusBar1->Panels->Items[1]->Text="发送数据失败";
ShowMessage("发送数据失败");return;
}
}
wlength = wlength-rwlength;
wtwr=rwlength;
}while(wlength>0); //发送完毕
GetSystemTime(&SysTime);
Memo1->Lines->Add(IntToStr(SysTime.wHour)+":"+IntToStr(SysTime.wMinute)+":"+IntToStr(SysTime.wSecond));
Memo1->Lines->Add(" 已发送"+IntToStr(wtwr-12)+"个字节数据:"+Edit3->Text);
StatusBar1->Panels->Items[1]->Text="字符发送完毕";
StatusBar1->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
if(Form1->OpenDialog1->Execute())
Form1->Edit4->Text=Form1->OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
unsigned long wlength,rwlength=0,wtwr=0;
int Error;
int handle;
OVERLAPPED fwe;
/*打开所选文件*/
handle=open((Form1->Edit4->Text).c_str(),O_RDONLY|O_BINARY);
if(handle>=0)
{
bufl=filelength(handle)+44; /*获取文件长度*/
buf=(unsigned char*)malloc(bufl*sizeof(unsigned char)); /*申请缓冲区*/
read(handle,&buf[44],bufl-44); /*读取文件*/
memset(buf,'\0',44);
buf[0]='1'; /*文件标志*/
strncpy(&buf[1],IntToStr(bufl).c_str(),10);
strncpy(&buf[12],(ExtractFileName(Edit4->Text)).c_str(),
(ExtractFileName(Edit4->Text)).Length()); /*文件数据打包*/
close(handle); /*关闭文件*/
}
else
{
StatusBar1->Panels->Items[2]->Text="该文件不存在";
return;
}
wlength=bufl;
memset(&fwe,'a',sizeof(fwe));
fwe.hEvent=CreateEvent(NULL,true,false,NULL); /*设置事件*/
SetCommMask(hb,EV_TXEMPTY); /*设置事件掩码*/
StatusBar1->Panels->Items[2]->Text="正在发送文件";
StatusBar1->Refresh();
do
{
if(!WriteFile(hb,&buf[wtwr],wlength,&rwlength,&fwe))
{
Error=GetLastError();
if(Error!=0)
if(Error==ERROR_IO_PENDING)
{
WaitForSingleObject(fwe.hEvent,INFINITE);
GetOverlappedResult(hb,&fwe,&rwlength,false);
}
else
{
StatusBar1->Panels->Items[2]->Text="文件发送失败";
ShowMessage("文件发送失败");return;}
}
wlength=wlength - rwlength;
wtwr=rwlength;
}while(wlength>0);
free(buf); /*释放缓冲区*/
GetSystemTime(&SysTime);
Memo1->Lines->Add(IntToStr(SysTime.wHour)+":"+IntToStr(SysTime.wMinute)+":"+IntToStr(SysTime.wSecond));
Memo1->Lines->Add(" 已发送文件:"+Form1->Edit4->Text);
StatusBar1->Panels->Items[2]->Text="文件发送完毕,共"+IntToStr(rwlength-44)+"字节";
StatusBar1->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
Form1->Edit3->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn7Click(TObject *Sender)
{
Form1->Memo1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn8Click(TObject *Sender)
{
Form1->Edit4->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
Close( );
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -