The constructor is new with a PortName (as the Registry knows it) specified. This will do a CreateFile, get the available options and capabilities via the Win32 API, and create the object. The port is not yet ready for read/write access. First, the desired parameter settings must be established. Since these are tuning constants for an underlying hardware driver in the Operating System, they should all checked for validity by the method calls that set them. The initialize method takes a list of required parameters and confirms they have been set. For others, it will attempt to deduce defaults from the hardware or from other parameters. The initialize method returns the number of faults (zero if the port is setup ok). The update_DCB method writes a new Device Control Block to complete the startup and allow the port to be used. Ports are opened for binary transfers. A separate binmode is not needed. The USER must release the object if initialize or update_DCB does not succeed.

Version 0.15 adds an optional $quiet parameter to new. Failure to open a port prints a error message to STDOUT by default. Since only one application at a time can "own" the port, one source of failure was "port in use". There was previously no way to check this without getting a "fail message". Setting $quiet disables this built-in message. It also returns 0 instead of undef if the port is unavailable (still FALSE, used for testing this condition - other faults may still return undef). Use of $quiet only applies to new.

The fault checking in initialize consists in verifying an _N_$item internal variable exists for each $item in the input list. The _N_$item is created for each parameter that is set either directly or by default. A derived class must create the _N_$items for any varibles it adds to the base class if it wants initialize to check them. Win32API::CommPort supports the following:

Some individual parameters (eg. baudrate) can be changed after the initialization is completed. These will automatically update the Device Control Block as required. The init_done method indicates when initialize has completed successfully.

The PortName maps to both the Registry Device Name and the Properties associated with that device. A single Physical port can be accessed using two or more Device Names. But the options and setup data will differ significantly in the two cases. A typical example is a Modem on port "COM2". Both of these PortNames open the same Physical hardware:

$P1 is a "generic" serial port. $P2 includes all of $P1 plus a variety of modem-specific added options and features. The "raw" API calls return different size configuration structures in the two cases. Win32 uses the "\\.\" prefix to identify "named" devices. Since both names use the same Physical hardware, they can not both be used at the same time. The OS will complain. Consider this A Good Thing.

Version 0.16 adds pulse methods for the RTS, BREAK, and DTR bits. The pulse methods assume the bit is in the opposite state when the method is called. They set the requested state, delay the specified number of milliseconds, set the opposite state, and again delay the specified time. These methods are designed to support devices, such as the X10 "FireCracker" control and some modems, which require pulses on these lines to signal specific events or data.

Version 0.16 also adds experimental support for the rest of the option bits available through the Device Control Block. They have not been extensively tested and these settings are NOT saved in the configuration file by Win32::SerialPort. Please let me know if one does not work as advertised. [Win32 API bit designation]

The get_tick_count method is a wrapper around the Win32::GetTickCount() function. It matches a corresponding method in Device::SerialPort which does not have access to the Win32:: namespace. It still returns time in milliseconds - but can be used in cross-platform scripts.

The Win32 Serial Comm API provides extensive information concerning the capabilities and options available for a specific port (and instance). "Modem" ports have different capabilties than "RS-232" ports - even if they share the same Hardware. Many traditional modem actions are handled via TAPI. "Fax" ports have another set of options - and are accessed via MAPI. Yet many of the same low-level API commands and data structures are "common" to each type ("Modem" is implemented as an "RS-232" superset). In addition, Win95 supports a variety of legacy hardware (e.g fixed 134.5 baud) while WinNT has hooks for ISDN, 16-data-bit paths, and 256Kbaud.

There are a large number of possible configuration and option parameters. To facilitate checking option validity in scripts, most configuration methods can be used in two different ways:

method called with an argument

The parameter is set to the argument, if valid. An invalid argument returns false (undef) and the parameter is unchanged. After init_done, the port will be updated immediately if allowed. Otherwise, the value will be applied when update_DCB is called.

method called with no argument in scalar context

The current value is returned. If the value is not initialized either directly or by default, return "undef" which will parse to false. For binary selections (true/false), return the current value. All current values from "multivalue" selections will parse to true. Current values may differ from requested values until init_done. There is no way to see requests which have not yet been applied. Setting the same parameter again overwrites the first request. Test the return value of the setting method to check "success".

Asynchronous (Background) I/O

This version now handles Polling (do if Ready), Synchronous (block until Ready), and Asynchronous Modes (begin and test if Ready) with the timeout choices provided by the API. No effort has yet been made to interact with Windows events. But background I/O has been used successfully with the Perl Tk modules and callbacks from the event loop.

Timeouts

The API provides two timing models. The first applies only to reading and essentially determines Read Not Ready by checking the time between consecutive characters. The ReadFile operation returns if that time exceeds the value set by is_read_interval. It does this by timestamping each character. It appears that at least one character must by received in everyreadcall to the API to initialize the mechanism. The timer is then reset by each succeeding character. If no characters are received, the read will block indefinitely.

Setting is_read_interval to 0xffffffff will do a non-blocking read. The ReadFile returns immediately whether or not any characters are actually read. This replicates the behavior of the API.

The other model defines the total time allowed to complete the operation. A fixed overhead time is added to the product of bytes and per_byte_time. A wide variety of timeout options can be defined by selecting the three parameters: fixed, each, and size.

When reading a known number of characters, the Read_Total mechanism is recommended. This mechanism MUST be used with Win32::SerialPort tied FileHandles because the tie methods can make multiple internal API calls. The Read_Interval mechanism is suitable for a read_bg method that expects a response of variable or unknown size. You should then also set a long Read_Total timeout as a "backup" in case no bytes are received.

The constants for the Device Control Block returned by GetCommState and updated by SetCommState. Again, included mostly for completeness. But there are some combinations of "FM_f" settings which are not currently supported by high-level commands. If you need one of those, please report the lack as a bug.

The object returned by new is NOT a Filehandle. You will be disappointed if you try to use it as one.

e.g. the following is WRONG!!____print $PortObj "some text";

Win32::SerialPort supports accessing ports via Tied Filehandles.

An important note about Win32 filenames. The reserved device names such as COM1, AUX, LPT1, CON, PRN can NOT be used as filenames. Hence "COM2.cfg" would not be usable for $Configuration_File_Name.

This module uses Win32::API extensively. The raw API calls are very unforgiving. You will certainly want to start perl with the -w switch. If you can, use strict as well. Try to ferret out all the syntax and usage problems BEFORE issuing the API calls (many of which modify tuning constants in hardware device drivers....not where you want to look for bugs).

The current version of the module has been designed for testing using the ActiveState and Core (GS 5.004_02) ports of Perl for Win32 without requiring a compiler or using XS. In every case, compatibility has been selected over performance. Since everything is (sometimes convoluted but still pure) Perl, you can fix flaws and change limits if required. But please file a bug report if you do. This module has been tested with each of the binary perl versions for which Win32::API is supported: AS builds 315, 316, and 500-509 and GS 5.004_02. It has only been tested on Intel hardware.

With all the options, this module needs a good tutorial. It doesn't have a complete one yet. A "How to get started" tutorial appeared The Perl Journal #13 (March 1999). The demo programs are a good starting point for additional examples.

The size of the Win32 buffers are selectable with is_buffers. But each read method currently uses a fixed internal buffer of 4096 bytes. This can be changed in the module source. The read-only internal_buffer method will give the current size. There are other fixed internal buffers as well. But no one has needed to change those. The XS version will support dynamic buffer sizing.

Lots of options are just "passed through from the API". Some probably shouldn't be used together. The module validates the obvious choices when possible. For something really fancy, you may need additional API documentation. Available from Micro$oft Pre$$.

ActiveState ports of Perl for Win32 before build 500 do not support the tools for building extensions and so will not support later versions of this extension. In particular, the automated install and test scripts in this distribution work differently with ActiveState builds 3xx.

There is no parameter checking on the "raw" API calls. You probably should be familiar with using the calls in "C" before doing much experimenting.

On Win32, a port must close before it can be reopened again by the same process. If a physical port can be accessed using more than one name (see above), all names are treated as one. The perl script can also be run multiple times within a single batch file or shell script. The Makefile.PL spawns subshells with backticks to run the test suite on Perl 5.003 - ugly, but it works.

On NT, a read_done or write_done returns False if a background operation is aborted by a purge. Win95 returns True.

A few NT systems seem to set can_parity_enable true, but do not actually support setting is_parity_enable. This may be a characteristic of certain third-party serial drivers. Or a Microsoft bug. I have been able to reproduce it on my system, but not identify a specific cause.

Most of the code in this module has been stable since version 0.12. Version 0.20 adds explicit support for COM10++ and USB - although the functionality existed before. Perl ports before 5.6.0 are no longer supported for test or install. The modules themselves work with 5.003. 1 April 2010.