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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應用 > delphi與51單片機串行通信

          delphi與51單片機串行通信

          作者: 時間:2016-11-22 來源:網(wǎng)絡(luò) 收藏
          說明:可能前面幾次首發(fā)不太準確

          //delphi源碼

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

          unit Unit1;

          interface

          uses
          Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
          Dialogs, SPComm, StdCtrls;

          type
          TFCOMM = class(TForm)
          Button1: TButton;
          Button2: TButton;
          Button3: TButton;
          Memo1: TMemo;
          Comm1: TComm;
          procedure FormShow(Sender: TObject);
          procedure FormClose(Sender: TObject; var Action: TCloseAction);
          procedure Button1Click(Sender: TObject);
          procedure Button2Click(Sender: TObject);
          procedure Button3Click(Sender: TObject);
          procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
          BufferLength: Word);
          private
          { Private declarations }
          public
          { Public declarations }
          end;

          var
          FCOMM: TFCOMM;
          viewstring:string;
          i:integer;
          rbuf,sbuf:array[1..9] of byte;

          implementation

          {$R *.dfm}

          procedure TFCOMM.FormShow(Sender: TObject);
          begin
          comm1.startcomm;
          end;

          procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
          begin
          comm1.stopcomm;
          end;
          //自定義發(fā)送數(shù)據(jù)過程

          procedure senddata;
          var
          i:integer;
          commflg:boolean;
          begin
          viewstring:= ;
          commflg:=true;
          for i:=1 to 9 do
          begin
          if not fcomm.comm1.writecommdata(@sbuf[i],1) then
          begin
          commflg:=false;
          break;
          end;
          //發(fā)送時字節(jié)間的延時
          sleep(2);
          viewstring:=viewstring+inttohex(sbuf[i],1)+ ;
          end;
          viewstring:=發(fā)送+viewstring;
          fcomm.memo1.lines.add(viewstring);
          fcomm.memo1.lines.add( );
          if not commflg then
          messagedlg(發(fā)送失敗! ,mterror,[mbyes],0);
          end;

          procedure TFCOMM.Button1Click(Sender: TObject);
          begin
          sbuf[1]:=byte($95); //幀頭
          sbuf[2]:=byte($10); //命令號
          sbuf[3]:=byte($20);
          sbuf[4]:=byte($25);
          sbuf[5]:=byte($95); //幀頭
          sbuf[6]:=byte($10); //命令號
          sbuf[7]:=byte($20);
          sbuf[8]:=byte($25);
          sbuf[9]:=byte($25);
          senddata;
          end;

          procedure TFCOMM.Button2Click(Sender: TObject);
          begin
          memo1.clear;
          end;

          procedure TFCOMM.Button3Click(Sender: TObject);
          begin
          close;
          end;

          procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
          BufferLength: Word);
          var
          i:integer;
          begin
          viewstring:=;
          move(buffer^,rbuf,bufferlength);
          for i:=1 to bufferlength do
          viewstring:=viewstring+ inttohex(rbuf[i],2)+ ;
          viewstring:=接收+ viewstring;
          memo1.lines.add(viewstring);
          memo1.lines.add();
          end;
          end.

          //單片機源碼

          #include

          void send(b)
          {

          SBUF=b;
          while(TI==0);
          TI=0;

          }

          void main()
          {
          int i=0,k;
          unsigned int a[9];
          TMOD=0x20;
          TL1=0xfd;
          TH1=0xfd;
          SCON=0xd8;
          PCON=0x00;
          TR1=1;
          while(1)
          { if(i>8)break;
          while(RI==0);
          RI=0;
          a[i]=SBUF;
          //for(k=0;k<200;k++);
          i++;
          }
          for(i=0;i<9;i++)
          {
          send(a[i]);
          //for(k=0;k<10000;k++);
          }
          }



          關(guān)鍵詞: delphi51單片機串行通

          評論


          技術(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); })();