首页 > 解决方案 > 在 Perl 中向串口发送下划线

问题描述

当我向串行端口 (COM42) 发送下划线时,就好像我向串行端口发送了一个退格键。例如,如果我发送“Perl_Great”,我的串行端口会显示“PerGreat” 我目前以这种方式配置了串行端口。是否有一个参数可以将串行端口配置为不同的编码,这样它就不会将下划线视为退格?

use Win32::SerialPort;
my $port = new Win32::SerialPort( "\\\\.\\" . $self->{Port} )
        or $self->__Error( "Could not open com port $self->{Port}\n\t$^E\n", 'FATAL' );

$port->baudrate($self->{BaudRate});
$port->parity($self->{Parity});
$port->databits($self->{DataBits});
$port->stopbits($self->{StopBits});
$port->handshake( "none" );
$port->are_match( "\r" );     # the symbol that "lookfor" will be looking for in a blocking call
$port->read_const_time(2000); #timeout for blocking read (ms)
$port->lookclear;

$port->write_settings();

my $command = "Perl_Great";
$port->write($command);  # where $command is previously loaded with "Perl_Great".  I checked with a Win32::MsgBox That the string is intact at this point

标签: perlencodingserial-port

解决方案


您的问题的简单答案是-“可能不是”:-)

您的问题“是否有一个参数可以将串行端口配置为不同的编码,以便它不会将下划线视为退格?”,可能会更好地表述为“是什么导致字符串“Perl_Great”的接收者去掉两个字符,只得到“PerGreat”?

您询问编码,并提到 UTF8,您是否验证过“Perl_Great”中的下划线是 eq “\x5f”,而不是一些神奇的 utf-8 字符串


推荐阅读