Jump to content

Send hex string to COM port


Brian39
 Share

Recommended Posts

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)
Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
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.

Link to comment
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.

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

Link to comment
Share on other sites

  • 7 months later...
  • Developers

No need to post multiple times with the same question. a search on "commMG" would work as well. :graduated:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 5 years later...

Hi,

Does anyone have an idea how to controll two COM ports? I've changed @Brian39 above code for sending strings but I need to controll two different COM ports with different Baud rate.  AS long as I have connection with both COM's second one does not "respect" different Baud rate. My code below:

 

#include "FilesHandling.au3"
;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 $CMPortScanner = 7              ; Port
Global $CMPortOptic = 10              ; Port
Local $CmBoBaudOptic = 19200        ; Baud
Local $CmBoBaudScanner = 115200        ; 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


;==========================Funcions=============================

;DailyQC

Func SerialPort_UsingCommMG_DailyQC($FileName1, $LineNumber1)
    SendData2($FileName1, $LineNumber1)
    Sleep(500)
EndFunc


;Single Test

Func SerialPort_UsingCommMG_SingleTest($FileName1, $LineNumber1, $FileName2, $LineNumber2)
    SendData2($FileName1, $LineNumber1)
    Sleep(500)
    SendData2($FileName2, $LineNumber2)
    Sleep(500)
EndFunc

; BatchMode

Func SerialPort_UsingCommMG_BatchMode($NumberOfTests, $FileName1, $FileName2)
    For $i = 1 To $NumberOfTests
        SendData2($FileName1, $i)
        Sleep(500)
        SendData2($FileName2, $i)
        Sleep(500)
    Next

EndFunc

Func SerialPort_UsingCommMG_OpticSend($Event)

    Sleep(500)
    Switch($Event)
        Case 1 ;"AK"
            SendDataOPTIC("OpticsData.txt",1)
        Case 2 ;"SHUT"
            SendDataOPTIC("OpticsData.txt",2)
        Case 3 ;"OPEN"
            SendDataOPTIC("OpticsData.txt",3)
        Case 4 ;"Data"
            SendDataOPTIC("OpticsData.txt",4)
        Case 5 ;"SData"
            SendDataOPTIC("OpticsData.txt",5)
    EndSwitch


EndFunc

;~ ;Func SerialPort_UsingCommMG_
;~ ;for testing only
Func SerialPort_Test_SHUT()
    SendDataOPTIC("OpticData.txt", 1)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 2)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 3)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
    SendDataOPTIC("OpticData.txt", 4)
    SendDataOPTIC("OpticData.txt", 6)
    Sleep(100)
EndFunc


;==========================EOF Funcions===================================

Func SendData2($FileName, $LineNumber)
    ;Start up communication
    _CommSwitch($CMPortScanner)
    _CommSetPort($CMPortScanner, $sportSetError, $CmBoBaudScanner, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

    $sStrData  = ReadFile($FileName, $LineNumber)
    $iNumString = StringLen($sStrData)
    $tStrData = DllStructCreate("String["&$iNumString&"]")
    $iRet = _CommSendString($sStrData,1)

    If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


    _CommClearOutputBuffer()
    _CommClearInputBuffer()
    _Commcloseport()
EndFunc

Func SendDataOPTIC($FileName, $LineNumber)
    ;Start up communication
    _CommSwitch($CMPortOptic)
    _CommSetPort($CMPortOptic, $sportSetError, $CmBoBaudOptic, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow, $RTSMode, $DTRMode)

    $sStrData  = ReadFile($FileName, $LineNumber)
    $iNumString = StringLen($sStrData)
    $tStrData = DllStructCreate("String["&$iNumString&"]")
    $iRet = _CommSendString($sStrData,1)

    If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)


    _CommClearOutputBuffer()
    _CommClearInputBuffer()
    _Commcloseport()
EndFunc

 

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