com1 input

7332 views
Skip to first unread message

john miller

unread,
Feb 14, 2002, 8:08:15 AM2/14/02
to
I have a device attached to COM1 which sends/recieves
data. I have been able to send data using this:

echo "command" > COM1

How can I recieve the answer or result that the device is
returning. I would like for the for the data which is
being transmitted from the device to go to a file.

Al Dunbar

unread,
Feb 15, 2002, 11:26:58 PM2/15/02
to

"john miller" <jo...@millertransportation.com> wrote in message
news:43ea01c1b558$aa1d7010$19ef2ecf@tkmsftngxa01...

Kind of depends on the format of the data that the device will return. If it
can be configured to append a ctrl-Z at the end of everything to signal an
end-of-file condition, then a copy command might work, i.e.:

copy com1 result.txt

If it returns a fixed number of records, the following might work:

<com1 (set/p line1=&line2=&line3=)

If the extent of the output can be deduced from its content, for example, if
the first line gave the number of lines following, perhaps something like:

<com1 set/p nrec=
for /l %%A in (1,1,%nrec%) do (
<com1 set/p line%%A=
)

If the syntax is more complex, requiring that each line be parsed, well,
that is a little more complex. In that case it might make more sense to
write a program to communicate with the device, or run a scriptable terminal
emulator.


/Al

Al Dunbar

unread,
Feb 16, 2002, 12:05:18 AM2/16/02
to

"john miller" <jo...@millertransportation.com> wrote in message
news:43ea01c1b558$aa1d7010$19ef2ecf@tkmsftngxa01...

In testing my previous reply to the above, I ran across a way to simulate an
ECHO without a terminating CRLF sequence using batch-only facilities. No
doubt others are aware of this, however, I have not seen it since I have
been following this newsgroup, so will post it here for the benefit of any
others who, like myself, did not know.

The command "echo.hello world" emits the string "hello world" followed by a
CRLF sequence. There are at least a couple of situations where this fact is
a nuisance:

- displaying a minimalist progress bar.
- sending arbitrary character strings to a serial device.
- writing strings from separate commands that need to appear in the same
line in an output file.

The command that does this is:

<nul (set/p anyvariable=string to emit)

The "<nul" pipes a nul response to the set/p command, which will cause the
variable used to remain unchanged. As usual with set/p, the string to the
right of the equal sign is displayed as a prompt with no CRLF.

Here is an example where this is used for a rudimentary progress bar:

@echo off
for /l %%A in (1,1,20) do (
<nul (set/p z=%%A)
>nul ping 127.0.0.1 -n 2
)

And here is an example where info is written to a single line in a file from
multiple uses of the set/p command:

<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt

The dir command should indicate the file size as 12 bytes: "hello world!".

The strings output need not be literal, and can originate from any source
capable of creating a variable, simply by including a variable reference in
the prompt string:

<nul (set/p z=sec min hours: %time:~6,2% %time:~3,2% %time:~0,2%)


/Al


Clay Calvert

unread,
Feb 16, 2002, 9:47:49 AM2/16/02
to
This is an excellent technique. I definitely hadn't seen it before.

Thanks


Clay Calvert
Replace "W" with "L" in email.

Al Dunbar

unread,
Feb 17, 2002, 12:49:18 PM2/17/02
to

"Clay Calvert" <ccal...@Wanguru.com> wrote in message
news:3trs6uon4pej3hci5...@4ax.com...

> This is an excellent technique. I definitely hadn't seen it before.

Gasp! Al Dunbar finally comes up with something new! Will wonders never
cease?

> Thanks

You're welcome. Enjoy.

/Al

Phil Robyn

unread,
Feb 17, 2002, 4:43:46 PM2/17/02
to
Al Dunbar wrote:

> "Clay Calvert" <ccal...@Wanguru.com> wrote in message
> news:3trs6uon4pej3hci5...@4ax.com...
> > This is an excellent technique. I definitely hadn't seen it before.
>
> Gasp! Al Dunbar finally comes up with something new! Will wonders never
> cease?
>
> > Thanks
>
> You're welcome. Enjoy.
>
> /Al

Great tip, Al! You can also use it to write the 'pesky' characters to a file
(< > | ^ &) if you use double quotes:

<nul (set /p z="<this>|<is>|<only>|<a>|<TEST>")>out.txt

Phil Robyn
Univ. of California, Berkeley


--

u n z i p m y a d d r e s s t o s e n d e - m a i l


Al Dunbar

unread,
Feb 17, 2002, 6:50:20 PM2/17/02
to

"Phil Robyn" <pro...@uclink.berkzipeley.edu> wrote in message
news:3C702411...@uclink.berkzipeley.edu...

> Al Dunbar wrote:
>
> > "Clay Calvert" <ccal...@Wanguru.com> wrote in message
> > news:3trs6uon4pej3hci5...@4ax.com...
> > > This is an excellent technique. I definitely hadn't seen it before.
> >
> > Gasp! Al Dunbar finally comes up with something new! Will wonders never
> > cease?
> >
> > > Thanks
> >
> > You're welcome. Enjoy.
> >
> > /Al
>
> Great tip, Al! You can also use it to write the 'pesky' characters to a
file
> (< > | ^ &) if you use double quotes:
>
> <nul (set /p z="<this>|<is>|<only>|<a>|<TEST>")>out.txt

Hmmm, I didn't know that set/p would strip the double-quotes before
displaying the prompt. Makes this tip even more useful for the reason you
give.

Now all we need is a similarly simple way to output non-displaying control
characters.

/Al

Edwin Collins

unread,
Feb 27, 2002, 4:30:13 PM2/27/02
to
Very nice Al

"Al Dunbar" <Luigi...@hotmail.com> wrote in message
news:eGOMC5AuBHA.2660@tkmsftngp05...

Eashwer B. Iyer

unread,
Nov 18, 2021, 11:58:42 AMNov 18
to
I wonder if the opposite is also possible -I need to preserve the CRLF in the incoming parameters to the batch script.
So if test.bat "Hello
World
Line 3
Line 4"

then I want echo %* to echo the output in 4 lines without taking out the CRLF

Is this possible?
Reply all
Reply to author
Forward
0 new messages