IphoneDev Posted March 17, 2010 Posted March 17, 2010 (edited) Hi, I wondered if there was some command in autoit like the Convert.ToByte for C#. Need to output something to a serial device through the COM port already got that working but need to send it as a Byte value. Thanks! Edited March 17, 2010 by IphoneDev
Fulano Posted March 17, 2010 Posted March 17, 2010 (edited) Something like StringToBinary?Looked at the C# docs, and Convert.ToByte did not do what I thought it did. Edited March 17, 2010 by Fulano #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
IphoneDev Posted March 17, 2010 Author Posted March 17, 2010 (edited) I don't think thats it :s This is how I do it in C# serialPort1.Write(new byte[] { Convert.ToByte(string) }, 0, 1); Edited March 17, 2010 by IphoneDev
IphoneDev Posted March 17, 2010 Author Posted March 17, 2010 (edited) AutoIt outputs this 0x39332E36373239383231313433363732 This is my AutoIt code: #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=n #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <CommMG.au3> ;or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll' #include <String.au3> $error = "" $rand = Random(0, 100) $rand = StringToBinary($rand) ConsoleWrite($rand & @CR) _CommSetPort(3, $error, 9600, 8, 0, 1, 0) _CommSendString($rand, 8) _CommCloseport() Edited March 17, 2010 by IphoneDev
Fulano Posted March 17, 2010 Posted March 17, 2010 Because AutoIt is a weakly typed language conversions are usually handled in the background. To get this kind of conversion you may need to try calling Convert.ToByte directly - something like they were doing here with the VB Dictionary object. I'm short on time right now or I'd test it myself. If the question is still open when I get back to my windows box on Monday then I'll play around with it and see what I can find out. #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
jvanegmond Posted March 17, 2010 Posted March 17, 2010 _CommSendString accepts string as input, you don't have to manually convert the string into a byte array. Your code becomes: #include <CommMG.au3> ;or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll' #include <String.au3> $error = "" $rand = Random(0, 100) ConsoleWrite($rand & @CR) _CommSetPort(3, $error, 9600, 8, 0, 1, 0) _CommSendString($rand, 8) _CommCloseport() See if this works. github.com/jvanegmond
trancexx Posted March 17, 2010 Posted March 17, 2010 So the title should have been: "Convert String to Byte Array". Thanks Manadar. ♡♡♡ . eMyvnE
IphoneDev Posted March 17, 2010 Author Posted March 17, 2010 Already tried that, it did not work. Just to assure myself I tried again, still no luck.
trancexx Posted March 17, 2010 Posted March 17, 2010 Is there any error/exception/whatever handling in C#? ♡♡♡ . eMyvnE
IphoneDev Posted March 18, 2010 Author Posted March 18, 2010 Yes, atleast when its only running in the IDE. Still trying to find out how to convert a string to byte array, no luck so far. Anyone else?
trancexx Posted March 18, 2010 Posted March 18, 2010 Yes, atleast when its only running in the IDE.There is in AutoIt too. Use it. Check errors. I'm sure martin is setting @errors properly.Still trying to find out how to convert a string to byte array, no luck so far.Anyone else?You really make no sense now. ♡♡♡ . eMyvnE
Fulano Posted March 22, 2010 Posted March 22, 2010 Correct me if I'm wrong, but looking at the documentation here it looks like you are looking for a simple String->Ascii Array conversion.If that's the case, there's a function for that. #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
tukangusil7 Posted April 26, 2018 Posted April 26, 2018 (edited) By coincidence, I recently dealt with this issue. To copy a string to an array bytes, just use a StringToBinary() in the third parameter of DllStructSetData(). #include <StringConstants.au3> Opt("MustDeclareVars", 1) Local Const $ciSizeOfWChar = 2 ; Copy $sText to an array of bytes, which is the first element of $taiData struct Local $taiData = DllStructCreate("BYTE[256]") Local $sText = "Ani text" DllStructSetData($taiData, 1, StringToBinary($sText, $SB_UTF16LE)) ; Replace the third character in the array of bytes, which is "i", to "y", ; thus "Ani text" becomes "Any text". Local $piData = DllStructGetPtr($taiData, 1) ; Pointer to the BYTE[0] Local $tchChar = DllStructCreate("WCHAR", $piData + (3 - 1) * $ciSizeOfWChar) DllStructSetData($tchChar, 1, "y") ; Display the result Local $bvResult = DllStructGetData($taiData, 1) ; We get a binary variant here Local $sResult = BinaryToString($bvResult, $SB_UTF16LE) ; Convert the binary variant to string MsgBox(0, "", $sResult) Good luck. Edited April 26, 2018 by tukangusil7
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