weirddave Posted March 24, 2015 Share Posted March 24, 2015 (edited) Edit: hit return and it submitted before I had actually posed my question.... Edited March 24, 2015 by weirddave Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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 Link to comment Share on other sites More sharing options...
JohnOne Posted March 24, 2015 Share Posted March 24, 2015 I believe some reproduing code is required, rather than just one expression. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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 Link to comment Share on other sites More sharing options...
JohnOne Posted March 24, 2015 Share Posted March 24, 2015 That's a start, can you show visually what you get from the struct and what you expect/want from the struct? Basically, what do you mean by "normal binary bytes"? AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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. Link to comment Share on other sites More sharing options...
Solution funkey Posted March 24, 2015 Solution Share Posted March 24, 2015 #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 tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Geir1983 Posted March 24, 2015 Share Posted March 24, 2015 (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 March 24, 2015 by Geir1983 Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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... Link to comment Share on other sites More sharing options...
JohnOne Posted March 24, 2015 Share Posted March 24, 2015 (edited) I imagine you just open a file in binary mode, and write the array to it. Edited March 24, 2015 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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 Link to comment Share on other sites More sharing options...
Geir1983 Posted March 24, 2015 Share Posted March 24, 2015 remove the $FO_READ? Link to comment Share on other sites More sharing options...
JohnOne Posted March 24, 2015 Share Posted March 24, 2015 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. Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 Doesn't seem to make a difference. tried both suggestions Link to comment Share on other sites More sharing options...
Geir1983 Posted March 24, 2015 Share Posted March 24, 2015 you do call your other function that send data to that array? The example you posted will not have any data... Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 Hang on, I missed something in JohnOnes' code.... be right back Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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 Link to comment Share on other sites More sharing options...
Geir1983 Posted March 24, 2015 Share Posted March 24, 2015 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 Link to comment Share on other sites More sharing options...
jchd Posted March 24, 2015 Share Posted March 24, 2015 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
weirddave Posted March 24, 2015 Author Share Posted March 24, 2015 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) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now