esps
asked on
read networkstream
Hi Experts,
I get HEX data from a port as per sample:
Each occurance of 0x55 indicates start of string and each 0xAA indicates end of string.
You will notice that there are several occurances of such strings plus some other garbage as well.
I need to identify each such string within the stream and write it to a database.
Would appreciate help to get this done i.e. identify first string, store 'somewhere' continue and get next valid string.
I get HEX data from a port as per sample:
Each occurance of 0x55 indicates start of string and each 0xAA indicates end of string.
You will notice that there are several occurances of such strings plus some other garbage as well.
I need to identify each such string within the stream and write it to a database.
Would appreciate help to get this done i.e. identify first string, store 'somewhere' continue and get next valid string.
0x55 0x02 0x00 0x0A 0x00 0x01 0x00 0xE1 0xFF 0x00 0x00 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x79 0x06 0xAA 0x61 0x0D 0x0A 0x55 0x02 0x00 0x0A 0x00 0x01 0x00 0xD1 0xFF 0xBE 0x07 0x00 0x00 0xA1 0x5D 0x2C 0x00 0x00 0x00 0xF9 0xFF 0xFF 0xFF 0xC2 0x07 0xAA 0x00 0x0D 0x0A
0x55 0x02 0x00 0x02 0x00 0x01 0x00 0xD1 0x02 0x0B 0x1B 0x00 0x00 0xB8 0x5D 0xFF 0xFF 0xFF 0xFF 0xF9 0xFF 0xFF 0xFF 0x05 0x0A 0xAA 0x00 0x0D 0x0A 0x55 0x02 0x00 0x0B 0x00 0x01 0x00 0xE1 0xFF 0x00 0x00 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x7A 0x06 0xAA 0x61 0x0D 0x0A 0x55 0x02 0x00 0x02 0x00 0x01 0x00 0xD1 0x02 0x0B 0x1B 0x00 0x00 0xB8 0x5D 0xFF 0xFF 0xFF 0xFF 0xF9 0xFF 0xFF 0xFF 0x05 0x0A 0xAA 0x00 0x0D 0x0A 0x55 0x02 0x00 0x0B 0x00 0x01 0x00 0xE1 0xFF 0x00 0x00 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x61 0x7A 0x06 0xAA 0x61 0x0D 0x0A
Have you tried implementing a BinaryReader, and looking for the marker bytes?
ASKER
Hi TheLearnedOne,
Thank you for your response.
In the absence of a potential solution, I have in fact gone ahead and coded a rather primative solution by analysing the bytes coming in on the stream byte by byte.
Probably not the most effective method but it works, for now.
If you would like, I could share the coseand you could possibly suggest a better method.
Otherwise I will award a few point to you for your response and close
rgs
Thank you for your response.
In the absence of a potential solution, I have in fact gone ahead and coded a rather primative solution by analysing the bytes coming in on the stream byte by byte.
Probably not the most effective method but it works, for now.
If you would like, I could share the coseand you could possibly suggest a better method.
Otherwise I will award a few point to you for your response and close
rgs
I would love to see your attempt. There might be a better way to approach a solution.
ASKER
Note that sourcestring is the stream captured from the port and the positions of the Hex Data identified by the seperate functions represents different values which is then converted to decimal.
Function updateAll(ByVal SourceString As String)
Dim stringlen As Int32 = Len(sourceString)
Dim wordToFind1 As String = "0x55"
Dim wordToFind2 As String = "0xAA"
Dim tt1 As Int16
Dim tt2 As Int16
Dim v55 As String
Dim vAA As String
Dim locationInString1 As Int16
Dim locationInString2 As Int16
Dim switch As Int16 = 0
For i = 1 To stringlen
locationInString1 = InStr(i, sourceString, wordToFind1)
tt1 = locationInString1 + 2
v55 = Mid(sourceString, tt1, 2)
locationInString2 = InStr(i, sourceString, wordToFind2)
tt2 = locationInString2 + 2
vAA = Mid(sourceString, tt2, 2)
i = i + locationInString1
Dim val1 As Int64 = getAmps(sourceString, locationInString1)
Dim val2 As Int64 = getVolts(sourceString, locationInString1)
Dim val3 As Int64 = getReal(sourceString, locationInString1)
Dim val4 As Int64 = getApparent(sourceString, locationInString1)
Dim unit As Int64 = getUnit(sourceString, locationInString1)
updateGullserver(unit, RealPower, amps, volts, Apparent)
locationInString1 = 0
locationInString2 = 0
Next
End Function
Function val1(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 62, 2) & Mid(sourceString, vlocation + 57, 2) & Mid(sourceString, vlocation + 52, 2) & Mid(sourceString, vlocation + 47, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles .HexNumber )
Return value
End Function
Function val2(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 72, 2) & Mid(sourceString, vlocation + 67, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles .HexNumber )
Return value
End Function
Function val3(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 92, 2) & Mid(sourceString, vlocation + 87, 2) & Mid(sourceString, vlocation + 82, 2) & Mid(sourceString, vlocation + 77, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles .HexNumber )
Return value
End Function
Function val4(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 112, 2) & Mid(sourceString, vlocation + 107, 2) & Mid(sourceString, vlocation + 102, 2) & Mid(sourceString, vlocation + 97, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles .HexNumber )
Return value
End Function
Function getUnit(ByVal sourceString As String, ByVal vlocation As Int16)
Dim Amps As String = Mid(sourceString, vlocation + 22, 2) & Mid(sourceString, vlocation + 17, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles .HexNumber )
Return value
End Function
Function updateAll(ByVal SourceString As String)
Dim stringlen As Int32 = Len(sourceString)
Dim wordToFind1 As String = "0x55"
Dim wordToFind2 As String = "0xAA"
Dim tt1 As Int16
Dim tt2 As Int16
Dim v55 As String
Dim vAA As String
Dim locationInString1 As Int16
Dim locationInString2 As Int16
Dim switch As Int16 = 0
For i = 1 To stringlen
locationInString1 = InStr(i, sourceString, wordToFind1)
tt1 = locationInString1 + 2
v55 = Mid(sourceString, tt1, 2)
locationInString2 = InStr(i, sourceString, wordToFind2)
tt2 = locationInString2 + 2
vAA = Mid(sourceString, tt2, 2)
i = i + locationInString1
Dim val1 As Int64 = getAmps(sourceString, locationInString1)
Dim val2 As Int64 = getVolts(sourceString, locationInString1)
Dim val3 As Int64 = getReal(sourceString, locationInString1)
Dim val4 As Int64 = getApparent(sourceString, locationInString1)
Dim unit As Int64 = getUnit(sourceString, locationInString1)
updateGullserver(unit, RealPower, amps, volts, Apparent)
locationInString1 = 0
locationInString2 = 0
Next
End Function
Function val1(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 62, 2) & Mid(sourceString, vlocation + 57, 2) & Mid(sourceString, vlocation + 52, 2) & Mid(sourceString, vlocation + 47, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles
Return value
End Function
Function val2(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 72, 2) & Mid(sourceString, vlocation + 67, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles
Return value
End Function
Function val3(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 92, 2) & Mid(sourceString, vlocation + 87, 2) & Mid(sourceString, vlocation + 82, 2) & Mid(sourceString, vlocation + 77, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles
Return value
End Function
Function val4(ByVal sourceString As String, ByVal vlocation As Int16)
Dim TT As String = Mid(sourceString, vlocation + 112, 2) & Mid(sourceString, vlocation + 107, 2) & Mid(sourceString, vlocation + 102, 2) & Mid(sourceString, vlocation + 97, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles
Return value
End Function
Function getUnit(ByVal sourceString As String, ByVal vlocation As Int16)
Dim Amps As String = Mid(sourceString, vlocation + 22, 2) & Mid(sourceString, vlocation + 17, 2)
Dim value As Long = Long.Parse(TT, Globalization.NumberStyles
Return value
End Function
1) How many bytes would the typical length be?
2) Converting the entire byte array to a string, and then looking for bytes doesn't sound optimized.
3) There is an algorithm for strings (Boyer-Moore), that is optimized to 3n comparisons. It might work for byte arrays, too.
Boyer-Moore String Search Algorithm
http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
4) String.IndexOf() uses internally generated assembly code to more than likely use the Boyer-Moore algorithm.
Fast Text Search with Boyer-Moore
http://www.blackbeltcoder.com/Articles/algorithms/fast-text-search-with-boyer-moore
5) I wouldn't know if Instr is optimized, since it is included for backwards compatibility with VB6.
2) Converting the entire byte array to a string, and then looking for bytes doesn't sound optimized.
3) There is an algorithm for strings (Boyer-Moore), that is optimized to 3n comparisons. It might work for byte arrays, too.
Boyer-Moore String Search Algorithm
http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
4) String.IndexOf() uses internally generated assembly code to more than likely use the Boyer-Moore algorithm.
Fast Text Search with Boyer-Moore
http://www.blackbeltcoder.com/Articles/algorithms/fast-text-search-with-boyer-moore
5) I wouldn't know if Instr is optimized, since it is included for backwards compatibility with VB6.
ASKER
26 Bytes.
My problem is that I can never tell how many strings of 26 bytes plus some garbage in between wil be in the stream.
You have some good points though.
I'll do few tests with indexof as well.
Well ok, unless you have some more ideas, lets close this and I'll award the point to your last response.
Thanks again!
My problem is that I can never tell how many strings of 26 bytes plus some garbage in between wil be in the stream.
You have some good points though.
I'll do few tests with indexof as well.
Well ok, unless you have some more ideas, lets close this and I'll award the point to your last response.
Thanks again!
I mean how many bytes for the entire array?
ASKER
Dim inStream(10024) As Byte
A valid string is:
0x55 0x02 0x00 0x02 0x00 0x01 0x00 0xD1 0x02 0x0B 0x1B 0x00 0x00 0xB8 0x5D 0xFF 0xFF 0xFF 0xFF 0xF9 0xFF 0xFF 0xFF 0x05 0x0A 0xAA
with ox55 = beginning of string and 0xAA end of string.
A valid string is:
0x55 0x02 0x00 0x02 0x00 0x01 0x00 0xD1 0x02 0x0B 0x1B 0x00 0x00 0xB8 0x5D 0xFF 0xFF 0xFF 0xFF 0xF9 0xFF 0xFF 0xFF 0x05 0x0A 0xAA
with ox55 = beginning of string and 0xAA end of string.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good discussion