📄 unit1.~cpp
字号:
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::ButtonSendDataClick(TObject *Sender)
{
AnsiString SendData = EditSendData->Text;
WideString ChineseCode = SendData;
if(!CheckBoxHexSend->Checked)
{
if(CheckBoxReturnSend->Checked)
{
char D[3]= {13,10,00}; //0d 0a's ascii code;
SendData=SendData+D;//
}
CommUart->Write(SendData.c_str(),SendData.Length());
//string send
}
else
{
/* TODO -oXukaiming : Hex发送 */
//#ifdef HexSEND
unsigned int i=0;
unsigned int Len=0;
unsigned int tempLength =(EditSendData->Text.Length())/3+1;
unsigned char* temp = new char[sizeof(char)*(tempLength+3)]; // 预留"\0"和回车符的 空间
//String temp("",sizeof(char)*(tempLength+3));
String HEX;
for(i=0;i<sizeof(char)*(tempLength+3);i++)
{
temp[i]=0; //清空temp数组的内容
}
for(i=0;i<tempLength;i++)
{
HEX = EditSendData->Text.SubString(i*3+1,2);
HEX = "0x"+HEX;
char* endptr;
long lnumber = strtol(HEX.c_str(), &endptr, 16);
temp[i]=(char)lnumber;
// 将字符串转换为hex code
}
Len=i;
if(CheckBoxReturnSend->Checked)
{
temp[i++]=0x0d;
temp[i++]=0x0a;
Len+=2;
}
CommUart->Write(temp,Len);
delete []temp;
temp = NULL;
//#ifdef HEXSEND//hex十六进制发送
//#endif
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CommUartComStatEvent(TObject *Sender,
DWORD Status, COMSTAT ComStat)
{
if(ComStat.fCtsHold)
{
;
}
if(ComStat.fDsrHold)
{
;
}
if(ComStat.fRlsdHold)
{
;
}
if(ComStat.fXoffHold)
{
;
}
if(ComStat.fXoffSent)
{
;
}
if(ComStat.fEof)
{
;
}
if(ComStat.fTxim)
{
;
}
if(ComStat.fReserved)
{
;
}
if(ComStat.cbInQue)
{
unsigned int Count = CommUart->InQueCount();
//Buffer="0\0\0\0";
AnsiString TempBuffer=AnsiString::StringOfChar('\0',Count+1);
Buffer = TempBuffer; //清除脏值
if(Count!=0)
{
CommUart->Read(Buffer.c_str(),Count);
if(ButtonDisplay->Caption!="开始显示")
{
if(!CheckBoxHexReceive->Checked)
{
///* TODO -oXukaiming : 添加一个临时缓冲区,满了之后才让memo换行 */
HexBuffer.Insert(Buffer.c_str(),(HexBuffer.Length()+1)); //
RichEditReceive->Lines->Clear();
try
{
RichEditReceive->Lines->Add(HexBuffer);
}
catch(Exception &exception)
{
//Application->ShowException(&exception);
}
/*
if(RichEditReceive->Lines->Count>8)
{
SendMessage(
RichEditReceive->Handle,
WM_VSCROLL,
SB_PAGEDOWN,
NULL
);
}
*/
}
else
{
unsigned int i;
unsigned char* temp = Buffer.c_str();
for(i=0;i<Count;i++) //count 才是真正的字符串长度
{
HexBuffer.Insert(IntToHex(temp[i],2),(HexBuffer.Length()+1)); //于尾部加入16进制
HexBuffer.Insert(" ",(HexBuffer.Length()+1)); //尾部追加空格
RichEditReceive->Lines->Clear();
try
{
RichEditReceive->Lines->Add(HexBuffer.c_str());
}
catch(Exception &exception)
{
//Application->ShowException(&exception);
}
}
}
}
if(RichEditReceive->Lines->Count>10)
{
static WriteCount = 0;
AnsiString SaveFileName = ChangeFileExt(Application->ExeName,"")+WriteCount+".rtf";
RichEditReceive->Lines->SaveToFile(SaveFileName);
WriteCount++;
HexBuffer = "\0";
}
}
;
}
if(ComStat.cbOutQue)
{
;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::TimerSendTimer(TObject *Sender)
{
CommUart->Write(EditSendData->Text.c_str(),EditSendData->Text.Length());
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::EditAutoSendTimeClick(TObject *Sender)
{
EditAutoSendTime->Text = '\0';
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::EditAutoSendTimeKeyPress(TObject *Sender,
char &Key)
{
switch(Key)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
default:
Key = 0;
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::ButtonDisplayClick(TObject *Sender)
{
if(ButtonDisplay->Caption=="停止显示")
{
ButtonDisplay->Caption="开始显示";
//打开接收功能
}
else
{
ButtonDisplay->Caption="停止显示" ;
/* TODO -oXukaiming : 最好改为屏蔽接收 */
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::ButtonSetClick(TObject *Sender)
{
//Application->CreateForm(__classid(TFormSet), &FormSet);
TFormSet *FormSet = new TFormSet(NULL);
FormSet->ShowModal();
delete FormSet;
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CheckBoxReturnSendClick(TObject *Sender)
{
// EditSendData->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CheckBoxTopClick(TObject *Sender)
{
if(CheckBoxTop->Checked)
{
FormPort->FormStyle = fsStayOnTop;
}
else
{
FormPort->FormStyle = fsNormal;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if(MessageBox(NULL,"确实要退出吗?","系统提示",MB_OKCANCEL+MB_ICONQUESTION+MB_APPLMODAL+MB_TOPMOST+MB_TASKMODAL )!= IDOK){
CanClose = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CheckBoxGlassClick(TObject *Sender)
{
OSVERSIONINFO osversioninfo;
osversioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osversioninfo);
if((CheckBoxGlass->Checked)&&(osversioninfo.dwMajorVersion>=5))
{
AlphaBlend = True;
TrackBarAlpha->Enabled = True;
TrackBarAlpha->Position = AlphaBlendValue;
}
else
{
AlphaBlend = False;
TrackBarAlpha->Enabled = False;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::TrackBarAlphaChange(TObject *Sender)
{
AlphaBlendValue = TrackBarAlpha->Position;
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CheckBoxDTRClick(TObject *Sender)
{
if(CheckBoxDTR->Checked)
{
CommUart->SetDTRState(true);
}
else
{
CommUart->SetDTRState(false);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CheckBoxRTSClick(TObject *Sender)
{
if(CheckBoxRTS->Checked)
{
CommUart->SetRTSState(true);
}
else
{
CommUart->SetRTSState(false);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::ComboBoxRTSControlChange(TObject *Sender)
{
CommUart->RTSControl = (TRTSControl)(SendMessage(ComboBoxRTSControl->Handle,
CB_FINDSTRING,0,
long(ComboBoxRTSControl->Text.c_str())));;
if(ComboBoxRTSControl->Text != "Disable")
{
CheckBoxRTS->Enabled = false;
}
else
{
CheckBoxRTS->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::EditSendDataDblClick(TObject *Sender)
{
EditSendData->SelectAll();
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CommUartRing(TObject *Sender)
{
if(CommUart->RING)
{
ShapeRI->Brush->Color = clRed;
}
else
{
ShapeRI->Brush->Color = clRed;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::ComboBoxDTRControlChange(TObject *Sender)
{
CommUart->DTRControl= (TDTRControl)(SendMessage(ComboBoxDTRControl->Handle,
CB_FINDSTRING,0,
long(ComboBoxDTRControl->Text.c_str())));;
if(ComboBoxDTRControl->Text != "Disable")
{
CheckBoxDTR->Enabled = false;
}
else
{
CheckBoxDTR->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CommUartCts(TObject *Sender)
{
if(CommUart->CTS)
{
shpCTS->Brush->Color = clRed;
}
else
{
shpCTS->Brush->Color = clWhite;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormPort::CommUartDsr(TObject *Sender)
{
if(CommUart->DSR)
{
shpDSR->Brush->Color = clRed;
}
else
{
shpDSR->Brush->Color = clWhite;
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -