批处理脚本使用COM端口读/写数据 [英] Batch script to read/write data using COM port

查看:220
本文介绍了批处理脚本使用COM端口读/写数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个批处理脚本,可以读取和写入到一个COM端口(在我的情况,COM1)。

I need to write a batch script that can read and write to a COM port (in my case, COM1).

我知道我可以通过将数据发送到一个COM端口

I know that I can send data to a COM port using

echo hello > COM1

和我可以读取数据来使用到一个文本文件中。

And I can read data coming in to a text file using

type COM1 > sample.txt

我想不断捕获进入COM端口到一个文本文件中的数据,并能根据什么数据刚读的COM端口上发送命令到COM端口。有没有办法在批处理脚本中做到这一点?

I'd like to be constantly capturing the data coming into the COM port into a text file and be able to send commands to the COM port depending on what data was just read on the COM port. Is there a way to do this in a batch script?

难道是更容易/更好地做到这在bash脚本?我会怎么做在bash?

Would it be easier/better to do it in a bash script? How would I do it in bash?

编辑:
我要读出的数据是一系统的引导的输出到UEFI壳。然后,当它到达那里,输入一些指令和捕获这些命令的输出。然后,引导OS(捕捉引导的输出),输入一些更多的命令到Linux壳和捕获它们的输出。它不包含字符的确切数目。

The data I wish to read is the output of the boot of a system to an UEFI shell. Then, when it gets there, enter some commands and capture the output of those commands. Then, boot an OS (capturing the output of the boot), enter some more commands to the Linux shell and capture the output of them. It does not contain an exact number of characters.

如果一个bash脚本是唯一的答案,那就是好的。我可以与工作。我使用的cygwin当前运行在Windows 7中的Bash版本4.1.10(4)。

If a bash script is the only answer, that is ok. I can work with that. I am currently running version 4.1.10(4) of bash in Windows 7 using cygwin.

推荐答案

有一些不同的东西玩弄批处理脚本使我相信没有什么能比在批处理脚本和标准的Windows命令行工具来工作。 设置/ P COM1读取数据时立即返回,以及像键入COM1 将行复制大块线串行数据。

Playing around with a few different things with batch scripts leads me to believe nothing is going to work in with a batch script and the standard Windows command line tools. set /P returns immediately when reading from COM1, and something like type COM1 will copy the serial data in large chunks line by line.

我不得不使用Cygwin的bash更好的运气。下面是接收从 COM1 线和呼应他们回到一个简单的脚本。它退出时它接收开始跳槽一条线。您可以通过使用串行链路的另一端的终端模拟器或者只是使用类似语句回声退出&GT测试出来; COM1

I had better luck using Cygwin bash. Here's a simple script that receives lines from COM1 and echoes them back. It exits when it receives a line starting with "quit". You can test it out by using a terminal emulator on the other end of the serial link or just using statements like echo quit > COM1.

CR="$(echo -e '\r')"
exec 4<> /dev/com1

cat <&4 | while :
do
    IFS="$CR" read -r line 
    case "$line" in
    quit*)
        echo "goodbye$CR" >&4
        break
        ;;
    *)
        echo "recieved line: $line"
        echo "recieved line: $line$CR" >&4
        ;;
    esac
done

CR 变量保存在这个例子中是用来剥离其关闭输入线,用于在输出时,他们终止了与CR LF行回车符串口线。根据你的BIOS究竟如何表现,你可能会或可能不会需要做在自己的脚本。

The CR variable holds a carriage return character which in this example is used to strip it off the input lines and used to terminate lines with CR LF when outputting them over the serial line. Depending how exactly your BIOS behaves you may or may not need to do this in your own script.

EXEC 4℃;&GT;为/ dev / COM1 行是至关重要的。这个用于读和写都打开的COM端口一次。仅适用于Windows允许一个COM端口是打开一次,因此,如果没有这样做,将无法读取和写入COM端口。在 4 表示分配给文件描述符4和 EXEC 语句保持其开放的脚本的其余部分。

The exec 4<> /dev/com1 line is crucial. This opens the COM port once for both reading and writing. Windows only allows a COM port to be open once, so if this wasn't done it wouldn't be possible to read and write to the COM port. The 4 means that is assigned to file descriptor 4 and the exec statement keeps it open for the rest of the script.

猫&LT;及4 | 部分也很重要。不幸的是,似乎是在Cygwin的bash中有一个错误:它会尝试倒带文件描述符,如果它读取过去行的末尾。这适用于文件,但这样的数据丢失如果不是确实为串口。要解决这个问题,该脚本从管道代替,这是bash足够聪明,不要试图倒带读取。

The cat <&4 | part is also important. Unfortunately there seems to be a bug in Cygwin bash where it will try to rewind the file descriptor if it reads past the end of a line. This works for files, but it doesn't for serial ports so data gets lost. To workaround this problem the script reads from a pipe instead, which bash is smart enough to not try to rewind.

原因设置 IFS =$ CR是剥离在一行的末尾回车如前所述,并没有去掉什么,而读。在命令使用IFS串,打破了输入线成词。您可以使用你的优势,将其设置为不同的值,使其更容易解析BIOS输出。

The reason for setting IFS="$CR" is to strip off the carriage return at the end of a line as mentioned before, and to not strip off anything while reading. The read command uses the IFS string to break up the input line into words. You may be able to use this to your advantage and set it to a different value to make it easier to parse the BIOS output.

的其余的细节是pretty简单。为使其不以治疗 \\ -r 选项>特殊字符。根据什么样的行结束符你的BIOS希望你有三种不同的方式,你可以写你的echo语句:

The rest of the details are pretty straightforward. The -r option for read causes it not to treat \ characters specially. Depending on what sort of line endings your BIOS expects you have three different ways you can write your echo statements:

echo "Both CR and LF line ending$CR" >&4
echo -n "CR only line ending$CR" >&4
echo "LF only line ending" >&4

一件事这个脚本没有做像设置波特率COM端口参数和流量控制。这是使用正常 MODE COM1 命令可能是最好的完成。 Cygwin的具有等效的stty 命令,但它似乎并不支持所有的参数。

One thing this script doesn't do it set the COM port parameters like baud rate and flow control. This is probably best done using the normal MODE COM1 command. Cygwin has an equivalent stty command, but it doesn't appear to support all the parameters.

另一种完全不同的选择是使用期望。如果你发现它很难得到bash的解析,并适当向你的BIOS输出回应,那么你可以考虑使用该网址。这样的事情就是它的设计,虽然有点学习曲线,如果你还不熟悉TCL的。它可以作为一个标准的Cygwin包。

Another entirely different option is to use Expect. If you find that it's hard to get bash to parse and respond appropriately to your BIOS's output then you might consider using that instead. This sort of thing is what its designed for, though there's a bit of learning curve if you're not already familiar with TCL. It's available as a standard Cygwin package.

这篇关于批处理脚本使用COM端口读/写数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆