<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > C++Builder串口通信設(shè)計(jì)(二)-發(fā)送字節(jié)包

          C++Builder串口通信設(shè)計(jì)(二)-發(fā)送字節(jié)包

          作者: 時(shí)間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
          一、組成

          1、PC機(jī)

          本文引用地址:http://www.ex-cimer.com/article/201611/322878.htm

          2、STM32F10x

          通過通過串口連接(由相應(yīng)的硬件芯片支持:MAX323或USB轉(zhuǎn)串口芯片等)。

          設(shè)計(jì)一個(gè)由PC機(jī)通過串口發(fā)送數(shù)據(jù)包的程序。串口也可采用USB轉(zhuǎn)串口。串口與單片機(jī)(stm32F10x通信)。單片機(jī)程序是收到數(shù)據(jù)后再通過串口發(fā)回去(發(fā)給PC機(jī))。

          二、設(shè)計(jì)工程

          1、界面

          一個(gè)mscomm控件;

          一個(gè)memo1控件顯示串口接收的內(nèi)容;

          三個(gè)button分別用于串口初始化、發(fā)送、終止程序;


          2、串口初始化化部分

          //---------------------------------------------------------------------------
          void __fastcall TForm1::Button1Click(TObject *Sender) //串口初始化
          {
          static st=0;
          AnsiString s;
          if(st==0)
          {
          try
          {
          Form1->MSComm1->_CommPort=3;//COM3( 采用USB轉(zhuǎn)串口)
          Form1->MSComm1->Settings="9600,n,8,1"; //初始化串口
          Form1->MSComm1->InputMode=type; //采用全局變量type設(shè)置傳入數(shù)據(jù)的格式,0表示文本形式 ,1表示二進(jìn)制格式,初始默認(rèn)取為1。
          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;
          }
          }
          3、串口中斷接收部分

          //---------------------------------------------------------------------------
          void __fastcall TForm1::MSComm1Comm(TObject *Sender) //串口接收事件
          {
          AnsiString str; //聲明一個(gè)AnsiString類型的變量
          OleVariant rec; //聲明一個(gè)用于接收數(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
          {
          buf[j]=rec.GetElement(j); //轉(zhuǎn)換成字節(jié)類型
          }
          Memo1->Text=Memo1->Text+"";
          for(j=0;j
          {
          Memo1->Text=Memo1->Text+IntToHex(buf[j],2)+" "; //顯示接收的字節(jié)(以十六進(jìn)制顯示)
          }
          }
          break;
          default: break;
          } //switch(MSComm1->CommEvent)
          }
          //---------------------------------------------------------------------------

          4、串口發(fā)送部分

          //---------------------------------------------------------------------------
          void __fastcall TForm1::Button2Click(TObject *Sender) //發(fā)送
          {
          int i;
          OleVariant send;
          int count=5; //發(fā)送包的字節(jié)數(shù)
          unsigned char pc_to_stm32[5]={0x80,0x81,0xff,0x05,0x05}; //PC發(fā)出的數(shù)據(jù)包
          send=VarArrayCreate(OPENARRAY(int,(0,count-1)),varByte); //創(chuàng)建一個(gè)動(dòng)態(tài)數(shù)組
          for(i=0;i
          {
          send.PutElement(pc_to_stm32[i],i); //填充待發(fā)送的數(shù)據(jù)元素
          }
          MSComm1->Output=send;//從串口發(fā)送
          }

          5、程序終止部分

          void __fastcall TForm1::Button3Click(TObject *Sender) //退出應(yīng)用程序
          {
          Application->Terminate();
          }

          三、運(yùn)行結(jié)果



          評(píng)論


          相關(guān)推薦

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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();