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

電子工程網(wǎng)

標(biāo)題: 串口控件SPComm多方法使用例程 [打印本頁(yè)]

作者: hotpower    時(shí)間: 2009-4-2 23:52
標(biāo)題: 串口控件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;//打開串口(注意:本例將串口的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]開始
      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]開始
      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開始
      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;
作者: alpha321    時(shí)間: 2010-8-5 18:39
樓主也搞DELPHI
作者: haoweng    時(shí)間: 2011-2-12 15:58





歡迎光臨 電子工程網(wǎng) (http://m.4huy16.com/) Powered by Discuz! X3.4