国产精品免费无遮挡无码永久视频-国产高潮视频在线观看-精品久久国产字幕高潮-国产精品99精品无码视亚

查看: 5650|回復(fù): 2
打印 上一主題 下一主題

串口控件SPComm多方法使用例程

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2009-4-2 23:52:00 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
關(guān)鍵詞: SPComm , 串口 , 控件 , 例程
重點(diǎn)注意:本例程一定要將串口的2,3腳短聯(lián)做自發(fā)自收演示)
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  str: string;
begin
  str := '';
  for i := 0 to 2047 do
  begin
    str := str + Char(i and $ff);//所有字符0x00~0xff
  end;
  Comm1.PortOpen := true;//打開(kāi)串口(注意:本例將串口的2,3腳短聯(lián)做自發(fā)自收演示)
  Comm1.Output := str;//發(fā)送2048個(gè)字符到串口
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: PAnsiChar;//舊事件處理方法用Buffer: Pointer
  BufferLength: Word);
var
  i: integer;
  str: string;
begin                                      
  Memo1.Lines.Add('SPCOMM控件改造應(yīng)用方法一:(String)');
  SetString(str, Buffer, BufferLength);//從串口接收BufferLength個(gè)字節(jié)
  if Length(str) <> BufferLength then Exit;
  for i := 0 to BufferLength - 1 do
  begin
    if (i and $f) = 0 then
    begin
      Buffers := inttohex(i, 4) + ': ';
    end;
    if (i and $ff) = Byte(str[i + 1]) then//str[i]從str[1]開(kāi)始
      Buffers := Buffers + '0x' + inttohex(Byte(str[i + 1]), 2) + ' '
    else //接收錯(cuò)誤顯示
      Buffers := Buffers + '**/' + inttohex(Byte(str[i + 1]), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//輸出一行顯示數(shù)據(jù)
      Buffers := '';
    end;
  end;
  Memo1.Lines.Add('');
  Memo1.Lines.Add('SPCOMM控件改造應(yīng)用方法二:(Array)');
  for i := 0 to BufferLength - 1 do
  begin
    if (i and $f) = 0 then
    begin
      Buffers := inttohex(i, 4) + ': ';
    end;
    if (i and $ff) = Byte(Buffer[i]) then//Buffer[i]從Buffer[0]開(kāi)始
      Buffers := Buffers + '0x' + inttohex(Byte(Buffer[i]), 2) + ' '
    else //接收錯(cuò)誤顯示
      Buffers := Buffers + '**' + inttohex(Byte(Buffer[i]), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//輸出一行顯示數(shù)據(jù)
      Buffers := '';
    end;
  end;
  Memo1.Lines.Add('');
  Memo1.Lines.Add('SPCOMM控件改造應(yīng)用方法三:(Pointer)');
  for i := 0 to BufferLength - 1 do
  begin
    if (i and $f) = 0 then
    begin
      Buffers := inttohex(i, 4) + ': ';
    end;
    if (i and $ff) = Byte((Buffer + i)^) then//Buffer+i從Buffer開(kāi)始
      Buffers := Buffers + '0x' + inttohex(Byte((Buffer + i)^), 2) + ' '
    else //接收錯(cuò)誤顯示
      Buffers := Buffers + '**' + inttohex(Byte((Buffer + i)^), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//輸出一行顯示數(shù)據(jù)
      Buffers := '';
    end;
  end;
  Memo1.Lines.Add('');
  Memo1.Lines.Add('SPCOMM控件改造應(yīng)用方法四:(Pointer)');
  for i := 0 to BufferLength - 1 do
  begin
    if (i and $f) = 0 then
    begin
      Buffers := inttohex(i, 4) + ': ';
    end;
    if (i and $ff) = Byte(Buffer^) then
      Buffers := Buffers + '0x' + inttohex(Byte(Buffer^), 2) + ' '
    else //接收錯(cuò)誤顯示
      Buffers := Buffers + '**' + inttohex(Byte(Buffer^), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//輸出一行顯示數(shù)據(jù)
      Buffers := '';
    end;
    Inc(Buffer, SizeOf(Char));//移動(dòng)字符指針
  end;
end;
沙發(fā)
發(fā)表于 2010-8-5 18:39:29 | 只看該作者
樓主也搞DELPHI
板凳
發(fā)表于 2011-2-12 15:58:52 | 只看該作者

本版積分規(guī)則

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權(quán)所有   京ICP備16069177號(hào) | 京公網(wǎng)安備11010502021702
快速回復(fù) 返回頂部 返回列表