1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
NAME
IO::Termios - supply termios(3) methods to IO::Handle objects
SYNOPSIS
use IO::Termios;
my $term = IO::Termios->open( "/dev/ttyS0", "9600,8,n,1" )
or die "Cannot open ttyS0 - $!";
$term->print( "Hello world\n" ); # Still an IO::Handle
while( <$term> ) {
print "A line from ttyS0: $_";
}
DESCRIPTION
This class extends the generic IO::Handle object class by providing
methods which access the system's terminal control termios(3)
operations. These methods are primarily of interest when dealing with
TTY devices, including serial ports.
The flag-setting methods will apply to any TTY device, such as a
pseudo-tty, and are useful for controlling such flags as the ECHO flag,
to disable local echo.
my $stdin = IO::Termios->new( \*STDIN );
$stdin->setflag_echo( 0 );
When dealing with a serial port the line mode method is useful for
setting the basic serial parameters such as baud rate, and the modem
line control methods can be used to access the hardware handshaking
lines.
my $ttyS0 = IO::Termios->open( "/dev/ttyS0" );
$ttyS0->set_mode( "19200,8,n,1" );
$ttyS0->set_modem({ dsr => 1, cts => 1 });
Upgrading STDIN/STDOUT/STDERR
If you pass the -upgrade option at import time, any of STDIN, STDOUT or
STDERR that are found to be TTY wrappers are automatically upgraded
into IO::Termios instances.
use IO::Termios -upgrade;
STDIN->setflag_echo(0);
Arbitrary Baud Rates on Linux
Linux supports a non-POSIX extension to the usual termios interface,
which allows arbitrary baud rates to be set. IO::Termios can
automatically make use of this ability if the Linux::Termios2 module is
installed. If so, this will be used automatically and transparently, to
allow the set*baud methods to set any rate allowed by the
kernel/driver. If not, then only the POSIX-compatible rates may be
used.
CONSTRUCTORS
new
$term = IO::Termios->new();
Construct a new IO::Termios object around the terminal for the program.
This is found by checking if any of STDIN, STDOUT or STDERR are a
terminal. The first one that's found is used. An error occurs if no
terminal can be found by this method.
new (handle)
$term = IO::Termios->new( $handle );
Construct a new IO::Termios object around the given filehandle.
open
$term = IO::Termios->open( $path, $modestr, $flags );
Open the given path, and return a new IO::Termios object around the
filehandle. If the open call fails, undef is returned.
If $modestr is provided, the constructor will pass it to the set_mode
method before returning.
If $flags is provided, it will be passed on to the underlying sysopen()
call used to open the filehandle. It should contain a bitwise-or
combination of O_* flags from the Fcntl module - for example O_NOCTTY
or O_NDELAY. The value O_RDWR will be added to this; the caller does
not need to specify it directly. For example:
use Fcntl qw( O_NOCTTY O_NDELAY );
$term = IO::Termios->open( "/dev/ttyS0", O_NOCTTY|O_NDELAY );
$term->setflag_clocal( 1 );
$term->blocking( 1 );
METHODS
getattr
$attrs = $term->getattr;
Makes a tcgetattr() call on the underlying filehandle, and returns a
IO::Termios::Attrs object.
If the tcgetattr() call fails, undef is returned.
setattr
$term->setattr( $attrs );
Makes a tcsetattr() call on the underlying file handle, setting
attributes from the given IO::Termios::Attrs object.
If the tcsetattr() call fails, undef is returned. Otherwise, a true
value is returned.
set_mode
get_mode
$term->set_mode( $modestr );
$modestr = $term->get_mode;
Accessor for the derived "mode string", which is a comma-joined
concatenation of the baud rate, character size, parity mode, and stop
size in a format such as
19200,8,n,1
When setting the mode string, trailing components may be omitted
meaning their value will not be affected.
tiocmget
tiocmset
$bits = $term->tiocmget;
$term->tiocmset( $bits );
Accessor for the modem line control bits. Takes or returns a bitmask of
values.
tiocmbic
tiocmbis
$term->tiocmbic( $bits );
$term->tiocmbis( $bits );
Bitwise mutator methods for the modem line control bits. tiocmbic will
clear just the bits provided and leave the others unchanged; tiocmbis
will set them.
get_modem
$flags = $term->get_modem;
Returns a hash reference containing named flags corresponding to the
modem line control bits. Any bit that is set will yield a key in the
returned hash of the same name. The bit names are
dtr dsr rts cts cd ri
set_modem
$term->set_modem( $flags );
Changes the modem line control bit flags as given by the hash
reference. Each bit to be changed should be represented by a key in the
$flags hash of the names given above. False values will be cleared,
true values will be set. Other flags will not be altered.
getmodem_BIT
setmodem_BIT
$set = $term->getmodem_BIT;
$term->setmodem_BIT( $set );
Accessor methods for each of the modem line control bits. A set of
methods exists for each of the named modem control bits given above.
FLAG-ACCESSOR METHODS
Theses methods are implemented in terms of the lower level methods, but
provide an interface which is more abstract, and easier to re-implement
on other non-POSIX systems. These should be used in preference to the
lower ones.
For efficiency, when getting or setting a large number of flags, it may
be more efficient to call getattr, then operate on the returned object,
before possibly passing it to setattr. The returned IO::Termios::Attrs
object supports the same methods as documented here.
The following two sections of code are therefore equivalent, though the
latter is more efficient as it only calls setattr once.
$term->setbaud( 38400 );
$term->setcsize( 8 );
$term->setparity( 'n' );
$term->setstop( 1 );
my $attrs = $term->getattr;
$attrs->setbaud( 38400 );
$attrs->setcsize( 8 );
$attrs->setparity( 'n' );
$attrs->setstop( 1 );
$term->setattr( $attrs );
However, a convenient shortcut method is provided for the common case
of setting the baud rate, character size, parity and stop size all at
the same time. This is set_mode:
$term->set_mode( "38400,8,n,1" );
getibaud
getobaud
setibaud
setobaud
setbaud
$baud = $term->getibaud;
$baud = $term->getobaud;
$term->setibaud( $baud );
$term->setobaud( $baud );
$term->setbaud( $baud );
Convenience accessors for the ispeed and ospeed. $baud is an integer
directly giving the line rate, instead of one of the Bnnn constants.
getcsize
setcsize
$bits = $term->getcsize;
$term->setcsize( $bits );
Convenience accessor for the CSIZE bits of c_cflag. $bits is an integer
5 to 8.
getparity
setparity
$parity = $term->getparity;
$term->setparity( $parity );
Convenience accessor for the PARENB and PARODD bits of c_cflag. $parity
is n, o or e.
getstop
setstop
$stop = $term->getstop;
$term->setstop( $stop );
Convenience accessor for the CSTOPB bit of c_cflag. $stop is 1 or 2.
cfmakeraw
$term->cfmakeraw;
Since version 0.07.
Adjusts several bit flags to put the terminal into a "raw" mode. Input
is available a character at a time, echo is disabled, and all special
processing of input and output characters is disabled.
getflag_FLAG
setflag_FLAG
$mode = $term->getflag_FLAG;
$term->setflag_FLAG( $mode );
Accessors for various control flags. The following methods are defined
for specific flags:
inlcr
Since version 0.09.
The INLCR bit of the c_iflag. This translates NL to CR on input.
igncr
Since version 0.09.
The IGNCR bit of the c_iflag. This ignores incoming CR characters.
icrnl
Since version 0.09.
The ICRNL bit of the c_iflag. This translates CR to NL on input, unless
IGNCR is also set.
ignbrk
Since version 0.09.
The IGNBRK bit of the c_iflag. This controls whether incoming break
conditions are ignored entirely.
brkint
Since version 0.09.
The BRKINT bit of the c_iflag. This controls whether non-ignored
incoming break conditions result in a SIGINT signal being delivered to
the process. If not, such a condition reads as a nul byte.
parmrk
Since version 0.09.
The PARMRK bit of the c_iflag. This controls how parity errors and
break conditions are handled.
opost
Since version 0.07.
The OPOST bit of the c_oflag. This enables system-specific
post-processing on output.
cread
The CREAD bit of the c_cflag. This enables the receiver.
hupcl
The HUPCL bit of the c_cflag. This lowers the modem control lines after
the last process closes the device.
clocal
The CLOCAL bit of the c_cflag. This controls whether local mode is
enabled; which if set, ignores modem control lines.
icanon
The ICANON bit of c_lflag. This is called "canonical" mode and controls
whether the terminal's line-editing feature will be used to return a
whole line (if true), or if individual bytes from keystrokes will be
returned as they are available (if false).
echo
The ECHO bit of c_lflag. This controls whether input characters are
echoed back to the terminal.
setflags
$term->setflags( @flags );
Since version 0.09.
A convenient wrapper to calling multiple flag setting methods in a
sequence.
Each flag is specified by name, in lower case, prefixed by either a +
symbol to enable it, or - to disable. For example:
$term->setflags( "+igncr", "+opost", "+clocal", "-echo" );
TODO
* Adding more getflag_*/setflag_* convenience wrappers
SEE ALSO
* IO::Tty - Import Tty control constants
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
|