weirddave

ill a byte array from DllStructGetData($lpBuffer, 1)

22 posts in this topic

#1 ·  Posted (edited)

Edit:
hit return and it submitted before I had actually posed my question....

Edited by weirddave

Share this post


Link to post
Share on other sites



I'm trying to fill a byte array from DllStructGetData($lpBuffer, 1)

I can't figure out how to do it, the code I have is currently:
$data=DllStructGetData($lpBuffer, 1)
which seems to create a string starting with 0x then the rest is in hex.

While I have made my code work by using string manipulation to get the data I want, I could really do with this as normal binary bytes.

Can anyone point me in the right direction?

Cheers,

Dave

Share this post


Link to post
Share on other sites

Some example code:

 
$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
$data=DllStructGetData($lpBuffer, 1)
msgbox (0,"",$data)
 
What I want is MyArray[] filled with binary data

Share this post


Link to post
Share on other sites

This is what I get:

0x0000000000000000000000000000000000000000001FF100011800002E801F00F36400000104001C000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000403040300000000FFFFFFFF001600008000000000003000000000000000426F1DD701007988AF6C0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020

I used FileWrite($hFileOpen, $data) to grab the $data

The result is that $data is a string 514 chars long, what I would like is an array[] 256 bytes long containing the $data

At the moment I'm doing a lot of

Dec(StringMid($data,($ByteNum*2)+3,2))

to interrogate the $data. An array[] of bytes would be a lot simpler, but I can't see a simple way to do it.

Share this post


Link to post
Share on other sites

#include <Array.au3>

$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
$data=DllStructGetData($lpBuffer, 1)

;~ msgbox (0,"",$data)

$MyArray = _BinaryToArray($data)
_ArrayDisplay($MyArray)

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Why not just enter data to a normal array directly (from your serial port) instead of putting it in a dllstruct and then convert it to an autoit array?

Edited by Geir1983

Share this post


Link to post
Share on other sites

I'm not accessing a COM port, it's a USB to RS232 device, controlled via FTD2XX.dll_UDF

Now I just need to work out how to save the data... :)

Share this post


Link to post
Share on other sites

I did try that, but I got a 0 byte file

#include <Array.au3>
#include <File.au3>
$lpBuffer = DllStructCreate('BYTE[256]')
$data=DllStructGetData($lpBuffer, 1)
$MyFile = "C:\AutoIt\a.bin"
_FileCreate($MyFile)
$hFileOpen = FileOpen($MyFile, BitOr($FO_READ, $FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY ))
$MyArray = _BinaryToArray($data)
FileWrite($hFileOpen, $MyArray)
FileClose($hFileOpen)

;_ArrayDisplay($MyArray)

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc

Share this post


Link to post
Share on other sites

remove the $FO_READ?

Share this post


Link to post
Share on other sites

Might be something like...

#include <Array.au3>

$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
$data=DllStructGetData($lpBuffer, 1)

;~ msgbox (0,"",$data)

$MyArray = _BinaryToArray($data)
_ArrayDisplay($MyArray)
_BinaryArrayToFile($MyArray)

Func _BinaryArrayToFile(ByRef $array)
    Local $hFile = FileOpen("file.bin", 16 + 2) ;Binary, Overwite
    _FileWriteFromArray($hFile, $array)
    FileClose($hFile)
EndFunc

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Doesn't seem to make a difference. tried both suggestions :(

Share this post


Link to post
Share on other sites

you do call your other function that send data to that array? The example you posted will not have any data...

Share this post


Link to post
Share on other sites

Hang on, I missed something in JohnOnes' code.... be right back :D

Share this post


Link to post
Share on other sites

OK, that result is pretty bad for me, I get a file 1536 bytes long, it contains 256 text lines of:
0x00

I'm after a 256 byte file which notepad doesn't understand :)

Share this post


Link to post
Share on other sites

Something like this? I think the empty array in autoit is a problem, it has no datatype..

#include <Array.au3>
#include <File.au3>
$lpBuffer = DllStructCreate('BYTE[256]')
$data=DllStructGetData($lpBuffer, 1)
$MyFile = "C:\a.bin"
_FileCreate($MyFile)
$hFileOpen = FileOpen($MyFile, BitOr($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY ))
$MyArray = _BinaryToArray($data)
FileWrite($hFileOpen, DllStructGetData($lpBuffer, 1))
FileClose($hFileOpen)

;_ArrayDisplay($MyArray)

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    ConsoleWrite("Count := " &$iCount&@CRLF)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc

Share this post


Link to post
Share on other sites

Why going through an array is beyond me:

#include <File.au3>
$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
; sample data
For $i = 1 To 256
    DllStructSetData($lpBuffer, 1, $i - 1, $i)
Next
Local $hOut = FileOpen("data.bin", BitOr($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY))
$data=DllStructGetData($lpBuffer, 1)
FileWrite($hOut, $data)
FileClose($hOut)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

That does the trick. I was originally saving $data, but I had a timestamp in there as well which was causing $data to get converted to a string :(

jchd, the array is there to solve a different problem (easier interrogation of the data than the method I had)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now