Sascha B

Sascha B

  • NA
  • 237
  • 0

How to check the recieved code ?

Sep 9 2010 4:55 AM
Hello again :)
I noticed that my device sends me sonmetimes wrong codes...
I made thousands measurement and there are about 5% wrong codes.
Here is how a right code has to look like:
02 30 30 30 31 30 20 33 30 2B 31 32 32 34 34 2B 31 32 34 35 34 2B 31 30 31 33 34 03 30 41 0D 0A 
02 30 31 30 31 30 20 32 30 2B 33 34 32 31 33 2B 33 31 31 32 33 2B 32 31 36 38 33 03 30 31 0D 0A
02 30 32 30 31 34 36 31 30 3D 20 20 30 30 33 3D 20 20 30 30 33 3D 20 20 30 30 33 03 30 44 0D 0A
Normallly it's a long string but a made better looking for you.
So there are 3 parts (every line) and every part starts with a "02" and ends with "0D 0A"

And now some wrong codes:
02 30 30 30 31 30 20 33 30 2B 31 31 38 37 34 2B 31 32 30 36 34 2B 20 39 37 36 34 03 31 43 0D 0A 
3F 3F 3F 3F 3F 3F 5A 1A 3F 23 43 3F 43 5A 3F 43 43 3F 5A 3F 3F 3F 3F 1A 3F 3F 12 3F 3F 7E 02 30
32 30 31 34 36 31 30 3D 20 20 30 30 33 3D 20 20 30 30 33 3D 20 20 30 30 33 03 30 44 0D 0A

That would be easy to check because of the lenght but what can I do when I get a code like this?
02 30 30 30 31 30 20 33 30 2B 31 31 37 37 34 2B 31 31 39 35 34 2B 20 39 35 38 34 03 31 36 0D 0A 
3F 3F 3F 3F 3F 5A 1A 2A 3F 43 3F 31 5A 1A 3F 0B 3F 1A 5A 3F 0B 43 3F 1A 3F 3F 42 3F 3F 5A 3F 3F
02 30 32 30 31 34 36 31 30 3D 20 20 30 30 33 3D 20 20 30 30 33 3D 20 20 30 30 33 03 30 44 0D 0A
Right lenght but in the middle of the string is the wrong part...

Can I check something like:
Contains the string 3 times the string "0D 0A"?
I know the contains function but can I combine it with a count function?
Would be awesome if you could give me hint :)

Edit: Regular Expressions has gone trough my mind and this came out:
 public bool IsValid(string code)
        {
            string pattern = @"([0][2][ 0-9A-F]*[0][D][ ][0][A])([ ][0][2][ 0-9A-F]*[0][D][ ][0][A])([ ][0][2][ 0-9A-F]*[0][D][ ][0][A])";
            Regex RegEx = new Regex(pattern);
            return RegEx.IsMatch(code);
        }
so it gives true back if the code is right :)
Can be closed.

Answers (2)