Send Command to Bluetooth Serial using Putty/Plink - batch-file

I have searchewd a few posts here, but I cannot find one that is exactly what I am looking for. In simple terms I am trying to send a character to my Arduino via bluetooth Automatically.
I have tried both Putty and Plink, but neither work automatically. Here is the commands I have tried so far:
command.bat | putty -serial com3 -sercfg 9600
Command.bat:
#echo off
timeout /t 5
echo 2
and
plink -load Arduino echo 2
This connects to the bluetooth adapter on the Arduino, but opens an Interactive console. I can hit the number 2 on the keyboard it is sends it correctly. However I want that to be sent automatically. I have timeout in there because it takes a few seconds to connect to the bluetooth.
Is there a way to do this so I can just run a bat file and have it send the commands automatically?

If the interactive console opens up and is the most present item, you can use the following code upon the interactive console startup...
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem wait for interactive console to appear before pressing 2 to initialize
timeout /t 5
%SendKeys% "{2}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
Call it a cheap fix if you will, the code will call a javascript to press 2 for you. Im not sure how to tie it with your absolute program but this will run it automatically as the computer will simulate the input for you.

Related

Keypress simulation with Batch-JScript duo not working as expected

After doing some googling I got many answers(from stackoverflow and other sites) saying keypress automation & simulation is possible with Batch-JScript duo file, so I put together my fndings and made a script like below, but it doesn't work as expected at all.
Take a look at the below script:
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
start SystemPropertiesAdvanced.exe
%SendKeys% "{n}"
goto :EOF
#end
::JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
As it is apparent from the code, I a trying open the Environment Variables section which is already elevated to Administrator rights, so that user can edit both User and System environment variables. But this script only opens the Window before Environment Variables in elevated privilege.
As you can see, I have already put the code to simulate & automate keypress of n letter after opening the SystemPropertiesAdvanced Window and thus bypassing that manual step, but this part doesn't work.
Maybe I have made some mistake, but I cannot find what would that be, so any help is appreciated.

Make batch "START /WAIT" command wait for program's launch

I'm not a batch coder, but I'm trying to create a script that does the following once executed:
enables the dedicated video card (laptop NVIDIA card)
runs a specific video game and waits for its end
once the game is terminated, disables the dGPU
The following code works great with simple programs like notepad.exe, since they start immidiatly:
// enable dedicated GPU
devcon enable "#PCI\VEN_10DE&DEV_1C8C&SUBSYS_8259103C&REV_A1\4&3455E2C8&0&0008"
// start notepad and wait for its termination
start /b /wait "" "C:\Windows\notepad.exe" || goto QUIT
:QUIT
// disable dedicated GPU
devcon disable "#PCI\VEN_10DE&DEV_1C8C&SUBSYS_8259103C&REV_A1\4&3455E2C8&0&0008"
exit
The problem is that if I change "notepad.exe" for some heavy video game (BF1 for example) which takes some time to start, the script doesn't wait for game's actual launch and proceeds with other command. So the result is:
GPU is turned on
GPU is turned off (script exit)
After some time game starts (using Intel Graphics)
Is there a way to make the second command (start /wait) wait for game's actual launch?
The reason I need this script is: NVIDIA Optimus + Windows 10 = micro freezes
PS: sorry for my english.
// start game launcher
start /b /wait "" "launcherexecutable"
:wait
timeout 5 >nul
tasklist |findstr /i /L /c:"launcherexecutable" /c:"gameexecutable" >nul
if not errorlevel 1 goto wait
:QUIT
which should repeatedly delay for (5 seconds) then see whether either the launcher or the game executable is running and repeat the loop if so.
Hence, should proceed to quit once the game executable finishes.
Obviously, change the 5 to some other value or add intermediate executablenames as appropriate.

how to detect change in .txt file using batch?

I made a simple LAN chat batch file, and i would like to know if
there is an command that checks if a specific txt file is updated.
The chat history is stored in log.dat and i want to have a sound notification or something like that when theres a new message.
#echo off
title LAN chat reader
call Color.bat
:read
call Color.bat
cls
type log.dat
timeout /t 3 /nobreak >nul
goto read
(im a noob, please tell me if this is possible)
To check the file date/time use for and %%~t prefix:
#echo off
title LAN chat reader
setlocal enableDelayedExpansion
:read
call Color.bat
cls
type log.dat
for %%a in (log.dat) do set filetimesize=%%~tza
:checkupdate
ping -n 1 -w 100 localhost >nul
for %%a in (log.dat) do if "!filetimesize!"=="%%~tza" goto checkupdate
echo updated
goto read
wOxxOm already gave a solution to check for an updated file.
Here is a way to produce a Sound:
copy con bell.txt
Then press the ALT-key enter 007 while keeping ALT pressed, then release the ALT key. ^G should appear on your Screen (= 0x07, which is a Bell), then press Ctrl-Z. This gives you a textfile with lenght = 1 Byte
Type bell.txt
will then beep.
EDIT an easier way to produce bell.txt: on commandline, enter echo ^G>bell.txt (to produce ^G press CTRL-G). This will create a three-byte-file (instead of the one-byte-file with the copy trick) (but that's only a line feed and should not disturb).

Serial port reading using Command Prompt BATCH

I'm trying to communicate with a hardware through a TTL bridge via serial com port of a PC. And I found out some useful commands that help receiving and sending strings using a batch file . And here is my batch code
#echo off
mode COM3 BAUD=9600 PARITY=n DATA=8
:main
set /p x=5 <nul >\\.\COM3
ping localhost -n 2 >nul
type com3
goto main
The problem is that the batch file stucks when it arrives to "type com3" line . It starts to listen the com3 port and never leaves that line and stucks there .. Is there anything like a timeout procedure that will help to terminate the "type com3" line after a while ?
I'm not good at batch programming , all I can do is writing simple scripts using batch commands.
Thanks in advance
The type com3 command will never return for you. It will listen on the port forever. A slight tweak to have 2 windows should help you out.
#echo off
mode COM3 BAUD=9600 PARITY=n DATA=8
start type com3
:main
set /p x=5 <nul >\\.\COM3
ping localhost -n 2 >nul
goto main

start batch file in fullscreen mode

How can I start a batch file in full-screen mode? I know that this question was asked before, but it wasn't actually answered.
unfortunately, I don't know reverse engineering, so I cant decompile the code.
Here is something I tested:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
start cmd.exe
%sendkeys% "(%{enter})"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
The idea is that it starts a cmd window, and (this part of the code %sendkeys% "(%{enter})") is supposed to simulate the user pressing [alt] + [enter]. But it doesn't work.
I wrote a little c# tool to send an alt+enter to the cmd window, but I'm looking for an internal method.
If sendkeys parameter for "Alt-Enter" should be "%{enter}", then this line:
%sendkeys% "(%{enter})"
... should have not parentheses and there is one percent sign missing. Try:
%sendkeys% "%%{enter}"
See the example for "Alt-V" at this post
I made an autoIT script, extremely simple.
Run("cmd.exe")
sleep (1000)
Send("!{enter}")
Unfortunately, this isn't what I want. I was looking for more of an actual command, not some other 3d party program.
:: edit
here: This is a link to the compiled 1 line skiddy script that I made.
Its a command line tool, so all you have to do is type fullscreen in a cmd window to toggle fullscreen mode.
In case you don't know how to use a command line tool, you have to be in the same directory as the file.

Resources