This is my packet builder class, written in REALbasic. I hope this helps anybody who wants to use it. I should get my packet buffer posted soon..
Adding a byte:
Sub InsertByte(data as integer)
subpacket = subpacket + ChrB(Data)
end sub
Adding a DWORD:
Sub InsertDWORD(data as string)
subpacket = subpacket + MakeDWORD(data)
End Sub
Making a DWORD:
Protected Function MakeDWORD(data as string) as string
Dim tmp as String
Dim a as String
Dim B as String
Dim c as String
Dim D as String
DIM varMakeDWORD as String
tmp = Right(chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + tohex(Data), 8)
a = Mid(tmp, 1, 2)
B = Mid(tmp, 3, 2)
c = Mid(tmp, 5, 2)
D = Mid(tmp, 7, 2)
varMakeDWORD=Chr(Val("&H" + D))
varMakeDWORD= varMakeDWORD + Chr(Val("&H" + c))
varMakeDWORD= varMakeDWORD + Chr(Val("&H" + B))
varMakeDWORD= varMakeDWORD + Chr(Val("&H" + a))
Return varMakeDWORD
End Function
Insert a string:
Sub InsertString(data as string, NT as boolean)
Select case NT
case true
subpacket = subpacket + data + chr(0)
case false
subpacket = subpacket + data
end Select
End Sub
Insert a word:
Sub InsertWORD(data as integer)
subpacket = subpacket + MakeWord(data)
End Sub
Make a word:
Protected Function MakeWORD(data as integer) as string
Dim tmp as String
Dim a as String
Dim B as String
DIM varMakeWORD as String
tmp = Right(chr(0) + chr(0) + chr(0) + chr(0) + hex(Data), 4)
a = Mid(tmp, 1, 2)
B = Mid(tmp, 3, 2)
varMakeWORD=Chr( Val("&H" + B) )
varMakeWORD= varMakeWORD + Chr( Val("&H" + a) )
Return varMakeWORD
End Function
To hex:
Private Function ToHex(data as string) as string
Dim i as Integer
DIM varToHex as String
For i = 1 To Len(Data)
varToHex= varToHex + Right("00" + hex(Asc(Mid(Data, i, 1))), 2)
Next
Return varToHex
End Function
Applying BNCS headers:
Sub ApplyHeaders(MessageID as integer)
headers = chr(&HFF) + chr(MessageID)
headers = headers + MakeWord(LenB(subpacket) + lenB(Headers) + 2)
subpacket = headers + subpacket
End Sub
Applying BNLS headers:
Sub ApplyHeadersBNLS(MessageID as integer)
headers = MakeWord(lenb(subpacket) + 3) // Include the WORD and the message id byte
headers = headers + chr(MessageID)
subpacket = headers + subpacket
End Sub
Getting the packet to write out:
Function GetPacket() as string
return subpacket
End Function
You will need the following properties:
headers as string (Public)
subpacket as string (Public)
I hope these help!