BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading string from Comm port

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Thirdparty Tools (General)
View previous topic :: View next topic  
Author Message
Ernest P. Woorell
Guest





PostPosted: Mon May 21, 2007 8:02 am    Post subject: Reading string from Comm port Reply with quote



I am having trouble reading from a Com port. I have no controll over the
Sending app. It is to sent to me GETDATA and I am to replay with either
data or ALIVE. That is the protocol.

I am using Com Port Library 3.1

I listen using the ComPort1RxChar(Sender: TObject; Count: Integer);

My problem is that the G always comes first and then some portion of the
remainder of the string.

What I want is a result along the lines of

If Str = 'GETDATA' then
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));

I amd successfully able to read GETDATA into the Memo1 box by putting the
charaters together but I cannot get the If to fire.

I am not sure of the delimiters being used by the sender. In
Hyper-Terminal, I see none. But I should be able to filter out everything
but 'GETDATA' , right? Can someone throw me a bone on how to do this?


procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
fBuffer : String;
begin
ComPort1.ReadStr(Str, Count);
Memo1.Text := Memo1.Text + Str;
fBuffer := fBuffer + Str;
If fBuffer = 'GETDATA' then
Begin
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));
End;
end;
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon May 21, 2007 8:12 am    Post subject: Re: Reading string from Comm port Reply with quote



"Ernest P. Woorell" <lkillen (AT) charter (DOT) net> wrote in message
news:46510b86$1 (AT) newsgroups (DOT) borland.com...

Quote:
I amd successfully able to read GETDATA into the Memo1 box
by putting the charaters together but I cannot get the If to fire.

That is because you are not taking into account that the data can come
in multiple events. You have to cache the data on each triggering of
the event and then process the contents of the cache only when all of
the data is actually available. You are declaring your cache buffer
inside the event when it needs to be outside instead. For example:

var
fBuffer : String = '';

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
begin
ComPort1.ReadStr(Str, Count);
fBuffer := fBuffer + Str;
if Copy(fBuffer, 1, 7) = 'GETDATA' then
begin
Delete(fBuffer, 1, 7);
ComPort1.WriteStr(Chr(02) + 'ALIVE2' + Chr(03));
end;
end;

Now, with that said, your reply begins with the STX character and ends
with the ETX character. Does every packet of the entire protocol work
that same way? If so, then your buffer processing will need to take
those characters into account as well.


Gambit
Back to top
Warrick Wilson
Guest





PostPosted: Tue May 22, 2007 2:10 am    Post subject: Re: Reading string from Comm port Reply with quote



"Ernest P. Woorell" <lkillen (AT) charter (DOT) net> wrote in message
news:46510b86$1 (AT) newsgroups (DOT) borland.com...
Quote:
I am having trouble reading from a Com port. I have no controll over the
Sending app. It is to sent to me GETDATA and I am to replay with either
data or ALIVE. That is the protocol.

I am using Com Port Library 3.1

I listen using the ComPort1RxChar(Sender: TObject; Count: Integer);

My problem is that the G always comes first and then some portion of the
remainder of the string.

What I want is a result along the lines of

If Str = 'GETDATA' then
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));

I amd successfully able to read GETDATA into the Memo1 box by putting the
charaters together but I cannot get the If to fire.

I am not sure of the delimiters being used by the sender. In
Hyper-Terminal, I see none. But I should be able to filter out everything
but 'GETDATA' , right? Can someone throw me a bone on how to do this?


procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
fBuffer : String;
begin
ComPort1.ReadStr(Str, Count);
Memo1.Text := Memo1.Text + Str;
fBuffer := fBuffer + Str;
If fBuffer = 'GETDATA' then
Begin
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));
End;
end;

I'm not familiar with ComPort, but I've done some serial stuff before. There
are some potentially problematic things I see in your code snippet. Gambit
was asking you about them, too...

First, fBuffer is local (it's in the var section). When you grab a few
character, but not enough to spell GETDATA, you'll leave the function and
whatever you gathered up will be lost. If you want to get an idea of what
you're really seeing come in, change

Memo1.Text := Memo1.Text + Str;

to
Memo1.Lines.Add(Str);

I suspect you won't get a lot of 'GETDATA' on one line.

So you probably need fBuffer to be global, or at least external to your
RxChar() function.

Also, you are putting STX/ETC around your response. Is the incoming stream -
the stuff that should have GETDATA - using the STX/ETX, or just sending
'GETDATA' when you're required to respond?

Finally, I'm not usre how "clean" and reliable your serial transmissions
will be. You probably need to add some logic to account for garbled messages
coming in. For example, you could accumulating characters from the buffer
until you have too many to spell "GETDATA". Then you need to reset it. But
take into account what happens if you get a stream like

GETGETDATAGETDATAGETdATAgetDATAGETDATA

You might want to account for something like that coming in - at different
places, you'd need to throw out what you'd receive and start listening
again. Similarly, you should reset your receiving buffer once you get a
valid GETDATA and reply to it.
Back to top
Ernest P. Woorell
Guest





PostPosted: Tue May 22, 2007 5:43 am    Post subject: Re: Reading string from Comm port Reply with quote

Actually I am seeing a #D#A after the GETDATA. How can I use that to my
advantage?
I am trying some while loops with a break in #D#A but I have not been lucky;

Thanks


btw - GETDATA is the command being sent to my app. I replay using with
STX/ETX demiters since the author of that piece requires it. The reply
works correctly.


"Warrick Wilson" <warrickw (AT) mercuryonline (DOT) com> wrote in message
news:46520b07 (AT) newsgroups (DOT) borland.com...
Quote:
"Ernest P. Woorell" <lkillen (AT) charter (DOT) net> wrote in message
news:46510b86$1 (AT) newsgroups (DOT) borland.com...
I am having trouble reading from a Com port. I have no controll over the
Sending app. It is to sent to me GETDATA and I am to replay with either
data or ALIVE. That is the protocol.

I am using Com Port Library 3.1

I listen using the ComPort1RxChar(Sender: TObject; Count: Integer);

My problem is that the G always comes first and then some portion of the
remainder of the string.

What I want is a result along the lines of

If Str = 'GETDATA' then
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));

I amd successfully able to read GETDATA into the Memo1 box by putting the
charaters together but I cannot get the If to fire.

I am not sure of the delimiters being used by the sender. In
Hyper-Terminal, I see none. But I should be able to filter out
everything but 'GETDATA' , right? Can someone throw me a bone on how to
do this?


procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
fBuffer : String;
begin
ComPort1.ReadStr(Str, Count);
Memo1.Text := Memo1.Text + Str;
fBuffer := fBuffer + Str;
If fBuffer = 'GETDATA' then
Begin
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));
End;
end;

I'm not familiar with ComPort, but I've done some serial stuff before.
There are some potentially problematic things I see in your code snippet.
Gambit was asking you about them, too...

First, fBuffer is local (it's in the var section). When you grab a few
character, but not enough to spell GETDATA, you'll leave the function and
whatever you gathered up will be lost. If you want to get an idea of what
you're really seeing come in, change

Memo1.Text := Memo1.Text + Str;

to
Memo1.Lines.Add(Str);

I suspect you won't get a lot of 'GETDATA' on one line.

So you probably need fBuffer to be global, or at least external to your
RxChar() function.

Also, you are putting STX/ETC around your response. Is the incoming
stream - the stuff that should have GETDATA - using the STX/ETX, or just
sending 'GETDATA' when you're required to respond?

Finally, I'm not usre how "clean" and reliable your serial transmissions
will be. You probably need to add some logic to account for garbled
messages coming in. For example, you could accumulating characters from
the buffer until you have too many to spell "GETDATA". Then you need to
reset it. But take into account what happens if you get a stream like

GETGETDATAGETDATAGETdATAgetDATAGETDATA

You might want to account for something like that coming in - at
different places, you'd need to throw out what you'd receive and start
listening again. Similarly, you should reset your receiving buffer once
you get a valid GETDATA and reply to it.

Back to top
Ronaldo Souza
Guest





PostPosted: Tue May 22, 2007 6:16 am    Post subject: Re: Reading string from Comm port Reply with quote

Ernest P. Woorell wrote:
Quote:

What I want is a result along the lines of

If Str = 'GETDATA' then
ComPort1.WriteStr(Chr(02)+'ALIVE2'+Chr(03));

The data you write is enclosed in a STX/ETX pair of chars (STX = char #2
= "start of text"; ETX = char #3 = "end of text"). Is the data you
receive also enclosed in a STX/ETX pair?

--
Rgds,
Ronaldo
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Thirdparty Tools (General) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.