Linux 在 Perl 中写入串行端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10312654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:58:41  来源:igfitidea点击:

Writing to serial port in Perl

linuxperlserial-portexpect

提问by Strudle

I need to communicate with a serial port inside a Perl script. I need to send characters and read the input and search for strings.

我需要与 Perl 脚本中的串行端口进行通信。我需要发送字符并读取输入并搜索字符串。

What is the simplest way to achieve this? By using "expect" or by opening the /dev/ttys0 device in Perl itself ? Or some other method ?

实现这一目标的最简单方法是什么?通过使用“expect”或通过在 Perl 本身中打开 /dev/ttys0 设备?或者其他什么方法?

I prefer to use perl but I don't know if it is simple and featured as expect.

我更喜欢使用 perl,但我不知道它是否像预期的那样简单和特色。

采纳答案by askovpen

my $port = new Device::SerialPort("/dev/ttyS0"); 
$port->user_msg(ON); 
$port->baudrate(9600); 
$port->parity("none"); 
$port->databits(8); 
$port->stopbits(1); 
$port->handshake("xoff"); 
$port->write_settings;

$port->lookclear; 
$port->write("some command to com-port");

my $answer = $port->lookfor;
# or my $answer=$port->read(255);

回答by rpg

You can try Win32::SerialPortfor Win32 and Device::SerialPortfor linux.

你可以试试Win32::SerialPortWin32 和Device::SerialPortlinux。