user2363754
user2363754

Reputation: 3

receive coming characters in ciacomport

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?

Upvotes: 0

Views: 376

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163357

Evidently, RxCount tells you how many bytes you received. The Receive function expects you to give it a buffer, and then it will fill that buffer, up to the size you tell it. In your code, you've provided the size, but you haven't provided a buffer. You need to allocate space for the buffer. If you use the WideString as your buffer, then you allocate space with SetLength:

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

I don't know what the return value of Receive means, so I have not demonstrated its use here. I'm sure if you check the documentation, you can learn what it's for.

Upvotes: 2

Related Questions