Jump to content

send commands and receiving them correctly


Recommended Posts

am a noob and i made a little program to send commands to a device via usb by sending arguments to a command line tool

am having trouble in two things

1- first sending what i type into the input box and inserting in the command in place of the word HELLO between ' ' when clicking on send button.

Screenshot_1.png.a18abda33ce37a6ec1532bf3304f8177.png

2- second clearing the input and receiving the output as text also and displaying it in the input box when clicking on receive button.

;the UI
Global $cmd = GUICtrlCreateGroup("command sender", 13, 407, 340, 80)
Global $instcmdsnd = GUICtrlCreateButton("setup", 25, 422, 100, 25)
GUICtrlSetFont(-1, 8.5, 400, 0, "Segoe UI", 5)
GUICtrlSetOnEvent(-1, "setcmd")
Global $sendcmd = GUICtrlCreateButton("send", 133, 422, 100, 25)
GUICtrlSetFont(-1, 8.5, 400, 0, "Segoe UI", 5)
GUICtrlSetOnEvent(-1, "gocmd")
Global $getcmd = GUICtrlCreateButton("receive", 242, 422, 100, 25)
GUICtrlSetFont(-1, 8.5, 400, 0, "Segoe UI", 5)
GUICtrlSetOnEvent(-1, "getcmd")
Global $cmdInput = GUICtrlCreateInput("", 25, 455, 318, 22)

;the function

Func setcmd()
        Run(@ComSpec & " /c c:\mytool\bin\commandsender.exe start ", @ScriptDir, @SW_HIDE, $stderr_child + $stdout_child)
EndFunc
Func gocmd()
        Run(@ComSpec & " /c c:\mytool\bin\commandsender.exe send -t 'HELLO'  ", @ScriptDir, @SW_HIDE, $stderr_child + $stdout_child)
EndFunc
Func getcmd()
    Local $get = RunWait("c:\mytool\bin\response.bat", @ScriptDir, @SW_HIDE)
EndFunc

thanks in advance

Link to comment
Share on other sites

1st thing its always useful to post working code.  Probably why you haven't got much of a response.  You definitely get more help faster if people don't have to fight a partial script just to get something to happen.  

I wasn't able to test the stdread portion of this but it should give you a good idea of what needs to happen.  Its based off of the examples in the help file.  I'd also like to add that you will probably need to play around with the output format how its displayed etc.  you will more than likely overrun the input box with the output.  It will probably all fit but you probably won't be able to see it all. You may want to consider a different display method (unless it automatically resizes itself which i doubt but its possible). you may also want to consider just creating a new gui that just displays the output or instead of using the ArrayToString() just use _arraydisplay().   Would probably be easier to just read it off the cmd prompt but thats just me.   Goodluck.

 

;the UI
#include <GUIConstantsEx.au3>
#include <AutoItConstants.au3>
GUICreate('group',@DesktopWidth,@DesktopHeight)
Global $cmd = GUICtrlCreateGroup("command sender", 13, 407, 340, 80)
Global $instcmdsnd = GUICtrlCreateButton("setup", 25, 422, 100, 25)
GUICtrlSetFont(-1, 8.5, 400, 0, "Segoe UI", 5)
GUICtrlSetOnEvent(-1, "setcmd")
Global $sendcmd = GUICtrlCreateButton("send", 133, 422, 100, 25)
GUICtrlSetFont(-1, 8.5, 400, 0, "Segoe UI", 5)
GUICtrlSetOnEvent(-1, "gocmd")
Global $getcmd = GUICtrlCreateButton("receive", 242, 422, 100, 25)
GUICtrlSetFont(-1, 8.5, 400, 0, "Segoe UI", 5)
GUICtrlSetOnEvent(-1, "getcmd")
Global $cmdInput = GUICtrlCreateInput("", 25, 455, 318, 22)
 GUISetState(@SW_SHOW)
;the function

 While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $sendcmd
                    gocmd()
            case $getcmd
                   getcmd()
            case $instcmdsnd
                    setcmd()
        EndSwitch
    WEnd


Func setcmd()

       ;Run(@ComSpec & " /c c:\mytool\bin\commandsender.exe start ", @ScriptDir, @SW_HIDE, $stderr_child + $stdout_child)

EndFunc
Func gocmd()
    $read=GUICtrlRead($cmdInput)
    if $read="" Then
        ;Run(@ComSpec & " /c c:\mytool\bin\commandsender.exe send -t 'HELLO'  ", @ScriptDir, @SW_HIDE, $stderr_child + $stdout_child)
    Else
        ;Run(@ComSpec & " /c c:\mytool\bin\commandsender.exe send -t '" & $read & "'  ", @ScriptDir, @SW_HIDE, $stderr_child + $stdout_child)
    EndIf
EndFunc
Func getcmd()

   ;$iPID = RunWait("c:\mytool\bin\response.bat", @ScriptDir, @SW_HIDE)
   ;ProcessWaitClose($iPID)
   ;$sOutput = StdoutRead($iPID)
   ;$aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)
   ;$sString=ArrayToString($aArray)
   ;GUICtrlSetData($cmdInput,$sString)
EndFunc

Func ArrayToString($array)
    Local $temp
    for $x=1 to UBound($array)-1
        $temp&=$array[$x] & " "
    Next
    Return $temp
    EndFunc

 

Link to comment
Share on other sites

12 hours ago, xmustaphax said:

am a noob

lol, Welcome to the forum @xmustaphax :)

Opt("GUIOnEventMode", 1) ; Enable/disable OnEvent functions notifications.
The above is needed for GUICtrlSetOnEvent(-1, "gocmd") to work

As @markyrocks pointed, you'll need a "While loop" like:

While 1 ; so is always true
  sleep(100) ; any value realy
wend ; ..that is all as the code runs: Opt("GUIOnEventMode", 1) ; Enable/disable OnEvent functions notifications.

if you wanna read the output of the command line, you'll need to capture that and  the output will go ??, ...you'll need a better GUI

Again, welcome to the forum and have fun coding :ILA2:

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

thanks a lot for the help.

and sorry i forgot to past the rest of the code

the first part worked like a charm , for the second part " the receiving " part i was counting on using a batch script that call grep.exe and other linux commands binaries to trim the output  to get the text between " "  then display it in the inputbox

as an example the command sender give me this output :

completed: result=-1, data="text"

i have no idea how the do the trimming in autoit

thanks .

Link to comment
Share on other sites

SRE is quite handy in those tasks :

#include <Constants.au3>

Local $sTest = 'completed: result=-1, data="text"'

$sTrimmed = StringRegExpReplace ($sTest, '.*"(.*)".*','$1')
MsgBox ($MB_SYSTEMMODAL,"",$sTrimmed)

 

Edited by Nine
Link to comment
Share on other sites

Stringregex is a great tool but I don't understand it.  But for something as simple as find between quotes.  You can just do something like 

$sString=ArrayToString($array)
    $split=stringsplit($sString,'"')  ; '   "   '   expanded bc it looks weird
    ; after the first " would be...
    $split[2]  ;data to set
    ;but only with some kinda check
    If split[0]>1 then 
    ;set data $split[2]
    Endif

 

But whatevers clever.  

 

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