Brian39

Send hex string to COM port

12 posts in this topic

Hi,

I could really use some help on this.

I can send one hex number (41) with the code below. But I dont know how to send a string ec 0x41, 0x42, 0x42, 0x41, 0x41, 0x42, 0x41, 0x42.

Any help would be much appresiated

;Include the Serial UDF
#include 'CommMG.au3'

;Internal for the Serial UDF
Global $sportSetError = ''

;COM Vars
;_CommSetPort($iPort, ByRef $sErr, $iBaud = 9600, $iBits = 8, $iPar = 0, $iStop = 1, $iFlow = 0, $RTSMode = 0, $DTRMode = 0)
Global $CMPort = 3              ; Port
Global $CmBoBaud = 19200        ; Baud
Global $CmboDataBits =  8       ; Data Bits
Global $CmBoParity = "none"     ; Parity
Global $CmBoStop = 1            ; Stop
Global $setflow = 2             ; Flow
Global $RTSMode = 1
Global $DTRMode = 1
Global $iWait = 0

;Start up communication
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

$byte = ("0x41")

_CommSendByte($byte, $iWait)

Share this post


Link to post
Share on other sites



make it a function and pass the values to send one at a time


1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

_CommSendString

OK, I have tried the following:

$data = ("4D 42")
_CommSendString ($data, $iWait)

But my portmonitor reads: 34 44 20 34 32

It should read: 4D 42

What am I doing wrong?

Share this post


Link to post
Share on other sites

If you look at this chart and look at the Hex values for the characters "4", "D", " ", "4", and "2" I think you will see what your port monitor is showing you is being sent.

You're not sending the characters as Hex values, you're sending them as ASCII characters.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

If you look at this chart and look at the Hex values for the characters "4", "D", " ", "4", and "2" I think you will see what your port monitor is showing you is being sent.

You're not sending the characters as Hex values, you're sending them as ASCII characters.

Yes I noticed.

Then I need to change CommMG.au3? Or can I correct it in my script?

Sorry, I'am quite a newbi at this.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Brian39

use _CommSendByteArray()

it's all in Martins thread

Serial port (COM port) udf includes ability to send/receive binary data

http://www.autoitscript.com/forum/topic/45842-serial-port-com-port-udf/

Edit: forgot to set port back to 3

;Include the Serial UDF
#include 'CommMG.au3'

;Internal for the Serial UDF
Global $sportSetError = ''

;COM Vars
;_CommSetPort($iPort, ByRef $sErr, $iBaud = 9600, $iBits = 8, $iPar = 0, $iStop = 1, $iFlow = 0, $RTSMode = 0, $DTRMode = 0)
Global $CMPort = 3              ; Port
Global $CmBoBaud = 19200        ; Baud
Global $CmboDataBits =  8       ; Data Bits
Global $CmBoParity = "none"     ; Parity
Global $CmBoStop = 1            ; Stop
Global $setflow = 2             ; Flow
Global $RTSMode = 1
Global $DTRMode = 1
Global $iWait = 0

;Start up communication
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)


$bBinData = Binary("0xF10801F0010102EE")
$iNumbytes = BinaryLen($bBinData)
$tBinData = DllStructCreate("byte["&$iNumbytes&"]")
DllStructSetData($tBinData, 1, $bBinData)
$iRet = _CommSendByteArray(DllStructGetPtr($tBinData),$iNumbytes,1)
If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


_CommClearOutputBuffer()
_CommClearInputBuffer()
_Commcloseport()

;===============================================================================
; Function Name:   _CommSendByteArray($pAddr,$iNum,$iWait)

; Description:    Sends the bytes from address $pAddress
; Parameters:     $iNum the number of bytes to send.
;                 $iWaitComplete - integer: if 0 then functions returns without
;                                  waiting for bytes to be sent
;                                 if <> 0 then waits until all bytes are sent.
; Returns:  on success returns 1
;           on failure returns -1 and sets @error to 1
;
;;NB could hang if byte cannot be sent and $iWaitComplete <> 0
;    could lose data if you send more bytes than the size of the outbuffer.
;    the output buffer size is 2048
;===============================================================================
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

GREAT!!!. Now it works fine.

Thank you all for the help

Share this post


Link to post
Share on other sites

Hi,

I've done some more testing on my script. It seems to work fine, but..

I would like to use the script to send something else. The only different is the way the COM port is set up.

I have another program (not AutoIT) that works fine. So I have compared it with Portmon.

Only different from what I send with AutoIT is InSize/OutSize. It should both be 500.

With AutoIT it is 4096/2048.

Could someone please tell my how to change that, if possible.

Also, I dont know if that's the actual problem. But it's the only different, as I can see it.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Hi,

I've done some more testing on my script. It seems to work fine, but..

I would like to use the script to send something else. The only different is the way the COM port is set up.

I have another program (not AutoIT) that works fine. So I have compared it with Portmon.

Only different from what I send with AutoIT is InSize/OutSize. It should both be 500.

With AutoIT it is 4096/2048.

Could someone please tell my how to change that, if possible.

Also, I dont know if that's the actual problem. But it's the only different, as I can see it.

Only Martin, the UDF author can help you answer that.

post in his thread.

I found nothing looking through the udf and dll exports to change buffer size.

it's hard coded.

Edit: typo again

Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

Does anyone have this commMG.au3 include file?

This thread: http://www.autoitscript.com/forum/topic/45842-serial-port-com-port-udf/ Seems to be broken.

Share this post


Link to post
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