0

I use the CiaComPort in Delphi5, and I have a problem. I send a command to the device. I use the Send(Buffer: Pointer; Len: integer): cardinal function.

procedure TFormMain.CiaComportraParancsotKuld(CNev, Szoveg: WideString; NyoId, PortSzam: Integer);
var
  Kar: PChar;
  Szam: Integer;
  Parancs: WideString;
begin
  Parancs := #$0002+'~JS0|'+CNev+'|0|'+Szoveg+#$0003;
  Kar := PChar(Parancs);
  Szam := length(Parancs)*2;
  FormMain.CiaComPort1.Open := True;
  FormMain.CiaComPort1.Send(Kar, Szam);
  FormMain.CiaComPort1.Open := False;
end;

This procedure is fine, but when I send the command, unfortunately I don't see the coming characters from the device, because In my opinion I do not use the CiaComPort1DataAvailable(Sender: TObject) well.

//Receive(Buffer: Pointer; Len: integer): cardinal

procedure TForm1.CiaComPort1DataAvailable(Sender: TObject);
var
  Kar: PChar;
  Szam: Integer;
  Parancs: WideString;
begin
  Szam := RxCount;
  Parancs := WideString(Receive(Kar, Szam)); //I think that's not good.
  Memo1.Lines.Add(Parancs);
end;

Unfortunately I can't read the buffer. Do you have any ideas?

4

1 回答 1

2

显然,RxCount告诉您收到了多少字节。该Receive函数希望您给它一个缓冲区,然后它将填充该缓冲区,直到您告诉它的大小。在您的代码中,您提供了大小,但没有提供缓冲区。您需要为缓冲区分配空间。如果您将WideString用作缓冲区,则分配空间SetLength

Szam := RxCount;
SetLength(Parancs, Szam div 2);
Receive(PWideChar(Parancs), Szam);

不知道返回值是什么Receive意思,这里就不演示它的使用了。我敢肯定,如果您查看文档,您可以了解它的用途。

于 2013-08-28T19:35:59.360 回答