Batch-File: Receive Data from the Serial-Port and

2019-03-31 12:16发布

问题:

I'm trying to extract some data from an Arduino over the usb-serial-port and to write it into a txt-file. Therefore i'm using a batch-file (Windows 7, ordinary cmd), which sends e.g. a "d" to request the desired data from the Arduino. Having received the "d", the Arduino starts to send the data to the pc. The batch-file reads the data and writes it to some txt-file. The batch code is:

    mode COM4 BAUD=9600 PARITY=n DATA=8
    echo d >COM4
    COPY COM4 data.txt

That works so far, but the problem is:

How do I stop the COPY-process?

When the Arduino is done with sending the data, the batch file keeps on wating for more. My preferred solution would be, that the Arduino sends some string like "end", the batch file recognizes this and stops reading. Is that somehow possible?

回答1:

I believe that copy will recognize a Control-Z as an end of file character and terminate.

Harkens way back to the old dos days, am I showing my age?



回答2:

< COM4 (
  set /p COM4_1=
  set /p COM4_2=
  set /p COM4_3=
) 
set COM4_

COM1, COM2 ... (not sure if is it possible for COM4 to exist — I have only COM3) are interpreted like some kind of files by cmd.exe (also CON and NUL, LPT1). With SET /p Lline1=<somefile you can read the first line of a file. This is on 3 lines because of the brackets.