uset.cpp

来自「cb下的串口通讯、短信收发(适于初学者)」· C++ 代码 · 共 89 行

CPP
89
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Uset.h"
#include "UMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm_Set *Form_Set;
extern HANDLE Hcom;
//---------------------------------------------------------------------------
__fastcall TForm_Set::TForm_Set(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set::Button1Click(TObject *Sender)
{
 String ComNum,Smsc;
 int baudrate,stopbit,ByteSize,Parity;
 ComNum=Trim(ComboBox1->Text);
 Parity=ComboBox3->ItemIndex;
 if(Parity=-1)
 Parity=ComboBox3->Items->IndexOf(ComboBox3->Text);
 baudrate=StrToInt(Trim(ComboBox2->Text));
 stopbit=StrToInt(Trim(Edit2->Text));
 ByteSize=StrToInt(Trim(Edit1->Text));
 Smsc=Trim(Edit3->Text);
 TIniFile *tempinifile= new TIniFile(ExtractFilePath(ParamStr(0))+"smsc.ini");
 tempinifile->WriteString("SetCom","Comm",ComNum);
 tempinifile->WriteInteger("SetCom","Parity",Parity);
 tempinifile->WriteString("Smsc","SMC",Smsc);
 tempinifile->WriteInteger("SetCom","BaudRate",baudrate);
 tempinifile->WriteInteger("SetCom","ByteSize",ByteSize);
 tempinifile->WriteInteger("SetCom","StopBit",stopbit);
 tempinifile->Free();
 DCB lpDcb;

 if (Hcom)
    CloseHandle(Hcom);
 if (ComNum=="COM1")
  Hcom=CreateFile("COM1",
                  GENERIC_READ | GENERIC_WRITE,
                  0,
                  NULL,
                  OPEN_EXISTING,
                  0,
                  NULL);
 else if (ComNum=="COM2")
  Hcom=CreateFile("COM2",
                  GENERIC_READ | GENERIC_WRITE,
                  0,
                  NULL,
                  OPEN_EXISTING,
                  0,
                  NULL);

 //hCom = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,0,
	       //	NULL,OPEN_EXISTING,0,NULL);
if (Hcom==INVALID_HANDLE_VALUE)
   {
    MessageBox(Handle,"串口打开错,请检查!","系统提示",0);
    return;
   }
 SetupComm( Hcom,20480,20480 );
    COMMTIMEOUTS gCommTimeOuts ;
    gCommTimeOuts.ReadIntervalTimeout = 50 ;
    gCommTimeOuts.ReadTotalTimeoutMultiplier = 100 ;
    gCommTimeOuts.ReadTotalTimeoutConstant = 100 ;
    gCommTimeOuts.WriteTotalTimeoutMultiplier = 500 ;
    gCommTimeOuts.WriteTotalTimeoutConstant = 500 ;
    SetCommTimeouts( Hcom,&gCommTimeOuts ) ;
GetCommState(Hcom,&lpDcb);
lpDcb.BaudRate=baudrate;
lpDcb.Parity=Parity;
lpDcb.ByteSize=ByteSize;
lpDcb.StopBits=stopbit;
SetCommState(Hcom,&lpDcb);
Form_Set->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Set::Button2Click(TObject *Sender)
{
 Form_Set->Close();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?