Sign in to follow this  
Followers 0
civilcalc

Interaction with an external device....

11 posts in this topic

I have an external device, attached to a USB port (COM18).

It is able to send data on command, but I want my script to be able to receive it, and save it or process it.

I think it can be done with either the TCP or UDP functions, but I am unable to figure out how to do it.

Can anyone help?

Share this post


Link to post
Share on other sites



I have an external device, attached to a USB port (COM18).

It is able to send data on command, but I want my script to be able to receive it, and save it or process it.

I think it can be done with either the TCP or UDP functions, but I am unable to figure out how to do it.

Can anyone help?

USB ports do not communicate by TCP/UDP protocols on the USB bus. What is the mystery device?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

LOL,

nothing like that, its a leica geosystems TCR805Power, its a survey tool.

It sends files like this

81..000000789822 82..47466344000 83..734288845325

81..000000736511 82..64552555266 83..725141614444

etc.......

and i need to use this data within a function

Share this post


Link to post
Share on other sites

LOL,

nothing like that, its a leica geosystems TCR805Power, its a survey tool.

It sends files like this

81..000000789822 82..47466344000 83..734288845325

81..000000736511 82..64552555266 83..725141614444

etc.......

and i need to use this data within a function

Have a look at my serial port UDF in my signature. That will deal with COM18 on a usb to serial port device.


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.

Share this post


Link to post
Share on other sites

Have a look at my serial port UDF in my signature. That will deal with COM18 on a usb to serial port device.

Thanks, this looks exactly like what I am looking for.

The machine is at work, so I won't be able to do it until then.....

but your udf looks easy to follow, and yet still complex enough to do what I want to.

Thanks again.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I cant seem to get the commmg udf working.

I have tried the simple _commlistports function, but just get a blank string returned.

I am running vista, and have included the #requireadmin feature.

Where am I going wrong?

#include <CommMG.au3>

#requireadmin

$list = _commlistports(1)

MsgBox(0,"",$list)

Edited by civilcalc

Share this post


Link to post
Share on other sites

CommMG works on WinXP SP2. The example from COMMGv2.zip just requires some funcs renamed to run on current AutoIt v3.2.10.0.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I cant seem to get the commmg udf working.

I have tried the simple _commlistports function, but just get a blank string returned.

I am running vista, and have included the #requireadmin feature.

Where am I going wrong?

#include <CommMG.au3>

#requireadmin

$list = _commlistports(1)

MsgBox(0,"",$list)

To ensure the dll is being found try this

msgbox(0,"commg.dll version is",_Commgetversion(1))

I have tried on a Vista PC. At first it gives an empty string as for you, but in this case the PC I tried had no COM ports as far as the control panel showed so an empty string is correct.

I then installed a USB to Serial port converter and tried again and this time I got "COM3" back as the list of available ports.

(I didn't use #RequireAdmin.)

So I conclude that the UDF should work on Vista though that's the only test I've done so far.

picaxe is correct that some function names need to be changed if you use the example I gave with the first versions of commg.dll. These changes are for the standard AutoIt include files not my functions.

I will make a more up-to-date version of the example.- Thanks for pointing it out picaxe.

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.

Share this post


Link to post
Share on other sites

FYI - Here's a modified version of Martin's CommgExample for AutoIt v3.2.10.0

;Example program showing how to use some of the commMg.au3 UDF functions
;this example is a very simple terminal
; for AutoIt v3.2.10.0 
#include <GUIConstants.au3>
#include "CommMG.au3"   ;or if you save the commMg.dll in the @scripdir use #include @ScriptDir & '\commmg.dll'
#include <GuiEdit.au3>
#include <GuiComboBox.au3>

Opt("WINTITLEMATCHMODE", 3)
Opt("OnExitFunc", "alldone")
HotKeySet("{ESC}", "AllDone")

$result = '';used for any returned error message setting port
Const $settitle = "COMMG Example - set Port", $maintitle = "COMMG Example" 
#region main program

#Region ### START Koda GUI section ###
$Form2 = GUICreate("COMMG Example", 473, 349, 339, 93, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
$Edit1 = GUICtrlCreateEdit("", 10, 25, 449, 223, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$BtnSend = GUICtrlCreateButton("Send", 380, 273, 53, 30, $BS_FLAT)
$Input1 = GUICtrlCreateInput("", 18, 279, 361, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Add LF to incomming CR", 273, 4, 145, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Label1 = GUICtrlCreateLabel("Text to send", 24, 261, 63, 17)
$BtnSetPort = GUICtrlCreateButton("Set Port", 16, 312, 73, 30, $BS_FLAT)
$Label2 = GUICtrlCreateLabel("Received text", 34, 6, 70, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
WinSetTitle($Form2, "", $maintitle)
While SetPort(0) = -1
    If MsgBox(4, 'Port not set', 'Do you want to quit the program?') = 6 Then Exit
WEnd

Events()
GUICtrlSetState($Edit1, $GUI_FOCUS)

While 1
    Sleep(100)
    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _CommGetline (@CR, 20, 200)
    ;$scroll = StringInStr($instr,@CR)
    If $instr <> '' Then;if we got something
        If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr, @CR, @CRLF)
        $lines = GUICtrlRead($Edit1)
        $charcount = StringLen($lines)
        _GUICtrlEdit_SetSel($Edit1, $charcount, $charcount)
        If $charcount > 10000 Then GUICtrlSetData($Edit1, StringRight($lines & $instr, 8000))
        _GUICtrlEdit_ReplaceSel($Edit1, $instr, False)
        _GUICtrlEdit_Scroll($Edit1, $SB_SCROLLCARET)
        ;If $scroll Then _GUICtrlEdit_LineScroll($edit1,0,_GUICtrlEdit_GetLineCount($edit1) - 14)
    EndIf
WEnd

AllDone()

#endregion main program
Func Events()
    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "justgo")
    GUICtrlSetOnEvent($BtnSend, "SendEvent")
    GUICtrlSetOnEvent($BtnSetPort, "SetPortEvent")
EndFunc   ;==>Events

Func SetPortEvent()
    SetPort();needed because a parameter is optional for setport so we can't use "setport" for the event
EndFunc   ;==>SetPortEvent

Func justgo()
    Exit
EndFunc   ;==>justgo

Func SendEvent();send the text in the inputand append CR
    _CommSendstring (GUICtrlRead($Input1) & @CR)
    GUICtrlSetData($Input1, '');clear the input
    GUICtrlSetState($Edit1, $GUI_FOCUS);sets the caret back in the terminal screen
EndFunc   ;==>SendEvent


Func AllDone()
    ;MsgBox(0,'will close ports','')
    _Commcloseport ()
    ;MsgBox(0,'port closed','')
    Exit
EndFunc   ;==>AllDone

; Function SetPort($mode=1)
; Creates a form for the port settings
; Parameter $mode sets the return value depending on whether the port was set
; Returns  0 if $mode <> 1
;          -1 If` the port not set and $mode is 1
Func SetPort($mode = 1);if $mode = 1 then returns -1 if settings not made
    Opt("GUIOnEventMode", 0);keep events for $Form2, use GuiGetMsg for $Form3
    #Region ### START Koda GUI section ###
    $Form3 = GUICreate("COMMG Example - set Port", 380, 280, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS, $DS_MODALFRAME), BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
    $Group1 = GUICtrlCreateGroup("Set COM Port", 10, 16, 280, 252)
    $CmboPortsAvailable = GUICtrlCreateCombo("", 119, 36, 145, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
    $CmBoBaud = GUICtrlCreateCombo("", 119, 74, 145, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $WS_VSCROLL))
    GUICtrlSetData(-1, "50|75|110|150|600|1200|1800|2000|2400|3600|4800|7200|9600|10400|14400|15625|19200|28800|38400|56000|57600|115200|128000|256000", "9600")
    $CmBoStop = GUICtrlCreateCombo("", 119, 149, 145, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
    GUICtrlSetData(-1, "1|2|1.5", "1")
    $CmBoParity = GUICtrlCreateCombo("", 119, 186, 145, 25)
    GUICtrlSetData(-1, "odd|even|none", "none")
    $Label2 = GUICtrlCreateLabel("Port", 86, 40, 23, 17)
    $Label3 = GUICtrlCreateLabel("baud", 81, 78, 28, 17)
    $Label4 = GUICtrlCreateLabel("No. Stop bits", 44, 153, 65, 17)
    $Label5 = GUICtrlCreateLabel("parity", 80, 190, 29, 17)
    $CmboDataBits = GUICtrlCreateCombo("", 118, 111, 145, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
    GUICtrlSetData(-1, "7|8", "8")
    $Label7 = GUICtrlCreateLabel("No. of Data Bits", 30, 115, 79, 17)
    $ChkboxFlow = GUICtrlCreateCheckbox("use XON/XOFF flow control", 118, 233, 160, 17)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $BtnApply = GUICtrlCreateButton("Apply", 305, 100, 60, 30, $BS_FLAT)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    $BtnCancel = GUICtrlCreateButton("Cancel", 305, 152, 60, 30, $BS_FLAT)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    WinSetTitle($Form3, "", $settitle);ensure a change to Koda design doesn't stop script working
    $mainxy = WinGetPos($Form2)
    WinMove($Form3, "", $mainxy[0] + 20, $mainxy[1] + 20)
    ;$set = _CommSetport(1,$result,9600,8,0,1,0)

    $portlist = _CommListPorts (0);find the available COM ports and write them into the ports combo
    If @error = 1 Then
        MsgBox(0, 'trouble getting portlist', 'Program will terminate!')
        Exit
    EndIf
        
    For $pl = 1 To $portlist[0]
        GUICtrlSetData($CmboPortsAvailable, $portlist[$pl]);_CommListPorts())
    Next
    GUICtrlSetData($CmboPortsAvailable, $portlist[1]);show the first port found

    _GUICtrlComboBox_SetMinVisible($CmBoBaud, 10);restrict the length of the drop-down list

    $retval = 0
    While 1
        $msg = GUIGetMsg()
        If $msg = $BtnCancel Then
            If Not $mode Then $retval = -1
            ExitLoop
        EndIf
        If $msg = $BtnApply Then
            Local $sportSetError
            If GUICtrlRead($ChkboxFlow) - $GUI_CHECKED Then
                $setFlow = 1
            Else
                $setFlow = 0
            EndIf
            $setport = StringReplace(GUICtrlRead($CmboPortsAvailable), 'COM', '')
            ; _CommSetPort Parameters: 
            ;   $iPort - integer = the port or COM number to set. Allowed values are 1 or higher.
            ;               NB WIndows refers To COM10 Or higher`as \\.\com10 but only use the number 10, 11 etc
            ;   $sErr  - string: the string to hold an error message if func fails.
            ;   iBaud  - integer: the baud rate required. allowed values are one of
            ;               50, 75, 110, 150, 600, 1200, 1800, 2000, 2400, 3600, 4800, 7200, 9600, 10400,
            ;               14400, 15625, 19200, 28800, 38400, 56000, 57600, 115200, 128000, 256000
            ;   $iBits - integer:  number of bits in code to be transmitted
            ; $iParity - integer: 0=None,1=Odd,2=Even,3=Mark,4=Space
            ;   $iStop - integer: number of stop bits, 1=1 stop bit 2 = 2 stop bits, 15 = 1.5 stop bits
            ;   $iFlow - integer: 0 sets hardware flow control, 
            ;               1 sets XON XOFF control, 
            ;               2 sets NONE i.e. no flow control.
            _CommSetPort($setport, $sportSetError, GUICtrlRead($CmBoBaud), GUICtrlRead($CmboDataBits), GUICtrlRead($CmBoParity), GUICtrlRead($CmBoStop), $setFlow)
            If @error <> 0 Then 
                MsgBox(262160, "Set Port Error", $sportSetError & @TAB)
                $retval = -1
                ExitLoop
            EndIf
            $mode = 1
            ExitLoop
        EndIf
        ;stop user switching back to $form2
        If WinActive($maintitle) Then
            ConsoleWrite('main is active' & @CRLF)
            If WinActivate($settitle) = 0 Then MsgBox(0, 'not found', $settitle)
        EndIf
    WEnd
    GUIDelete($Form3)
    WinActivate($maintitle)
    Events()
    Return $retval
EndFunc   ;==>SetPort

Share this post


Link to post
Share on other sites

Thanks for the advice, but which software did you install to get the conversion?

ps sorry for the late reply, but I had loads of other projects on.

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
Sign in to follow this  
Followers 0