Serial port reading using Command Prompt BATCH - batch-file

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

Related

Batch file output without path/directory in front of the commands

So, as the title says i would like to remove the directory or path that gets put in front of called commands if i output a batch file into a text file.
For example i've got a batch file like that
call :sub>log.txt
:sub
SET ip=127.0.0.1
ping %ip%
What i get as output is this:
C:\Users\...>SET ip=127.0.0.1
C:\Users...>ping 127.0.0.1
pingoutput
What i want is this:
SET ip=127.0.0.1
ping 127.0.0.1
pingoutput
The path in front can get pretty annoying for deep directories.
The examples above are just this, examples. I want to create a batch file to install a sql database and different triggers and functions with the batch file, so i can easily deploy it on different pcs.
Edit says: #echo off at the beginning just completely removes a line, even the command itself.
If deep paths is your real issue, there's a simpler fix, you probably haven't thought of. Change the prompt string.
#Prompt $G
Call :Sub 1> "log.txt"
#Prompt
GoTo :EOF
:Sub
Set "IP=127.0.0.1"
%__APPDIR__%ping.exe %IP%
In the case above, I have changed the prompt string to just a greater than character, after calling the label, using Prompt without an argument returns it to its default again. Also as you can see, the # character prefix turns off echoing for those commands, so they aren't shown in your console or output file.
In the example above I used $G because it still differentiates commands from output, but you could obviously play around with it to find something you prefer.
To find out more about the options available, open a Command Prompt window, type prompt /?, press the ENTER key, and read the information presented.
#echo off disables command echoing. That is kind off the point. if you want to see the command, then echo each command while #echo off is enabled:
#echo off
call :sub>log.txt
goto :eof
:sub
echo set ip=127.0.0.1 & SET ip=127.0.0.1
echo ping %ip% & ping %ip%
or if you do not want to use the `& operator, just newlines will work:
#echo off
call :sub>log.txt
goto :eof
:sub
echo set ip=127.0.0.1
SET ip=127.0.0.1
echo ping %ip%
ping %ip%
and completely without the calling of the label:
#echo off
(echo set ip=127.0.0.1 & set ip=127.0.0.1
echo ping %ip% & ping %ip%)>log.txt

Make a batch-file loop when there is internet or launch a program and exit when there is NO internet

The idea is to have a batch file ping the internet every 5 minutes to see if there is a connection. If there is an internet connection the batch file will loop every 5 minutes to ping again. If there is NO internet connection then the batch file will launch a program and exit. I created a batch file that does everything but exit when the internet is not detected.
What I have so far loops just the way it should but I'm stuck at making the file exit if the internet connection is NOT connected and notepad.exe is launched. I don't know much about batch-files and am trying to piece things together from searches, I need help.
#echo off
setlocal
cls
:loop
#ping 209.222.18.218 -n 1 -w 1000> nul
if %ERRORLEVEL% EQU 1 start C:\windows\notepad.exe
timeout /t 60 >null
goto loop
Expected result: the batch file pings the internet every 5 minutes to detected either there is or isn't an internet connection. If there is an internet connection the batch file will loop every 5 minutes. If there isn't an internet connection the batch file will launch notepad.exe and then exit itself.
Actual result, I get the batch file to loop when it detects internet but I can't get it to exit itself when there is NO internet.
Rearrange your lines a bit:
:loop
timeout 300 >nul
ping 209.222.18.218 -n 1 -w 1000 |find "TTL=" >nul
if %errorlevel%==0 goto :loop
REM if not errorlevel 1 goto :loop
start C:\windows\notepad.exe
Use one of the if lines - whichever you are more comfortable with.

Send Command to Bluetooth Serial using Putty/Plink

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.

command line parameter while

I want to run command line with patameters to make it wait for connection then do "gpupdate /force" and then nofity user the job is done. The thing I am stuck at is making it wait for connection.
cmd /c echo Connect a network cable. & *something* & echo n | gpupdate /force & msg * Done.
If something was following code then it would work:
:top
ping -n 1 site.com >nul
if errorlevel 1 (
goto top
)
but I don't know how would I write it on 1 line as a parameter.
How would I make it work the way I wish? Alternative solutions are welcome as well!
I don't understand your aversion to a batch script. But the following one liner should work.
#echo Connect a network cable.&cmd /q /c "for /l %N in () do ping -n 1 site.com >nul&&exit"&echo echo n^|gpupdate /force&msg * Done.
The key bit is cmd /q /c "for /l %N in () do ping -n 1 site.com >nul&&exit". A new CMD.EXE process is created that enters an infinite FOR loop. It continuously pings the site until it is successful. The EXIT command only executes when PING was successful because of the && operator. Once EXIT executes, control is returned to the parent CMD.EXE process.
Why not make it all a batch file?
#echo off
echo Connect a network cable.
:top
ping -n 1 site.com >nul || goto :top
echo Connected.
gpupdate /force
msg * Done.

Pause a batch file until a host is reachable (using ping)?

I'm looking to add some functionality to a batch file that I've been writing;
essentially what happens is I dial up a VPN connection using openvpn and then continue to mount a network drive as well as many other things, what I'm looking to do is:
Dial the connection via OpenVPN (which I have working fine)
Ping a host on the other side of the VPN and don't continue through the batch file until this host is reachable.
Currently I've been using a sleep command of 20 seconds which works, but is not a very clean or intelligent way of going about it; I'd imagine I need some sort of loop to attempt to ping the host infinitely until it is reachable before continuing in the batch file. Any help would be greatly appreciated.
from antoher thread on stackoverflow... credit to paxdiablo (find the original post here)
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
This will give you enough ammo to use and solve your problem
as already mentioned years ago, output of ping is language dependent, so it's not a good idea to rely on a string like Received. The preferred and most reliable method is searching for the string TTL=:
:loop
timeout 2
ping -n 1 %ipaddress% |find "TTL=" || goto :loop
echo Answer received.
|| works as "if previous command (find) wasn't successful, then"
(as for the timeout: never build a loop without some idle time to reduce CPU load)
It works if You translate the "Received" string using the string that your language uses for the word Received (i.e. in Italian is Ricevuti).

Resources