Problem with reading a complete string on a serial port

Problem with reading a complete string on a serial port

Dear all.

I have a problem with a reading a string on a serial port.
I can send string, send character, read character but I can't solve reading of complete string.
I have two similar functions in a unit for reading from a serial port:

function TPLC_Comm.ReadStr(var S: String) : Boolean;
var
TempArray : array[1..255] of Byte;
Count, RX_Count : DWord;
begin
S := '';
ReadFile(Com, TempArray, 255, RX_Count, nil);
for Count := 1 to RX_Count do S := S + Chr(TempArray[Count]);
Form_Glavna.Edit_ToPrint.Text := S;
if length(S) > 3 then Result := True;
end;

function TPLC_Comm.ReadChr(var C: Char) : Boolean;
var RX_Count: DWord;
begin
ReadFile(Com, C, 4, RX_Count, nil);
Result := (RX_Count=1);
end;

So in Execute for reading a character I do something like this:
Repeat
If ReadChr(Ch) then
Begin
IF (Ch=$#06) then ...( do something);
If (Ch=$#15) then ...(do something2);
End;
Until (Ch in [$#06, $#15];

This working great. But if I use a same method but with function of reading a string like:
Repeat
If ReadStr(S) then
Begin
If length(S) > 3 then ... (do something 3);
End;
Until length(S) > 3;

It happens that from time to time I miss a complete string and usually I do not receive a first character in a string.
Does anybody have an idea how to solve this?

Thx
Tomislav

Comments

  1. I am using 2 kind of components. No problem.

    1. on d10.2.3 CPort < comport411f
    ComPort Library version 4.10
    for Delphi 5, 6, 7, 2005, 2006, 2007, 2010, XE
    and C++ Builder 3, 4, 5, 6
    by Dejan Crnila 1998-2002

    2.. On D7 CommPortDriver < ComDrv32
    Written by Marco Cocco

    Both are excellent and sync with no problem.
    On rs232 you sync on a char otherwise you fail often.

    ReplyDelete
  2. I don't use any component…
    If you understand a principle of RS232 than is not a problem to write some part of code.
    For me it was a problem that I suppose that I will receive a complete string in one loop what is ridiculous. When I get information from “another angle” – everything gets in place and now is 100% working.
    I test my appl. in last 12h in full exchange of strings and chars on serial port without losing any information…

    Tomislav

    ReplyDelete
  3. Tomislav Salar Use the right component the right way and you will not have your problem !

    ReplyDelete

Post a Comment