📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "LMDCustomComponent"
#pragma link "LMDTrayIcon"
#pragma link "LMDWndProcComponent"
#pragma link "YbCommDevice"
#pragma link "LMDBaseControl"
#pragma link "LMDBaseGraphicControl"
#pragma link "LMDBaseLabel"
#pragma link "LMDButton"
#pragma link "LMDComboBox"
#pragma link "LMDControl"
#pragma link "LMDCustomBevelPanel"
#pragma link "LMDCustomButton"
#pragma link "LMDCustomComboBox"
#pragma link "LMDCustomControl"
#pragma link "LMDCustomGroupBox"
#pragma link "LMDCustomImageListBox"
#pragma link "LMDCustomLabel"
#pragma link "LMDCustomListBox"
#pragma link "LMDCustomPanel"
#pragma link "LMDCustomParentPanel"
#pragma link "LMDGroupBox"
#pragma link "LMDImageListBox"
#pragma link "LMDLabel"
#pragma link "LMDFormStyler"
#pragma resource "*.dfm"
TSerialIP_From *SerialIP_From;
//---------------------------------------------------------------------------
__fastcall TSerialIP_From::TSerialIP_From(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::close1Click(TObject *Sender)
{
SerialIP_From->Show();
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::close2Click(TObject *Sender)
{
SerialIP_From->Close();
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::FormCreate(TObject *Sender)
{
TCommSerialPortInfo spi;
LMDImageListBox1->Clear();
for(int i=0; i<spi.PortList->Count; i++)
LMDImageListBox1->Items->Add(spi.PortList->Strings[i]);
InitalCOMParams();
YbCommDevice1->PackageSize = 4096; //最大可发送 4096 个字节的数据包
YbCommDevice1->PackageType = cptFrameTimeout; //用判断超时的方法接收数据包
YbCommDevice1->UsePackage = true; //启动数据包 (可以随时启动和停止, 与 Active 属性无关)
LMDButton4->Enabled = false;
Stop1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::LMDButton1Click(TObject *Sender)
{
int index,i;
AnsiString pcomname;
index = LMDImageListBox1->ItemIndex;
if (index>-1)
{
pcomname = LMDImageListBox1->Items->Strings[index];
/*
for(i=0;i<LMDImageListBox2->Count;i++)
{
if(LMDImageListBox2->Items->Strings[i]==pcomname) break;
}
if(i == LMDImageListBox2->Count)
{
LMDImageListBox2->Items->Add(pcomname);
LMDImageListBox1->DeleteSelected();
}
*/
LMDImageListBox2->Items->Add(pcomname);
LMDImageListBox1->DeleteSelected();
}
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::LMDButton2Click(TObject *Sender)
{
int index,i;
AnsiString pcomname;
index = LMDImageListBox2->ItemIndex;
if (index>-1)
{
pcomname = LMDImageListBox2->Items->Strings[index];
/*
for(i=0;i<LMDImageListBox1->Count;i++)
{
if(LMDImageListBox1->Items->Strings[i]==pcomname) break;
}
if(i == LMDImageListBox1->Count)
{
LMDImageListBox1->Items->Add(pcomname);
LMDImageListBox2->DeleteSelected();
}
*/
LMDImageListBox1->Items->Add(pcomname);
LMDImageListBox2->DeleteSelected();
}
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::InitalCOMParams(void)
{
LMDComboBox1->ItemIndex = 6;
LMDComboBox2->ItemIndex = 0;
LMDComboBox3->ItemIndex = 3;
LMDComboBox4->ItemIndex = 0;
LMDComboBox5->ItemIndex = 0;
LMDComboBox6->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::SetCOMParams(void)
{
int NewPortNo, NewByteSize, NewAutoAAns;
long NewInBufSize, NewOutBufSize;
TYbCommDevice::TBaudRate NewBaud;
TYbCommDevice::TParity NewParity;
TYbCommDevice::TStopBits NewStopBits;
TYbCommDevice::TFlowControl NewFlowControl;
AnsiString s;
//选择端口
if(LMDImageListBox2->Count > 0)
{
if(LMDImageListBox2->ItemIndex > -1)
{
NewPortNo = TCommSerialPortInfo::PortNo(LMDImageListBox2->Items->Strings[LMDImageListBox2->ItemIndex]);
}
else
{
NewPortNo = TCommSerialPortInfo::PortNo(LMDImageListBox2->Items->Strings[0]);
}
}
else
{
LMDImageListBox2->SetFocus(); //"必须选择端口"
Application->MessageBox("必须选择端口", Caption.c_str(), MB_OK|MB_ICONSTOP);
return;
}
//设置波特率
switch(LMDComboBox1->ItemIndex)
{
case 0: NewBaud = TYbCommDevice::br110 ; break;
case 1: NewBaud = TYbCommDevice::br300 ; break;
case 2: NewBaud = TYbCommDevice::br600 ; break;
case 3: NewBaud = TYbCommDevice::br1200 ; break;
case 4: NewBaud = TYbCommDevice::br2400 ; break;
case 5: NewBaud = TYbCommDevice::br4800 ; break;
case 6: NewBaud = TYbCommDevice::br9600 ; break;
case 7: NewBaud = TYbCommDevice::br14400 ; break;
case 8: NewBaud = TYbCommDevice::br19200 ; break;
case 9: NewBaud = TYbCommDevice::br38400 ; break;
case 10: NewBaud = TYbCommDevice::br56000 ; break;
case 11: NewBaud = TYbCommDevice::br57600 ; break;
case 12: NewBaud = TYbCommDevice::br115200; break;
case 13: NewBaud = TYbCommDevice::br128000; break;
case 14: NewBaud = TYbCommDevice::br256000; break;
default: LMDComboBox1->SetFocus(); //"必须输入/选择波特率"
Application->MessageBox("必须输入/选择波特率", Caption.c_str(), MB_OK|MB_ICONSTOP);
return;
}
//数据位数
if(LMDComboBox3->ItemIndex<0)
{
LMDComboBox3->SetFocus(); //"必须选择数据位"
Application->MessageBox("必须选择数据位", Caption.c_str(), MB_OK|MB_ICONSTOP);
return;
}
NewByteSize = LMDComboBox3->ItemIndex+5;
//校验位
switch(LMDComboBox2->ItemIndex)
{
case 0: NewParity = TYbCommDevice::ptNoParity ; break;
case 1: NewParity = TYbCommDevice::ptOddParity ; break;
case 2: NewParity = TYbCommDevice::ptEvenParity ; break;
case 3: NewParity = TYbCommDevice::ptMarkParity ; break;
case 4: NewParity = TYbCommDevice::ptSpaceParity; break;
default: LMDComboBox2->SetFocus(); //"必须选择奇偶校验"
Application->MessageBox("必须选择奇偶校验", Caption.c_str(), MB_OK|MB_ICONSTOP);
return;
}
//停止位
switch(LMDComboBox4->ItemIndex)
{
case 0: NewStopBits = TYbCommDevice::sbOneStopBit ; break;
case 1: NewStopBits = TYbCommDevice::sbOne_5_StopBits; break;
case 2: NewStopBits = TYbCommDevice::sbTwoStopBit ; break;
default: LMDComboBox4->SetFocus(); //"必须选择停止位"
Application->MessageBox("必须选择停止位", Caption.c_str(), MB_OK|MB_ICONSTOP);
return;
}
//数据流控
switch(LMDComboBox5->ItemIndex)
{
case 0: NewFlowControl = TYbCommDevice::fcNone ; break;
case 1: NewFlowControl = TYbCommDevice::fcRtsCts ; break;
case 2: NewFlowControl = TYbCommDevice::fcXonXoff ; break;
case 3: NewFlowControl = TYbCommDevice::fcRtsCtsXonXoff; break;
default: LMDComboBox5->SetFocus(); //"必须选择数据流控制"
Application->MessageBox("必须选择数据流控制", Caption.c_str(), MB_OK|MB_ICONSTOP);
return;
}
//设置参数
try
{
YbCommDevice1->PortNo = NewPortNo;
YbCommDevice1->Baud = NewBaud;
YbCommDevice1->Parity = NewParity;
YbCommDevice1->ByteSize = NewByteSize;
YbCommDevice1->StopBits = NewStopBits;
YbCommDevice1->FlowControl = NewFlowControl;
YbCommDevice1->AutoAnswer = 0;
YbCommDevice1->InBufSize = 8192;
YbCommDevice1->OutBufSize = 8192;
ServerSocket1->Port = StrToIntDef(LMDComboBox6->Text,2000);
}
catch(Exception &e)
{
LMDComboBox1->SetFocus();
MessageBox(Handle, "设置参数错误", Caption.c_str(), MB_OK|MB_ICONSTOP);
}
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::LMDButton3Click(TObject *Sender)
{
try
{
SetCOMParams();
YbCommDevice1->Active = true;
AnsiString str1="打开串口COM"+IntToStr(YbCommDevice1->PortNo);
StatusBar1->Panels->Items[1]->Text=str1;
ServerSocket1->Port = StrToIntDef(LMDComboBox6->Text,10000);
ServerSocket1->Active = true;
str1="正在监听...";
StatusBar1->Panels->Items[2]->Text=str1;
EnableSetCOMParams();
}
catch(Exception &e)
{
MessageBox(Handle, "服务打开失败", Caption.c_str(), MB_OK|MB_ICONSTOP);
}
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::LMDButton4Click(TObject *Sender)
{
try
{
YbCommDevice1->Active = false;
AnsiString str1="关闭串口COM"+IntToStr(YbCommDevice1->PortNo);
StatusBar1->Panels->Items[1]->Text=str1;
ServerSocket1->Close();
str1="关闭监听...";
StatusBar1->Panels->Items[2]->Text=str1;
EbleSetCOMParams();
}
catch(Exception &e)
{
MessageBox(Handle, "服务关闭失败", Caption.c_str(), MB_OK|MB_ICONSTOP);
}
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString str1="连接到:"+Socket->RemoteAddress;
StatusBar1->Panels->Items[2]->Text=str1;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
int i;
AnsiString str1;
i = Socket->ReceiveBuf(WBuf,BufSize);
if (i>0)
{
YbCommDevice1->WritePackage(WBuf,i);
FIsTransmit = true;
str1 = StrPas(WBuf);
StatusBar1->Panels->Items[1]->Text=str1;
}
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::Timer1Timer(TObject *Sender)
{
int num,i;
AnsiString str1;
num = YbCommDevice1->ReadPackage(RBuf,BufSize);
if(num>0)
{
str1 = StrPas(RBuf);
StatusBar1->Panels->Items[2]->Text=str1;
for(i=0;i<ServerSocket1->Socket->ActiveConnections;i++)
ServerSocket1->Socket->Connections[i]->SendBuf(RBuf,num);
}
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::ExitE1Click(TObject *Sender)
{
SerialIP_From->Close();
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::HideH1Click(TObject *Sender)
{
SerialIP_From->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::Start1Click(TObject *Sender)
{
try
{
SetCOMParams();
YbCommDevice1->Active = true;
AnsiString str1="打开串口COM"+IntToStr(YbCommDevice1->PortNo);
StatusBar1->Panels->Items[1]->Text=str1;
ServerSocket1->Port = StrToIntDef(LMDComboBox6->Text,10000);
ServerSocket1->Active = true;
str1="正在监听...";
StatusBar1->Panels->Items[2]->Text=str1;
EnableSetCOMParams();
}
catch(Exception &e)
{
MessageBox(Handle, "服务打开失败", Caption.c_str(), MB_OK|MB_ICONSTOP);
}
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::Stop1Click(TObject *Sender)
{
try
{
YbCommDevice1->Active = false;
AnsiString str1="关闭串口COM"+IntToStr(YbCommDevice1->PortNo);
StatusBar1->Panels->Items[1]->Text=str1;
ServerSocket1->Close();
str1="关闭监听...";
StatusBar1->Panels->Items[2]->Text=str1;
EbleSetCOMParams();
}
catch(Exception &e)
{
MessageBox(Handle, "服务关闭失败", Caption.c_str(), MB_OK|MB_ICONSTOP);
}
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::EnableSetCOMParams(void)
{
LMDComboBox1->Enabled = false;
LMDComboBox2->Enabled = false;
LMDComboBox3->Enabled = false;
LMDComboBox4->Enabled = false;
LMDComboBox5->Enabled = false;
LMDComboBox6->Enabled = false;
LMDButton1->Enabled = false;
LMDButton2->Enabled = false;
LMDButton3->Enabled = false;
Start1->Enabled = false;
Stop1->Enabled = true;
LMDButton4->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TSerialIP_From::EbleSetCOMParams(void)
{
LMDComboBox1->Enabled = true;
LMDComboBox2->Enabled = true;
LMDComboBox3->Enabled = true;
LMDComboBox4->Enabled = true;
LMDComboBox5->Enabled = true;
LMDComboBox6->Enabled = true;
LMDButton1->Enabled = true;
LMDButton2->Enabled = true;
LMDButton3->Enabled = true;
Start1->Enabled = true;
Stop1->Enabled = false;
LMDButton4->Enabled = false;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -