Jump to content

Recommended Posts

Posted (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 by IphoneDev
Posted (edited)

Something like StringToBinary?

Looked at the C# docs, and Convert.ToByte did not do what I thought it did.

Edited 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!

Posted (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 by IphoneDev
Posted (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 by IphoneDev
Posted

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!

Posted

_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.

Posted

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

Posted

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!

  • 8 years later...
Posted (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 by tukangusil7

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...