[Gambas-user] gb3: writing variables to a string using a memory stream and pointer
Kevin Fishburne
kevinfishburne at ...1887...
Tue Sep 13 04:17:38 CEST 2011
On 09/12/2011 09:19 PM, Benoît Minisini wrote:
>>> On Sun, Sep 11, 2011 at 07:40, Kevin Fishburne<
>>>
>>> kevinfishburne at ...1887...> wrote:
>>>> My code looks like this:
>>>>
>>>> ' For writing outgoing UDP data to memory.
>>>> Public data As String
>>>> Public mem As Stream
>>>>
>>>> ' Create data string for outgoing transaction queue.
>>>> data = Space(8)
>>>> mem = Memory VarPtr(data) For Write
>>>> mem.Begin
>>>>
>>>> Write #mem, (Server.DateCurrent + Server.DateUTC) As Float
>>>>
>>>> mem.Send
>>>> Print data
>>>>
>>>> It throws signal 11 on the Print statement.
>>>>
>>>> I have two questions. First is this the correct way to write one or more
>>>> variables to a string in memory, and second what's up with the signal
>>>> 11? I'm using revision 4094 and will update to the newest revision in a
>>>> moment.
>>
>> On 09/11/2011 08:13 AM, Jussi Lahtinen wrote:
>>> Not sure what happen there, but try this:
>>>
>>> Public pData As Pointer
>>> Public mem As Stream
>>>
>>> pData = Alloc(SizeOf(gb.Float))
>>> mem = Memory pData For Write
>>> mem.Begin
>>>
>>> Write #mem, (Server.DateCurrent + Server.DateUTC) As Float
>>>
>>> mem.Send
>>>
>>> Print Float@(pData)
>>
>> Thanks everyone for your replies. I'm now using this code ('Data' is a
>> string):
>>
>> ' Create data string.
>> DataPointer = Alloc(8)
>> Mem = Memory DataPointer For Read Write
>> Mem.Begin
>> Write #Mem, (Server.DateCurrent + Server.DateUTC) As Float
>> Mem.Send
>> 'Data = Read #Mem As Float
>> Data = Read #Mem, 8
>>
>> ' Send transaction.
>> udp.Begin
>> Write #udp, id As Byte
>> Write #udp, 56 As Byte
>> 'Write #udp, (Server.DateCurrent + Server.DateUTC) As Float
>> Write #udp, Data, Len(Data)
>> udp.Send
>>
>> The recipient gets the wrong value for 'Data' when it is converted back
>> from a string to a float. It gets the correct value if I use the
>> commented "Write #udp, (Server.DateCurrent + Server.DateUTC) As Float".
>> Any ideas why I can't write the data stored at DataPointer to the string
>> Data?
>>
>> In case anyone forgot, I need the data to be saved as a string so it can
>> be stored in an outgoing network transaction queue, which is an array of
>> transaction structures. The Data property of the structure stores
>> arbitrary groups of variables so grouping them as a string is the most
>> convenient method since there will be up to 255 different transaction
>> types.
>
> 1) Using Begin/Send is useless if you have only one WRITE instruction.
>
> 2) Every stream has a byte order. Did you check that the byte order of your
> memory stream is the same as the byte order of your Udp socket stream?
I corrected the Begin/Send issue, thanks.
I'd tried setting the byte order both ways before reading the stream and
still had incorrect results. The client receiving the transaction
reverses the byte order, which produces the correct results when sending
the float directly. I tried altering the client code so that it did not
reverse the byte order and still received incorrect results. I changed
the code to look like this:
' Create data string.
DataPointer = Alloc(8)
Mem = Memory DataPointer For Read Write
Write #Mem, (Server.DateCurrent + Server.DateUTC) As Float
Data = Read #Mem As Float
Print "Original: " & (Server.DateCurrent + Server.DateUTC)
Print "From Mem: " & Float@(Data)
Print "Reversed: " & Float@(Convert.Reverse(Data))
and got these results:
Original: 197339.760373332
From Mem: 1.48440633070182E-76
Reversed: 1.4276114952676E-71
Convert.Reverse returns the string sent to it in reversed order ('abc'
becomes 'cba'). All of that is independent of UDP or the client.
--
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271
More information about the User
mailing list