Sign in to follow this  
Followers 0
Niverton

ConsoleRead(), writing in the script's console

7 posts in this topic

#1 ·  Posted (edited)

Hello

I want to be able to interact with my script without a GUI, using Window's console. I can't get the ConsoleRead() function to work like I want, after compiling my script as console application, the console appears, but I can't write anything in it manually.

Here is my testing script:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

ConsoleWrite("Hello World!" & @CRLF)
While 1
    $buff = ConsoleRead()
    If $buff <> "" Then MsgBox(0, "", $buff)
    Sleep(100)
WEnd

It does write "Hello World!" in the console, I can send it text when I launch the script like in the ConsoleRead() example, but I can't write in the console. Any help please?

Thank you, and sorry for the bad English.

Edited by Niverton

Share this post


Link to post
Share on other sites



So your script is for example test.au3.

You compile it to test.exe which is a console application.

You open a command prompt and type with a working directory same as test.exe

You type in the command prompt

echo testing | test.exe

Now, you should see "Hello World!" in the command prompt and see a MsgBox with "testing" in it (without the quotes).

So now you cannot type into the prompt, correct?

The reason is because the process test.exe is trapped in the loop reading from the console. Press Control+C to break out of the process. You are missing the @error check after the use of ConsoleRead to get out of the loop.

So try this

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

ConsoleWrite("Hello World!" & @CRLF)
While 1
    $buff = ConsoleRead()
    If @error Then ExitLoop
    If $buff <> "" Then MsgBox(0, "", $buff)
    Sleep(100)
WEnd

:)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for your answer :)

 

You open a command prompt and type with a working directory same as test.exe


You type in the command prompt
echo testing | test.exe
Now, you should see "Hello World!" in the command prompt and see a MsgBox with "testing" in it (without the quotes).

 

Yes, this works.

I tried your script, but I still can't write in the prompt, and now it closes instantly after checking the error, I tried adding a Sleep(), no result, can't write in the prompt :/

I tried modifying a bit, but I get the same result as my first test

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

ConsoleWrite("Hello World!" & @CRLF)
While 1
    $buff = ConsoleRead()
    If @error Then ContinueLoop
    If $buff == "stop" Then
        ExitLoop
    Else
        MsgBox(0, "", $buff)
    EndIf
    Sleep(100)
WEnd

Does the script work for you ? The cursor is blinking in the prompt, but I can't write in it

Edited by Niverton

Share this post


Link to post
Share on other sites

Does the script work for you ? The cursor is blinking in the prompt, but I can't write in it

Your code looks unexpected. No, it does not work good. You are continuing the use of ConsoleRead even after a possible error condition as you are using ContinueLoop. And how is $buff going to equal "stop" unless you stdin that text from the 1st process?

What you are doing is not clear with my understanding and what results you expect is not clear with my understanding. I am not sure if you are using ConsoleRead in a suitable way or whether you misunderstand it. I can guess that you may be trying something unusual and could be expecting something that may not happen. If you can explain to me to clear up these concerns then I would be appreciate it. :)

Share this post


Link to post
Share on other sites

Oh, okay, I'm sorry I guess I wasn't clear :/

I want to run the script in the console, and be able to send it commands via the prompt, but I don't want the script to pause and wait for an input (like a "cin >>" in C++) from the prompt, I'd like to be able to send the commands in STDIN so that the script can read them one by one when it's ready to treat a new command, like when you run another program and use a script to send to the program's STDIN. Hard to explain, it's like a game server that you run from your console, the server prints in the prompt, but you can send it via the same prompt a command at any time (for example Valve's games' servers, or Minecraft's), like this:

Server.png

The program does not wait for an input, it keeps running, and when it's ready it reads from the STDIN. It's hard to explain, and it looks like it's not possible in AutoIt :(

Thank you for your time :)

Share this post


Link to post
Share on other sites

I know of 2 basic ways to use ConsoleRead. One is the external use of piping from 1 process to another. The other is a parent to child process in a form similar to inter process communication. Even though ConsoleRead is referred to taking STDIN (kind of opposite of STDOUT with ConsoleWrite), the functionality is AFAIK different to STDIN and buffer use with the keyboard. The buffer tends to be shared between the 2 processes and CMD tends to be a 3rd party. I remember I once saw some C++ code to handle piping with STDIN and looked quite different to just use of CIN.

What you want seems to be a console application which some members here have explored with some UDFs which can be found in the examples forum. One is from Shaggi >here. Another is from Mat here. And you may find some other if you use keywords like console and other terms like that. I have not tried these to much extent so am not a good resource of information on them. Give them a try and one of them may inspire you to create what you wish for.

I know that you are just giving examples with a console application but be careful with the forum rules on game automation. :)

Share this post


Link to post
Share on other sites

Thanks for the links !

They're not allowing to read from the prompt without pausing the program, but I guess it's just impossible to do with Autoit :(

But these UDFs are really interesting, they will help me on my other projects, thanks for your time and your help :D

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