Jump to content

Sending HEX to Com Port


Recommended Posts

#include<CommMG.au3>

$iPort = "COM1"

$iBaud = 19200

$iBits = 8

$iPar = 0

$iStop = 1

$iFlow = 0

$sErr = "0x02"

_CommSetPort($iPort, $sErr, $iBaud, $iBits, $iPar, $iStop, $iFlow)

_CommPortConnection()

$iWaitComplete = 0

$sMGString = "0xbe,0xef,0x10,0x05,0x00,0xc6,0xff,0x11,0x11,0x01,0x00,0x01"

_CommSendString("{Asc 0xbe,0xef,0x10,0x05,0x00,0xc6,0xff,0x11,0x11,0x01,0x00,0x01}" & @CR )

_CommClosePort()

I have made a serial to RS232 cable. Which I know works and I can turn it on and off by a batch file that someone made that sent data to the port.

http://creosity.com/web/files/rs232.pdf

Shows on page 3 which hex values I have to send to get it to do what I want.

Some reason when I run the program it seems to crash AutoIT. Any help would be great to get me headed in the right direction...

Link to comment
Share on other sites

#include<CommMG.au3>

$iPort = "COM1"

$iBaud = 19200

$iBits = 8

$iPar = 0

$iStop = 1

$iFlow = 0

$sErr = "0x02"

_CommSetPort($iPort, $sErr, $iBaud, $iBits, $iPar, $iStop, $iFlow)

_CommPortConnection()

$iWaitComplete = 0

$sMGString = "0xbe,0xef,0x10,0x05,0x00,0xc6,0xff,0x11,0x11,0x01,0x00,0x01"

_CommSendString("{Asc 0xbe,0xef,0x10,0x05,0x00,0xc6,0xff,0x11,0x11,0x01,0x00,0x01}" & @CR )

_CommClosePort()

I have made a serial to RS232 cable. Which I know works and I can turn it on and off by a batch file that someone made that sent data to the port.

http://creosity.com/web/files/rs232.pdf

Shows on page 3 which hex values I have to send to get it to do what I want.

Some reason when I run the program it seems to crash AutoIT. Any help would be great to get me headed in the right direction...

The second parameter in _CommSetPort is for the function to use for an error message. It is set to an empty string at the start of the function so setting it to "0x02" or anything else will have no effect. The intention is that you check to see if the function returns 1 meaning successful, or 0 meaning there was a problem. If there was a problem the value of @error will tell you more as will the error message.

_CommPortConnection is not doing anything in your code. By seeing what the return is you can tell which COM port is connected to the currently selected channel. The channel will be 1 if you don't actually specify one, and if the port has been set correctly by the previous line then the return should be "COM1".

_CommSendString is for sending a string of course which you are doing. But from the link you gave you should be sending hex codes I think and I don't see where you got the "{" and "}" from.

To send hex codes, ie bytes, you can only use _CommSendString after converting the codes to the the ASCII characters the codes would represent. This is not possible with 0x0 because that means the end of a string, so in general if you need to send bytes or hex codes you should use one of the binary functions _CommSendByte or _CommSendByteArray.

You need to add some error checking to your code first to see what is going wrong I think because I wouldn't expect AutoIt to crash with what you showed.

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