windows 批处理文件:从串口接收数据并将其写入txt文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19489733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch-File: Receive Data from the Serial-Port and write it into txt-File
提问by DonCookie
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:
我正在尝试通过 USB 串行端口从 Arduino 中提取一些数据并将其写入 txt 文件。因此,我正在使用批处理文件(Windows 7,普通 cmd),它发送例如“d”以从 Arduino 请求所需的数据。收到“d”后,Arduino 开始将数据发送到 PC。批处理文件读取数据并将其写入某个 txt 文件。批号为:
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?
当 Arduino 完成发送数据时,批处理文件会继续等待更多。我的首选解决方案是,Arduino 发送一些像“end”这样的字符串,批处理文件识别出这一点并停止读取。这有可能吗?
采纳答案by Jeff
I believe that copy will recognize a Control-Z as an end of file character and terminate.
我相信副本会将 Control-Z 识别为文件结尾字符并终止。
Harkens way back to the old dos days, am I showing my age?
哈肯斯回到过去的日子,我暴露了我的年龄吗?
回答by npocmaka
< 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.
COM1、COM2 ...(不确定 COM4 是否可能存在——我只有 COM3)被 cmd.exe(还有 CON 和 NUL、LPT1)解释为某种文件。有了SET /p Lline1=<somefile
你可以读取文件的第一行。由于括号,这是在 3 行上。