netcat
(nc
)通过TCP以太网向Teledyne LeCroy T3PS3000电源设备发送命令。如果我使用echo
,设备无法正确接收命令,但如果我使用printf
,则可以正常工作。为什么?# Example command to Ethernet-connected digital power supply over TCP
# fails
echo 'measure:voltage? ch1' | timeout 0.2 nc 192.168.0.1 5025
# works
printf 'measure:voltage? ch1' | timeout 0.2 nc 192.168.0.1 5025
参考文献:
timeout
cmd: https://unix.stackexchange.com/questions/492766/usage-of-nc-with-timeouts-in-ms/492796#492796- Update/Note (written after I wrote the answer, so the
printf
form below is correct): without thetimeout
command, we would have to use the-w
option withnetcat
, but it accepts only whole integer wait period timeouts in seconds, like this (notice the-w 1
to set the 'w'ait period, or timeout, to 1 whole second):printf '%s' "my command to send" | nc -w1 192.168.0.1 5025
- Update/Note (written after I wrote the answer, so the
- Teledyne LeCroy T3PS3000 power supply: from my git & Linux cmds, help, tips & tricks - Gabriel.txt document in my eRCaGuy_dotfiles repo:
- For help, contact:
Teledyne LeCroy Support
support@teledynelecroy.com
700 Chestnut Ridge Road
Chestnut Ridge, NY 10977
(800) 553-2769 Option 3 - It's not in the manual, so I emailed them and they told me:
The T3PS3000 uses socket communication and the default port is 5025.
- Manual (Quick Start Guide): http://cdn.teledynelecroy.com/files/manuals/t3ps3000-quick-start-guide.pdf
- p24, "Chapter 3 Remote Control" is where the command interface begins.
- For help, contact:
echo
的各种实现和“因此,您不能使用echo
显示不受控制的数据”),请参阅以下链接:https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo - Jeff Schallerecho
也明确指出了这一点。(上面的 [unix.se] 链接提供了更详细的信息)。 - Charles Duffyprintf '%s' "string"
比使用printf "string"
更加可靠,因为前者将字符串强制视为文字而非格式化字符串。 - Charles Duffy%
、反斜杠或其他在格式化字符串中具有特殊意义的字符,则会以与原始输入不匹配的格式进行输出。 - Charles Duffy%s
,例如,这不会发送任何东西:printf "%s"
。输出为空,因为printf
将第一个参数解释为格式字符串,而%s
在格式字符串中有特殊意义。但是这确实将我的%s
作为文字发送:printf“%s”“%s”
。 输出为%s
。我会在我的答案中更新这个新信息。 - Gabriel Staples