⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~cpp

📁 uart串口通信的控件和demo,用API 编写
💻 ~CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "UnitSet.h"
#include "assert.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//#pragma link "COMM"
#pragma link "CSPIN"
#pragma resource "*.dfm"
TFormPort *FormPort;

//---------------------------------------------------------------------------
__fastcall TFormPort::TFormPort(TComponent* Owner)
        : TForm(Owner)
{


}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
/*
 __fastcall TFormPort::FormTrans(TComponent* Owner):TForm(Owner)
{
        m_nAlpha = 255;
        //设置WINDOWS2000 透明窗口参数WS_EX_LAYERED。
        SetWindowLong(Handle,GWL_EXSTYLE)
        GetWindowLong(Handle,GWL_EXSTYLE)|WS_EX_LAYERED);
        SetWindowLong(TrackBarAlpha->Handle,GWL_EXSTYLE,
        GetWindowLong(Handle,GWL_EXSTYLE)|WS_EX_LAYERED);
        //设置为不透明。
        SetAlpha(m_nAlpha);
}
*/

//---------------------------------------------------------------------------
void __fastcall TFormPort::CreateParams(TCreateParams &Params)
{
        DWORD minorversion;
        OSVERSIONINFO info;
        /*
        info.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); //init
        GetVersionEx(&info); 
        minorversion = info.dwMinorVersion;
        if(info.dwPlatformId ==VER_PLATFORM_WIN32_NT)
        {
                if(minorversion >= 5) //win2K以上版本;
                {

                        */
                        TCustomForm::CreateParams(Params);
                       // Params.ExStyle = Params.ExStyle|WS_EX_TRANSPARENT;
                       /*
                }

        }
        */
;
}
//---------------------------------------------------------------------------



void __fastcall TFormPort::ComboBoxBaudrateChange(TObject *Sender)
{
        long int ComBaudRate;
        if(ComboBoxBaudrate->Text=="Custom")
        {

                CheckBoxUserBaud->Checked = True;
                //TODO -oxukaiming : 自定义波特率
        }
        else
        {
                CheckBoxUserBaud->Checked = False;

                CommUart->BaudRate =(TBaudRate)(SendMessage(ComboBoxBaudrate->Handle,
                                                CB_FINDSTRING,0,
                                                long(ComboBoxBaudrate->Text.c_str())));
        //发出索引消息,得出字符串载列表中的位置;
        
        }

}
//---------------------------------------------------------------------------

void __fastcall TFormPort::CheckBoxUserBaudClick(TObject *Sender)
{
        if(CheckBoxUserBaud->Checked)
        {
                ComboBoxBaudrate->ItemIndex = 15; //display custom;

        }
        else
        {
                ComboBoxBaudrate->ItemIndex = 6 ; //9600
                
        }

}
//---------------------------------------------------------------------------







void __fastcall TFormPort::BitBtnOpenClick(TObject *Sender)
{
/*
        PPropInfo PropInfoComPort,PropInfoBaudRate,PropInfoDataBits,PropInfoStopBits,PropInfoParity ;
        //PropInfoComPort  = GetPropInfo((TTypeInfo*)CommUart->ClassInfo(),"DeviceName");
        PropInfoBaudRate = GetPropInfo((TTypeInfo*)CommUart->ClassInfo(),"BaudRate");
        PropInfoDataBits = GetPropInfo((TTypeInfo*)CommUart->ClassInfo(),"DataBits");
        PropInfoStopBits = GetPropInfo((TTypeInfo*)CommUart->ClassInfo(),"StopBits");
        PropInfoParity   = GetPropInfo((TTypeInfo*)CommUart->ClassInfo(),"Parity");
        ShowMessage(
                        "ComPort = "+(CommUart->DeviceName)+"\n"
                        "BaudRate = "+GetEnumName(*PropInfoBaudRate->PropType,(int)(CommUart->BaudRate))+"\n"
                        "DataBits="+GetEnumName(*PropInfoDataBits->PropType,(int)(CommUart->DataBits))+"\n"+
                        "StopBits = "+GetEnumName(*PropInfoStopBits->PropType,(int)(CommUart->StopBits))+"\n"+
                        "Parity = "+GetEnumName(*PropInfoParity->PropType,(int)(CommUart->Parity))+"\n");

*/
          if (!CommUart->Enabled())
          {

               
                try
                 {

                        CommUart->Open();
                 }
                 catch (Exception &exception)
                 {
                 
                        Application->ShowException(&exception);
                                
                 }
                 if (CommUart->Enabled())
                 {
                         BlinkTime = TimerBlink->Interval;
                         ComboBoxPort->Enabled = False;
                         ComboBoxBaudrate->Enabled = False;
                         ComboBoxDataBits->Enabled = False;
                         ComboBoxStopBits->Enabled = False;
                         ComboBoxParity->Enabled = False;
                         CheckBoxUserBaud->Enabled = False;
                         TimerBlink->Enabled = True;
                         GroupBoxOut->Enabled = True;
                         BitBtnOpen->Caption = "关闭串口";
                         EditBaud->Enabled = False;
                         GroupBoxOut->Font->Color = clBlack;

                  }
                  else
                  {
                        MessageBox(NULL,"端口已被打开或占用","系统提示",MB_OK+MB_ICONWARNING+MB_APPLMODAL+MB_TOPMOST+MB_TASKMODAL);


                  }
          }
          else
          {
               /*
                ShowMessage(
                        "ComPort = "+(CommUart->DeviceName)+"\n"
                        "BaudRate = "+GetEnumName(*PropInfoBaudRate->PropType,(int)(CommUart->BaudRate))+"\n"
                        "DataBits="+GetEnumName(*PropInfoDataBits->PropType,(int)(CommUart->DataBits))+"\n"+
                        "StopBits = "+GetEnumName(*PropInfoStopBits->PropType,(int)(CommUart->StopBits))+"\n"+
                        "Parity = "+GetEnumName(*PropInfoParity->PropType,(int)(CommUart->Parity))+"\n");
                */

                ComboBoxPort->Enabled = True;
                ComboBoxBaudrate->Enabled = True;
                ComboBoxDataBits->Enabled = True;
                ComboBoxStopBits->Enabled = True;
                ComboBoxParity->Enabled = True;
                CheckBoxUserBaud->Enabled = True;
                EditBaud->Enabled = True;
                TimerBlink->Enabled = False;
                ImagePortState->Picture = ImageButton2->Picture;
                GroupBoxOut->Enabled = False;
                BitBtnOpen->Caption = "打开串口";
                TimerBlink->Interval = BlinkTime;
                CheckBoxReturnSend->Checked = False;
                CheckBoxReturnSend->Checked = False;
                GroupBoxOut->Font->Color = clGray;
                CommUart->Close();

          }


}
//---------------------------------------------------------------------------



void __fastcall TFormPort::ButtonClearClick(TObject *Sender)
{

        HexBuffer="\0";
        try
        {
                RichEditReceive->Clear();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }

        
        //HexBuffer.SetLength(0);   //00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1F 20 21 22 23 24 25 26 27 28 29 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 5a 5b 5c 5d 5e 5f
        //SetLength会出现无法正常接受的bug
}
//---------------------------------------------------------------------------

void __fastcall TFormPort::EditBaudKeyPress(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':
        case  9 :           //删除
                break;
        default:
                Key = 0;
                break;
        }

}
//---------------------------------------------------------------------------






void __fastcall TFormPort::EditSendDataKeyPress(TObject *Sender, char &Key)
{
        if(CheckBoxHexSend->Checked)
        {
                switch(Key)
                {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                case ' ':
                case 8  :              //删除
                case 9  :
                        break;
                default:
                        Key = 0;
                        break;
                }
                if((EditSendData->Text.Length()+1)%3==0)
                {
                        //Key = ' '; 
                }
               
        }
}
//---------------------------------------------------------------------------



void __fastcall TFormPort::TimerBlinkTimer(TObject *Sender)
{
        static int x=2;
        if(x==2)
        {
                ImagePortState->Picture = ImageButton3->Picture;
                x=3;
        }
        else
        {
                ImagePortState->Picture = ImageButton1->Picture;
                x=2;
        }



}
//---------------------------------------------------------------------------


void __fastcall TFormPort::FormCreate(TObject *Sender)
{
/*
        SYSTEM_INFO systeminfo;
        MEMORYSTATUS memory;
        GetSystemInfo(&systeminfo);
        memory.dwLength =sizeof(memory); //初始化
        memory.dwLength =sizeof(memory); //初始化
        GlobalMemoryStatus(&memory);
        RichEditReceive->Lines->Add("您的CPU类型是:"+String( systeminfo.dwProcessorType ));
        RichEditReceive->Lines->Add("您的物理内存是(Mb):"+String(int(memory.dwTotalPhys /1024/1024)));
        RichEditReceive->Lines->Add("其中可用内存是(Mb):"+String(int(memory.dwAvailPhys)/1024/1024));
*/
        int COMCount = 1;
        ImagePortState->Picture = ImageButton2->Picture;
        BlinkTime = 1000;
        AnsiString Buffer = AnsiString("\0");
        AnsiString HexBuffer = AnsiString("\0");
        HexBuffer.SetLength(0);

        /*
        for(COMCount = 5;COMCount<256;COMCount++)
        {
                //CommUart->DeviceName="Com"+IntToStr(COMCount);
                //if(1)                   //Test Comm is enable;
                {
                        ComboBoxPort->Items->Add("Com"+IntToStr(COMCount));
                }
        }
        */

}
//---------------------------------------------------------------------------

void __fastcall TFormPort::ComboBoxPortChange(TObject *Sender)
{
        CommUart->DeviceName = ComboBoxPort->Text;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

void __fastcall TFormPort::ButtonAutoSendClick(TObject *Sender)
{
        if(ButtonAutoSend->Caption=="自动发送")
        {
                ButtonAutoSend->Caption="停止发送";
                ButtonSendData->Enabled = False;
                GroupBoxSendFiles->Enabled = False;
                TimerBlink->Interval = 100;   //闪动频率
                BitBtnOpen->Enabled = False ;
                if((EditAutoSendTime->Text!="自动发送时间")&&(EditAutoSendTime->Text!=""))
                {
                        TimerSend->Interval = StrToInt(EditAutoSendTime->Text);
                }
                EditAutoSendTime->Text = TimerSend->Interval;
                TimerSend->Enabled = True;
                /* TODO -oxukaiming : 加入发送数据的模块 */
                //
                
        }
        else
        {
                ButtonAutoSend->Caption="自动发送";
                ButtonSendData->Enabled = True;
                GroupBoxSendFiles->Enabled = True;
                BitBtnOpen->Enabled = True ;
                TimerBlink->Interval = BlinkTime;
                TimerSend->Enabled = False;
        }

}
//---------------------------------------------------------------------------

void __fastcall TFormPort::ButtonSaveClick(TObject *Sender)
{
        SaveDialogReceive->InitialDir = GetCurrentDir();
        if(SaveDialogReceive->Execute())
        {
                RichEditReceive->Lines->SaveToFile(SaveDialogReceive->FileName);
        }
}
//---------------------------------------------------------------------------

void __fastcall TFormPort::ButtonSendFileClick(TObject *Sender)
{
        OpenDialogSendFile->InitialDir = GetCurrentDir();
        if(OpenDialogSendFile->Execute())
        {
                EditFile->Enabled = False;
                ComboBoxProtocol->Enabled = False;
                ButtonSendFile->Enabled = False;
                ButtonReceiveFile->Enabled = False;
                GroupBoxSendData->Enabled = False;
                TimerBlink->Interval = 100;
        //  /* TODO -oxukaiming : 调用发送文件的模块 */
        }
}
//---------------------------------------------------------------------------

void __fastcall TFormPort::ButtonReceiveFileClick(TObject *Sender)
{

        SaveDialogReceive->InitialDir = GetCurrentDir();
        if(SaveDialogReceive->Execute())
        {
                ButtonSendFile->Enabled = False;
                ButtonReceiveFile->Enabled = False;
                GroupBoxSendData->Enabled = False;
                TimerBlink->Interval = 100;
                /* TODO -oxukaiming : 加入接受文件的模块 */
        }


}
//---------------------------------------------------------------------------

void __fastcall TFormPort::ButtonStopClick(TObject *Sender)
{
        ButtonSendFile->Enabled = True;
        ButtonReceiveFile->Enabled = True;
        GroupBoxSendData->Enabled = True;
        TimerBlink->Interval = BlinkTime;
}
//---------------------------------------------------------------------------


void __fastcall TFormPort::ComboBoxDataBitsChange(TObject *Sender)
{
         CommUart->DataBits = (TDataBits)(SendMessage(ComboBoxDataBits->Handle,
                                                CB_FINDSTRING,0,
                                                long(ComboBoxDataBits->Text.c_str()))+1);

}
//---------------------------------------------------------------------------

void __fastcall TFormPort::ComboBoxStopBitsChange(TObject *Sender)
{
        CommUart->StopBits = (TStopBits)(SendMessage(ComboBoxStopBits->Handle,
                                                CB_FINDSTRING,0,
                                                long(ComboBoxStopBits->Text.c_str())));
       
}
//---------------------------------------------------------------------------


void __fastcall TFormPort::ComboBoxParityChange(TObject *Sender)
{
        CommUart->Parity = (TParity)(SendMessage(ComboBoxParity->Handle,
                                                CB_FINDSTRING,0,
                                                long(ComboBoxParity->Text.c_str())));

⌨️ 快捷键说明

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