我在memo控件中使用了如下代码:procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
 xs: PChar;
begin
  GetLocalTime(st);
  xs:=Buffer;
  if(xs='1') then
    xs:='退出1#油泵选择!';
  if(xs='a') then
    xs:='选择1#油泵!';
memo1.Lines.Add(xs+DateTimetostr(now()));
end;为什么在运行的时候,时间会在memo控件上出现2次?谢谢!

解决方案 »

  1.   

    从代看,只显示“退出1#油泵选择!2017/8/28 7:16:49”
    后面的
    2017/8/28 7:16:49是不是激发事件的代码中,有一条"memo1.Lines.Add(DateTimetostr(now())); 代码。
    "
      

  2.   

    同意楼上,其他事件中应该还有add语句
      

  3.   

    1、搜索全部的 memo1.Lines.Add字段,看看是不是在什么地方触发了事件。
    2、自己追踪一下变量,看看这个时间是哪个变量赋值的。
      

  4.   

    你的COM口接收数据处理有问题,你只对可见字符'1'和'a'做了处理,有可能是COM设备还返回了其它内容,
    所以你的代码中的xs:=Buffer,这样处理是不完整的。procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    var
     s:string;
      i:Word;
      p,pData:PByte;
    begin
      GetLocalTime(st);
      s:='';
      GetMem(pData,BufferLength);
      Move(Buffer^, PByte(pData)^, BufferLength);
      p:=pData;
      i:=0;
      s:='';
      while i<BufferLength do
      begin
        s:=s+UpperCase(IntToHex(p^,2));
        inc(p);
        inc(i);
      end; 
      memo1.Lines.Add(s+DateTimetostr(now()));//用16进制格式返回接收到的数据
    end;