Jump to content

How do I create a variant which contains a byte array?


Recommended Posts

I'm trying to use MSCOMM32.OCX (or NETComm.ocx) to send a binary serial stream out from my RS-232 port. I've got the following program to work as desired:

$NetComm = ObjCreate("MSCommLib.MSComm")

With $NetComm

.CommPort = 5

.Settings = "38400,O,8,1"

.Handshaking = 0

.PortOpen = "True"

EndWith

$Test = CHR(32) & CHR(1) & Chr(33)

$NetComm.Output = $Test

Sleep(750)

$NetComm.PortOpen = "False"

$NetComm = 0

But if I change the 8th line to the following it fails:

$Test = CHR(32) & CHR(0) & CHR(33)

The error is reported as:

ThisWorks.au3 (8) : ==> The requested action with this object has failed.:

$NetComm.Output = CHR(32) & CHR(0) & Chr(33)

$NetComm.Output = CHR(32) & CHR(0) & Chr(33)^ ERROR

I need to send Chr(0), but the program fails if I try.

So I researched this forum and only discovered one other person who observed the same problem, but didn't report a solution. So I researched MSCOMM's Output Property. It says:

"The Output property can transmit text data or binary data. To send text data using the Output property, you must specify a Variant that contains a string. To send binary data, you must pass a variant which contains a byte array to the output property."

"$Test = CHR(32) & CHR(1) & Chr(33)" apparently creates a valid variant that contains a string. Trying to include a chr(0) apparently invalidates the string. It looks like I'll need to create a byte array.

How can I create a variant which contains a byte array (so I can include a chr(0))?

Any help is very much appreciated.

Link to comment
Share on other sites

I tried:

$NetComm = ObjCreate("MSCommLib.MSComm")

With $NetComm

.CommPort = 5

.Settings = "38400,O,8,1"

.Handshaking = 0

.PortOpen = "True"

EndWith

$a = DllStructCreate("byte[3]")

DllStructSetData($a,1,32,1)

DllStructSetData($a,1,1,2)

DllStructSetData($a,1,33,3)

$NetComm.Output = $a

$a = 0

Sleep(750)

$NetComm.PortOpen = "False"

$NetComm = 0

But I got the following error:

Doesnt Work.au3 (13) : ==> The requested action with this object has failed.:

$NetComm.Output = $a

$NetComm.Output = $a^ ERROR

While the structure appears OK, $NetComm.Output doesn't like $a.

I tried changing line 13 to $NetComm.Output = DllStructGetPtr($a) and got the same error.

I don't know what else to try.

Link to comment
Share on other sites

  • Administrators

Have a look at the beta. You can create pure binary byte data by doing something like

Binary("0x332244002233")

Where you use an 0x followed by the hex bytes you want to use (0-FF). Binary() forces this to be stored as byte data which can be used in the DllStruct calls.

Link to comment
Share on other sites

  • Administrators

Both those rely on the rewritten operation in the current beta I think. And if they don't work it may be you are passing the wrong data and nothing to do with binary data at all :shocked:

Link to comment
Share on other sites

I tried:

$NetComm = ObjCreate("MSCommLib.MSComm")

With $NetComm

.CommPort = 5

.Settings = "38400,O,8,1"

.Handshaking = 0

.PortOpen = "True"

EndWith

$a = DllStructCreate("byte[3]")

DllStructSetData($a,1,Binary("0x320133"))

$NetComm.Output = $a

$a = 0

Sleep(750)

$NetComm.PortOpen = "False"

$NetComm = 0

as well as:

$NetComm = ObjCreate("MSCommLib.MSComm")

With $NetComm

.CommPort = 5

.Settings = "38400,O,8,1"

.Handshaking = 0

.PortOpen = "True"

EndWith

$a = DllStructCreate("byte[3]")

DllStructSetData($a,1,"0x320133")

$NetComm.Output = $a

$a = 0

Sleep(750)

$NetComm.PortOpen = "False"

$NetComm = 0

Both received the same error:

Doesnt Work.au3 (10) : ==> The requested action with this object has failed.:

$NetComm.Output = $a

$NetComm.Output = $a^ ERROR

->AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 1.941

I also tried:

$NetComm = ObjCreate("MSCommLib.MSComm")

With $NetComm

.CommPort = 5

.Settings = "38400,O,8,1"

.Handshaking = 0

.PortOpen = "True"

EndWith

$a = DllStructCreate("byte[3]")

$b = Binary("0x320133")

DllStructSetData($a,1,$:)

$NetComm.Output = $a

$a = 0

Sleep(750)

$NetComm.PortOpen = "False"

$NetComm = 0

but got a similar error:

Doesnt Work.au3 (11) : ==> The requested action with this object has failed.:

$NetComm.Output = $a

$NetComm.Output = $a^ ERROR

->AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 1.942

A little more searching of the AutoIt forums seems to indicate that Byte Arrays can't be fully supported until SAFEARRAY support is added. Thanks for the help so far.....

Link to comment
Share on other sites

According to the references below, the Output property can send text data or binary data. Creating the text data is easy. Creating the binary data is the hard part.

"To send binary data, you must pass a Variant which contains a byte array to the Output property"

http://www.yes-tele.com/mscomm.html

http://www.tek-tips.com/faqs.cfm?fid=5775

http://www.picbasic.org/forum/archive/index.php/t-2302.html

Link to comment
Share on other sites

Sorry, I wasn't thinking. AutoIt can work with byte data but it may not have been implemented in the AutoIt to COM interface - that's probably where the problem is.

Would you maybe have to send a string consisting of the the concattenation of

$a = Chr(byte[0]) & Chr(byte[1]) .. ?

Edit:spelling

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

If I just wanted to send a sequence of characters/bytes which do NOT include a Chr(0), what you suggest should work.

The problem is that I want to send a Chr(0). [it's part of a sequence I need to control a video disk recorder.] But Chr(0) has a special meaning in a string as an end-of-string indicator. So I can never use Chr(0) as part of a string. This is probably why the developers of MSCommLib.MSComm created an alternative ability to use a byte array.

Based on Jon's feedback, the AutoIt - Com interface doesn't support byte arrays. So I can only hope that a future version/beta may include this at some future date.

Link to comment
Share on other sites

If I just wanted to send a sequence of characters/bytes which do NOT include a Chr(0), what you suggest should work.

The problem is that I want to send a Chr(0). [it's part of a sequence I need to control a video disk recorder.] But Chr(0) has a special meaning in a string as an end-of-string indicator. So I can never use Chr(0) as part of a string. This is probably why the developers of MSCommLib.MSComm created an alternative ability to use a byte array.

Based on Jon's feedback, the AutoIt - Com interface doesn't support byte arrays. So I can only hope that a future version/beta may include this at some future date.

I wrote a dll to work with Autoit to communicate over a serial port (including USB to Serial converters.) If your problem is not solved then I could make an option to read and write bytes, but at the moment it only reads and writes characters or strings. It is easy to use but you do need the dll. Can't promise I'd look at it straight away though.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I wrote a dll to work with Autoit to communicate over a serial port (including USB to Serial converters.) If your problem is not solved then I could make an option to read and write bytes, but at the moment it only reads and writes characters or strings. It is easy to use but you do need the dll. Can't promise I'd look at it straight away though.

I would appreciate any assistance. I'm sure there are other AutoIt users who may also find it useful. [i've read a number of messages from people looking for help on this subject and no real solutions.]

If helpful - I could offer to help document the functions your dll offers (assuming you're willing to post the file and you don't have a lot of time to document it yourself).

Thanks.

Link to comment
Share on other sites

I would appreciate any assistance. I'm sure there are other AutoIt users who may also find it useful. [i've read a number of messages from people looking for help on this subject and no real solutions.]

If helpful - I could offer to help document the functions your dll offers (assuming you're willing to post the file and you don't have a lot of time to document it yourself).

Thanks.

The dll is (V1.3 )

(Fixed ReadByte, SendByte, setting parity)

The UDF is (V1.2)(very lacking in comments)

A very simple script to show how to use some of the features

If you have problems send me a PM

Edit 1st April: updated all files with fixes (hopefully)

###########################################

EDIT; This dll and udf is now in example scripts here.

###########################################

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
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
 Share

  • Recently Browsing   0 members

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