Chinaunix首页 | 论坛 | 博客
  • 博客访问: 949791
  • 博文数量: 214
  • 博客积分: 10173
  • 博客等级: 上将
  • 技术积分: 1867
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-18 13:48
文章分类

全部博文(214)

文章存档

2012年(1)

2010年(13)

2009年(5)

2008年(98)

2007年(97)

分类: LINUX

2008-03-08 22:37:47

重点:(注意:本例程一定要将串口的2,3脚短联做自发自收演示)

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脚短联做自发自收演示)
  Comm1.Output := str;//发送2048个字符到串口
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: PAnsiChar;//旧事件处理方法用Buffer: Pointer
  BufferLength: Word);
var
  i: integer;
  str: string;
begin                                      

  Memo1.Lines.Add('SPCOMM控件改造应用方法一:(String)');
  SetString(str, Buffer, BufferLength);//从串口接收BufferLength个字节
  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 //接收错误显示
      Buffers := Buffers + '**/' + inttohex(Byte(str[i + 1]), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//输出一行显示数据
      Buffers := '';
    end;
  end;

  Memo1.Lines.Add('');
  Memo1.Lines.Add('SPCOMM控件改造应用方法二:(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 //接收错误显示
      Buffers := Buffers + '**' + inttohex(Byte(Buffer[i]), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//输出一行显示数据
      Buffers := '';
    end;
  end;

  Memo1.Lines.Add('');
  Memo1.Lines.Add('SPCOMM控件改造应用方法三:(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 //接收错误显示
      Buffers := Buffers + '**' + inttohex(Byte((Buffer + i)^), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//输出一行显示数据
      Buffers := '';
    end;
  end;

  Memo1.Lines.Add('');
  Memo1.Lines.Add('SPCOMM控件改造应用方法四:(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 //接收错误显示
      Buffers := Buffers + '**' + inttohex(Byte(Buffer^), 2) + ' ';
    if (i and $f) = $f then
    begin
      Memo1.Lines.Add(Buffers);//输出一行显示数据
      Buffers := '';
    end;
    Inc(Buffer, SizeOf(Char));//移动字符指针
  end;

end;

文件: spcommdemo.rar
大小: 175KB
下载: 下载

阅读(3849) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~