Find string but 1 character at a time

mmitchell

Registered User.
Local time
Today, 13:31
Joined
Jan 7, 2003
Messages
80
I am doing this the "brute force way", so I am looking for a better way.

I am intteracting with another program that is getting an ASCII text file via the serial port. I can't change how I get the data, it will be given to me one character at a time, I make a call to its function and it returns one character, then I do something with it then I make another call to it, etc., etc.


I need to know when a specific string comes in the serial port, but since I am getting the characters one character at a time, I "think" I have to "assemble" them first (at least that is what I am doing now).

What I am doing now is keeping track of certain flags, so when the first known character comes in I set a flag, then if I see the next known character I set another flag.

When all of my flags are set then I know I have the string I want then I can run the rest of my code.

My issue is that I am starting to do this all the time and I have to keep re-writing my code depending on what specific string I am looking for.

1. I need a better, more efficient way of writting my code so that I can just plug in the known string as a variable instead of hard coding each letter of it.

2. Is there a better way then setting all these flags?

Anyone have a good idea?
 
You could make a table of the "Known" Strings. Then after getting each new character you can assemble the characters and check to see if it holds the "Known" string.

This method would allow you to add a new "Known" String without having to recode.

Example
Code:
Dim RST as ADODB.Recordset
Dim Done as Boolean
Dim stString as String
Set RST= New ADODB.Recordset

RST.Open "Select [Known String] From [Table Known Strings]", CurrentProject.Connection,dbStatic,dbReadOnly
Done = False

  Do
    'Code to get String
    'When last character is retrieved Set Done = True
    'Set stString = stString & "NEW CHARACTER"
    With RST
        .MoveFirst
        Do While Not .EOF
           If Instr(1,stString,![Known String]) then
               'A Known String is Found
               Exit Loop
           End if
           .MoveNext
        Loop
    End With

  Loop While Done = False
 
But I still have the issue of how best to "put together" the characters as they come in one at a time.

Or am I making that harder then it should be?
 
Mitch,

I don't know what characters signify the start/end of
a transmission (I used ASCII 33), but you could do something
like this:


Dim strIn As String
Dim strBuffer As String
Dim flgStatus As Boolean

strBuffer = ""
flgStatus = False
While (flgStatus)
strIn = GetCharacter() 'Your function to read serially
If (strIn = Chr(33)) Then
flgStatus = True
End If
Wend

flgStatus = False
While (flgStatus)
strIn = GetCharacter() 'Your function to read serially
If (strIn = Chr(33)) Then
flgStatus = True
Else
strBuffer = strBuffer & strIn
End If
Wend

' Execute Travis's Code


hth,
Wayne
 

Users who are viewing this thread

Back
Top Bottom