在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > C++builder串口通信設(shè)計(一)-串口接收數(shù)據(jù)

            C++builder串口通信設(shè)計(一)-串口接收數(shù)據(jù)

            作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏

            3、unit1.cpp代碼


            void __fastcall TForm1::Button1Click(TObject *Sender) // 打開
            {
            static st=0;
            AnsiString s;
            if(st==0)
            {
            try
            {
            Form1->MSComm1->_CommPort=3;//COM3
            Form1->MSComm1->Settings="9600,n,8,1"; //初始化串口
            Form1->MSComm1->InputMode=type; ////設(shè)置傳入數(shù)據(jù)的格式,0表示文本形式 ,1表示二進(jìn)制格式
            Form1->MSComm1->RThreshold=1;
            Form1->MSComm1->PortOpen=true; //打開串口
            Application->MessageBoxA("串口初始化成功","串口初始化",0);
            }
            catch(...)
            //catch(EOleException&e)
            {
            Application->MessageBoxA("串口未連接好或已經(jīng)打開","故障提示");
            }

            Button1->Caption="關(guān)閉";
            st=1;
            }
            else
            {
            Form1->MSComm1->PortOpen=false;
            Button1->Caption="打開";
            st=0;
            }
            }
            //---------------------------------------------------------------------------
            void __fastcall TForm1::MSComm1Comm(TObject *Sender) //串口接收事件
            {
            AnsiString str; //聲明一個AnsiString類型的變量
            OleVariant rec; //聲明一個用于接收數(shù)據(jù)的OleVariant變量。
            int count;
            int j;
            unsigned char buf[128];

            switch(MSComm1->CommEvent)
            {
            case comEvReceive: //接收事件
            if(type==0) //字符型接收
            {
            str=MSComm1->Input;//收到字符串;
            Memo1->Text=Memo1->Text+str+" "; //顯示收到的字符串
            }
            else //type=1 為二進(jìn)制接收
            {
            count=MSComm1->InBufferCount; //字節(jié)數(shù)
            rec=MSComm1->Input; //取出接收緩沖器內(nèi)容
            for(j=0;j小于count;j++)
            {
            buf[j]=rec.GetElement(j); //轉(zhuǎn)換成字節(jié)類型
            }
            Memo1->Text=Memo1->Text+"";
            for(j=0;j小于count;j++)
            {
            Memo1->Text=Memo1->Text+IntToHex(buf[j],2)+" "; //顯示接收的字節(jié)(以十六進(jìn)制顯示)
            }
            }
            break;
            default: break;
            } //switch(MSComm1->CommEvent)
            }
            //---------------------------------------------------------------------------
            void __fastcall TForm1::Button2Click(TObject *Sender)
            {
            Application->Terminate();
            }
            //---------------------------------------------------------------------------
            void __fastcall TForm1::FormCreate(TObject *Sender) //初始化
            {
            RadioButton1->Checked=false;
            RadioButton2->Checked=true;
            type=1; //0--字符,1---二進(jìn)制
            }
            //---------------------------------------------------------------------------
            void __fastcall TForm1::RadioButton1Click(TObject *Sender) //字符型
            {
            RadioButton2->Checked=false;
            RadioButton1->Checked=true;
            type=0; //字符型
            Form1->MSComm1->InputMode=type;
            }
            //---------------------------------------------------------------------------
            void __fastcall TForm1::RadioButton2Click(TObject *Sender) //二進(jìn)制
            {
            RadioButton1->Checked=false;
            RadioButton2->Checked=true;
            type=1;//二進(jìn)制
            Form1->MSComm1->InputMode=type;
            }
            //---------------------------------------------------------------------------

            四、運行結(jié)果





            可見,選擇二進(jìn)制模式或字符模式都可以完成串口接收通信。


            上一頁 1 2 下一頁

            評論


            相關(guān)推薦

            技術(shù)專區(qū)

            關(guān)閉