📄 unit_set_comm.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit_Set_Comm.h"
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm_Set_Comm *Form_Set_Comm;
//---------------------------------------------------------------------------
__fastcall TForm_Set_Comm::TForm_Set_Comm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::FormCreate(TObject *Sender)
{
//检测可用端口并添加至ComboBox_Comm_Port
char *CommNo;
String Temp;
HANDLE hcomm;
Image_Warn->Visible = true;
Label_Warn->Visible = true;
for(int i = 1;i < 16;i ++ ){
Temp = "COM" + IntToStr(i);
CommNo = Temp.c_str();
//************打开端口操作
hcomm = CreateFile(CommNo,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);
if(hcomm == INVALID_HANDLE_VALUE) continue;
ComboBox_Comm_Port->Items->Add(Temp);
Image_Warn->Visible = false;
Label_Warn->Visible = false;
CloseHandle(hcomm);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::Button_CancelClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::Button_OKClick(TObject *Sender)
{
//将设置值赋予通讯参数结构
Form_Main->COMM.Comm_No = ComboBox_Comm_Port->Text.SubString(4,1).ToInt();
Form_Main->COMM.Comm_BaudRate = ComboBox_Baudrate->Text.ToInt();
Form_Main->COMM.Comm_ByteSize = ComboBox_Data_Bit->Text.ToInt();
Form_Main->COMM.Comm_Parity = ComboBox_Check_Bit->Text.ToInt();
Form_Main->COMM.Comm_StopBits = ComboBox_Stop_Bit->Text.ToDouble();
//将设置写入初始化文件
TIniFile *NN;
NN = new TIniFile(Form_Main->str_EXEC_PATH+"\\DCW.ini");
NN->WriteInteger("通讯参数","端口",Form_Main->COMM.Comm_No);//写端口号
NN->WriteInteger("通讯参数","波特率",Form_Main->COMM.Comm_BaudRate);//写波特率
NN->WriteInteger("通讯参数","数据位",Form_Main->COMM.Comm_ByteSize);//写数据位数
NN->WriteInteger("通讯参数","校验位",Form_Main->COMM.Comm_Parity);//写校验位
NN->WriteInteger("通讯参数","停止位",Form_Main->COMM.Comm_StopBits);//写停止位
delete NN;
Form_Main->Edit_Data_Send->Text="";
Form_Main->Edit_Data_Get->Text="";
Form_Main->Label_Com_Hint->Caption="";
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::FormActivate(TObject *Sender)
{
//显示端口号
if(ComboBox_Comm_Port->Items->Count != 0)//初始为第一项
ComboBox_Comm_Port->ItemIndex = 0;
for(int i=0;i<ComboBox_Comm_Port->Items->Count;i++){//根据初始化文件设置显示项目
if(ComboBox_Comm_Port->Items->Strings[i].SubString(4,1).ToInt()
== Form_Main->COMM.Comm_No){
ComboBox_Comm_Port->ItemIndex = i;
break;
}
}
//显示波特率
ComboBox_Baudrate->ItemIndex = 0;
for(int i = 0;i < ComboBox_Baudrate->Items->Count;i++){
if(ComboBox_Baudrate->Items->Strings[i]==String(Form_Main->COMM.Comm_BaudRate)){
ComboBox_Baudrate->ItemIndex = i;
break;
}
}
//显示数据位数
ComboBox_Data_Bit->ItemIndex = 0;
for(int i = 0;i < ComboBox_Data_Bit->Items->Count;i++){
if(ComboBox_Data_Bit->Items->Strings[i]==String(Form_Main->COMM.Comm_ByteSize)){
ComboBox_Data_Bit->ItemIndex = i;
break;
}
}
//显示校验位
ComboBox_Check_Bit->ItemIndex = 0;
for(int i = 0;i < ComboBox_Check_Bit->Items->Count;i++){
if(ComboBox_Check_Bit->Items->Strings[i]==String(Form_Main->COMM.Comm_Parity)){
ComboBox_Check_Bit->ItemIndex = i;
break;
}
}
//显示停止位
ComboBox_Stop_Bit->ItemIndex = 0;
for(int i = 0;i < ComboBox_Stop_Bit->Items->Count;i++){
if(ComboBox_Stop_Bit->Items->Strings[i]==String(Form_Main->COMM.Comm_StopBits)){
ComboBox_Stop_Bit->ItemIndex = i;
break;
}
}
if(Form_Main->COMM.Linked){
GroupBox_Comm_Par->Enabled = false;
Button_OK->Enabled = false;
Button_Default->Enabled = false;
Label_Hint_Check->Visible = true;
Label_Hint_Stop->Visible = true;
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::Button_DefaultClick(TObject *Sender)
{
ComboBox_Comm_Port->ItemIndex = 0; //列表第一项
ComboBox_Baudrate->ItemIndex = 1; //波特率=9600
ComboBox_Data_Bit->ItemIndex = 1; //数据位=8
ComboBox_Check_Bit->ItemIndex = 2; //校验位=偶
ComboBox_Stop_Bit->ItemIndex = 0; //停止位=1
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::ComboBox_Check_BitEnter(TObject *Sender)
{
Label_Hint_Check->Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::ComboBox_Check_BitExit(TObject *Sender)
{
Label_Hint_Check->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::ComboBox_Stop_BitEnter(TObject *Sender)
{
Label_Hint_Stop->Visible = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set_Comm::ComboBox_Stop_BitExit(TObject *Sender)
{
Label_Hint_Stop->Visible = false;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -