我在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次?谢谢!
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次?谢谢!
后面的
2017/8/28 7:16:49是不是激发事件的代码中,有一条"memo1.Lines.Add(DateTimetostr(now())); 代码。
"
2、自己追踪一下变量,看看这个时间是哪个变量赋值的。
所以你的代码中的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;