06-24-2011 04:06 PM
Hi
I have been trying for a while now to search for a word that is being written to a file.
I was wondering if anyone can get me over this hump?
What I am doing is reading from the serial port to a file (example file called test), and while this is being done I want to search for the word "passed" or the phrase "test passed"
Once I fine this word or phrase, I can stop the test
I cannot figure out how to search the file, I used the scan file VI and the search/ replace VI etc with no luck!
Your suggestions is appreciated
06-24-2011 04:14 PM
Scan From File is typically used to read in formatted data from a file, not necessarily for arbitrary text. Match Pattern or Search/Split String can be used to search a string.
06-24-2011 09:19 PM
It's always best to post your code so others can see the work you've done. Suggestions will be more effective that way.
So your reading from a serial port to a file? What do you mean by this? Are you reading from the serial port and writing the data to a file? It's the file you want to read, isn't it? If I had your code I could answer my own questions
Why don't you just monitor the incoming data stream from the serial port for "passed."
But to read from a file and search for a string, Smercurio is correct. Match Pattern will do what you need (You can use this same function for ASCII serial data as well).
06-24-2011 10:39 PM
If you really do have characters coming in one at a time, you can do some interesting stuff, depending on the string you want to find. I'm going to take an artifically "simple" example to make my algorithm "artificially simple". Suppose you want to stop when you see the string End! (that's a four-character string). Start your program off testing the incoming character for "E" -- if it isn't an E, then it clearly isn't the beginning of your target. When you get an "E", you need to change the test to look for an "n" -- if it isn't an "n", it again isn't your target. Note you have to go back and restart the test with E because you might have the string "EEnd!". A State Machine (with a state for matching "E", "n", "d", and "!") should do the trick. Note that once you find one match, finding a mismatch means "start over".
The algorithm becomes slightly more complicated if you have repeated letters. However, the nice thing about this algorithm is you only have to pay attention to the current letter -- no "string matching" is required.
Bob Schor
06-24-2011 10:47 PM
This seems overly complicated. String matching is a much simplier design and you can view the entire string/buffer in one scan, rather than dealing with multiple cases. Your algorithm will certainly work, but I prefer the easy way.
06-24-2011 11:14 PM
@MoReese wrote:
This seems overly complicated. String matching is a much simplier design and you can view the entire string/buffer in one scan, rather than dealing with multiple cases. Your algorithm will certainly work, but I prefer the easy way.
You are certainly correct, but in order to search the "entire string" in one scan, you need to read the "entire" string. If the object is to stop when the final character of the "Stop String" is read, you have to do something like "Read a character, append it to a string, search the resulting string, and repeat". Granted, if you do it right and keep a "search buffer" exactly as long as your target, adding the just-read charcter to the end after deleting the oldest character, you can just search for the target. So in my example, you'd have a 4-character "test string". As you read each character, you'd delete the first character of the test string, append the just-read character to the end, and test the string for a match to the target. Hmm, you're right, this is simpler ...
06-27-2011 10:51 AM
Hi There
Thanks for all the responses, I will continue to work on it with everyones suggestions
Here is the code maybe someone can come up with a faster solution with this VI