Hi All,
I got a strange one im trying to sort out. I have a class called clsPacket which writes to a byte array in various formats (string, int etc) and Im coming up against a strange problem. I've used simlair code before and not recieved this problem and it is confusing me quite a bit now.
The problem I am getting is I write two strings to the array and try and then to test they are correct (this is a test app I made) I first look at the Log I created and then use the built in packet functions I made to display the results in a message box. They return two different results which are both wrong. I'll include all the necersary code for if someone wants to see this for themselves.
clsPacket - Packet Class
vb Code:
Public Class clsPacket Private _Data As New List(Of Byte) Public ReadOnly Property Size() As Integer Get Return _Data.Count - 1 End Get End Property Public ReadOnly Property GetPoint(ByVal Index As Integer) As Byte Get Return _Data.Item(Index) End Get End Property Public ReadOnly Property GetHeader() As Byte Get Return _Data.Item(0) End Get End Property Public ReadOnly Property GetPacket() As Byte() Get _Data.TrimExcess() Dim TempByte(_Data.Count - 1) As Byte For TempInt As Integer = 0 To _Data.Count - 1 TempByte(TempInt) = _Data.Item(TempInt) Next Return TempByte End Get End Property Public Sub RemovePortion(ByVal Amount As Integer) _Data.RemoveRange(0, Amount - 1) _Data.TrimExcess() End Sub Public Sub ClearPacket() _Data.Clear() End Sub Public Sub DumpPacket(ByVal NewData() As Byte) _Data.Clear() For TempInt As Integer = 0 To NewData.Length - 1 _Data.Add(NewData(TempInt)) Next End Sub Public Function ReadASCIIStringUntilNull() As String Dim TempString As String = "", TempInt As Integer For TempInt = 0 To Size If _Data.Item(TempInt) <> 0 Then TempString = TempString & Chr(_Data.Item(TempInt)) Else Exit For End If Next TempInt RemovePortion(TempInt) Return TempString End Function Public Sub WriteASCIINull(ByVal TempString As String) For TempInt As Integer = 0 To Len(TempString) - 1 _Data.Add(Asc(TempString)) TempString = Right(TempString, Len(TempString) - 1) Next TempInt _Data.Add(&H0) End Sub End Class
Main Form
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim test As New clsPacket test.WriteASCIINull("test") test.WriteASCIINull("test2") Dim Log As String Log = vbCrLf & "Packet (0x" & Hex(test.GetPoint(0)) & ")" & vbCrLf Log += "Time/Date - " & TimeOfDay() & " / " & Now.Date() & vbCrLf Log += "Packet - " For i As Integer = 0 To test.Size Log += Hex(test.GetPoint(i)) & " " Next i Log += vbCrLf & "ASCII - " For i As Integer = 0 To test.Size Log += Chr(test.GetPoint(i)) & " " Next Log += vbCrLf & "Length - " & test.Size & vbCrLf MsgBox(Log) MsgBox(test.ReadASCIIStringUntilNull & " " & test.ReadASCIIStringUntilNull) End Sub
If you run the code you will see that from the first message box you get to see the raw packet which shows the words (in their ascii codes). but underneath you only see one word. Then on the second messagebox which uses the packets own function to show the words, you see one word, a space then the first letter of the second word. I have no clue why this is happening, any ideas?




Reply With Quote