Unable to receive data

Hello
I have an IR temperature sensor connected to my computer via a USB to RS-485 cable, I try to send a hex string (#$54#$50#$01#$F1#$96) using the WriteText method and everything is fine, But when I use the ReadText method to receive data there has been no data returned.
I tried using the ReadBuf method with the same result.
But when I pause for 1 second (Sleep(1000)) before the ReadText method, I can get the data. I would like to know what is the reason for this and how can I receive data without using pause?

Here is the code I am currently testing:

procedure TfrmMain.ButtonTransmitClick(Sender: TObject);
var
S: string;
Ok: Boolean;
begin
S := #$54#$50#$01#$F1#$96;
Ok := VaComm1.WriteText(S);
if not Ok then
Memo1.Lines.add('Error writing to: ' + Format('Port %d', [VaComm1.PortNum]))
else
begin
// Sleep(1000);
S := VaComm1.ReadText;
if Length(S) >= 7 then // Returns fixed length data
Memo1.Lines.Add('read successfully');
end;
end;

I suggest to use on event OnRxChar and read the data from this event.
Otherwise, VaComm.ReadText returns what is in the buffer at the time of calling it and when you call this immediately after WriteText() and the device could not respond yet, the buffer will still be empty at time of calling ReadText() (or ReadBuf())

1 Like