<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x
    查看: 6096|回復: 0
    打印 上一主題 下一主題

    在Delphi中使用串口控件MSComm的0字符接收例程祥解

    [復制鏈接]
    跳轉到指定樓層
    樓主
    發表于 2009-4-2 23:50:54 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
    關鍵詞: Delphi , MSComm , 串口 , 控件 , 例程
    菜農HotPower@126.com 發表于 2006/12/28 22:55:20
    //以下是創建窗體時的MSCOMM參數設置過程
    //MSComm1.InputMode := comInputModeBinary;
    //和MSComm1.InputMode := comInputModeText;
    //實驗結果基本對Delghi不太起作用

    procedure TForm1.FormCreate(Sender: TObject);
    var
      str: string;
    begin
    //MSCOMM參數設置
      MSComm1.CommPort := 1;//使用COM1
      MSComm1.Settings := '9600,N,8,1';//設置通信口參數
      MSComm1.InBufferSize := 32;//設置MSComm1接收緩沖區為32字節
      MSComm1.OutBufferSize := 2;//設置MSComm1發送緩沖區為2字節
      MSComm1.InputMode := comInputModeBinary;//設置接收數據模式為二進制形式
      MSComm1.InputLen := 1;//設置Input 一次從接收緩沖讀取字節數為1
      MSComm1.SThreshold := 1;//設置Output 一次從發送緩沖讀取字節數為1
      MSComm1.InBufferCount := 0;//清除接收緩沖區
      MSComm1.OutBufferCount := 0;//清除發送緩沖區
      MSComm1.RThreshold := 1;//設置接收一個字節產生OnComm事件
      MSComm1.PortOpen := true;//打開串口1
    /////////////////////////////////////////////////////////////
      Buffers := '';
      CheckSum := 0;
    //發送串口命令
      Command := 34;
      str := '$' + #2 + #$22 + #1;//讀MP3總曲目
      str := str + Char(GetCheckSum(str));//計算校驗和
      MSComm1.Output := str;//發送串口命令
    end;

    //以下是接收事件處理過程,在MCU中相當于串口中斷
    //注意其中2個語句
    //while MSComm1.InBufferCount > 0 do//輸入FiFO不為空
    //if str = '' then str := #0; //0字符處理
    //例接收的數據為#24#02#00#01#03
    //此時InBufferCount=5.若設置Input 一次從接收緩沖讀取字節數不限
    //即:MSComm1.InputLen := 0;則str := MSComm1.Input;后str好象為#24#02#00#01#03
    //但實際為'??'#24#02.總得不到結果,至少0字符后的#01#03無法讀出.
    //采用MSComm1.InputLen := 1;后,并配合while MSComm1.InBufferCount > 0 do
    //當讀到0字符時,由于str=''(空),故訪問str[1]將會引發異常的發生而導致程序的終止.
    //故用if str = '' then str := #0; 來向str[1]內認為地填入字符#0且str的長度也為1了.
    //故此要點是用if str = '' then str := #0;語句渡過讀0字符的難關~~~
    procedure TForm1.MSComm1Comm(Sender: TObject);
    var
      str: string;
      i: integer;
    begin
      case MSComm1.CommEvent of
        comEvReceive://接收事件處理
          begin
            while MSComm1.InBufferCount > 0 do//輸入FiFO不為空
            begin
              str := MSComm1.Input;//從FIFO中只取1個字符,因為MSComm1.InputLen := 1
              if str = '' then str := #0; //0字符處理
              if (Buffers = '') and (str = '$') then//同步符測試
              begin
                Buffers := str;//存入同步符'$'
                CheckSum := 0;//初始化校驗和
              end
              else if (Buffers <> '') and (Buffers[1] = '$') then begin//必須用同步符起始
                Buffers := Buffers + str;//加入數據串
                CheckSum := CheckSum xor Byte(str[1]);//求校驗和(除同步符'$'外)
                if Length(Buffers) = Byte(Buffers[2]) + 3 then//結束符測試
                begin
                  if CheckSum = 0 then//此時校驗和必須為0表示校驗和正確
                  begin
                    case Command of
                      $22: begin//取歌曲總數
                            ComboBox1.Items.Clear;
                            for i := 1 to Byte(Buffers[4]) do
                            begin
                              str := '第' + inttostr(i) + '首歌曲';
                              ComboBox1.Items.Add(str);//
                            end;
                            Command := 0;
                          end;
                      1: ;
                    else ;
                    end;
                  end;
                  Buffers := '';//接收完畢清空緩沖區
                  CheckSum := 0;//初始化校驗和
                end;
              end
              else
              begin
                Buffers := '';//接收錯誤清空緩沖區,放棄所有數據
                CheckSum := 0;//初始化校驗和
              end;
            end;
          end;
      end;
    end;
    您需要登錄后才可以回帖 登錄 | 立即注冊

    本版積分規則

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷