MSCOMM參數設定過程

來源:互聯網
上載者:User

//以下是建立表單時的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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.