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