Part 2.
Emulation of a serial port based external
device with software emulator.
Before creation of a hardware prototype, this is
always a good idea to emulate all possible parts without soldering
(assembling )anything and only after successful application of
the obtained knowledge from the PART
1 (and of course, after attentive reading of original document
from Microsoft) I believe, this is good idea to start implementation
of the prototype itself. For software emulation we need a program,
which is able to send the identification data and receives answer
from the host, accordingly to the specified requirements (see
PART 1). Here, we
have an alternative : create our own program or try to find something
ready one. The search on Internet brought the link to the PnP
Com simulator, the utility, created by Igor Cesko, Ing. This
utility can be used freely and it is done in a good professional
way, (Igor, thank you for well-done job) therefore I would suggest
to postpone the creation of our own emulator a bit and use the
found one. You can see visually how PnP Com simulator builds and
sends request and play with all parts of the request (you can
easily change them). Additionally, this utility allows you to
create a template (but not a full workable version) of device
specific INF file, which is used later for installation of our
demo WDM driver (virt.sys).
The template of INF file is built respectively to the applied
information, which you should fill in PnP Com simulator yourself
as it is described below.
In order to use this utility, you need a standard zero-modem cable
and PC with two serial ports. I used two PCs, host PC and target
PC, because I never use developer's PC for experiments and tests,
sometimes it can cost a lot (in the sense of the lost information)
I will provide you with full functional INF file
(srd.inf), which was created with GenInf utility from (WIN2K
DDK) (I have slightly changed the file), a simple driver
(virt.sys) with full support of SYSTEM EVENTS log (sample)(you
can use EventViewer for checking written information), debug output
(sample of log file),
which can be seen with usage of Dbgview (the utility from www.sysinternal.com)
and test utility (test.exe),
to be sure that after successful emulation of "installation
process" of our virtual device the necessary driver is found
and properly loaded. The driver and test utility are present with
full source code. More detail description can be found in project.txt
Please copy them and put it on your hard disk. During installation,
you will be asked about place on your local hard disk, where INF
file and driver
are located.
Let's start using PnP Com simulator. Please follow the action's
sequence, described below:
|
1.
|
Connect the zero-modem cable if it is not connected yet;
|
|
2.
|
Run PnP Com simulator. You see the certain predefined
values' set.
Change them according to our negotiation from
PART 1 in order to use our custom INF file and be
sure that necessary driver will be istalled and loaded
on the target PC (if you use two PCs like me). Later,
when all required changes are done you have to see something
like that.
|
|
3.
|
Select the necessary number of COM port (COM1 in my case)
and press "Simulate PNP" button. The checkbox
"use short characters" must be empty. Visually,
nothing happened, the memo box for program output is empty.
As I told before, OS will not determine the presence of
a new serial port based device automatically in this case.
|
|
4.
|
Open Start->Settings->Control
Panel->System ->Device Manager (I
used two PCs, and had to run the same application (PnP Com
simulator) on the host PC and used Device Manager on the
target PC); main menu "Action" contains item "Scan
for hardware changes", select it. Sometimes, the mentioned
item is invisible, in this case just select any point of
the device's tree in Device Manager window and after that,
open "Action" menu again, you will see the necessary
item.
|
If you do it at first time (I mean the installation
of our virtual driver, otherwise the another scenario can be used),
you should see a window
(the caption is "FOUND NEW HARDWARE") with name of our
device ("Virtual serial port based device"), the member
of COM ID structure, <USER NAME> field. The PnP Com simulator
memo box contains request,
which was built accordingly to the presented information about
our device and this request was sent to selected (by number) serial
port. This is as follow:
Written
PnP:
ASCII:(,SRD0003\12345678\MULTUNCTION\\Virtual serial port based
deviceBB)
HEXA:28 01 2C 53 52 44 30 30 30 33 5C 31 32 33 34 35 36 37 38
5C 4D 55 4C 54 55 4E 43 54 49 4F 4E 5C 5C 56 69 72 74 75 61 6C
20 73 65 72 69 61 6C 20 70 6F 72 74 20 62 61 73 65 64 20 64 65
76 69 63 65 42 42 29
Successfull send COM PnP enumerator.
The way how CRC was calcucated (this is an important
for building custom requests) is the next:
8
bit arithmetic checksum of all characters from Begin PnP to End
PnP inclusive, exclusive of the checksum bytes themselves;
You can see a strange place "\\"between "Class
Name" and "User Name" fields.This is a result of
omitted data in Driver ID"(Compatible Device IDs) in PnP
Com simulator. We did not enter any data in this field, but PnP
Com Simulator put the empty space between two slashes, which are
separators. If you analyze this request you will see it was built
in compliance with COM ID structure's description.(PART
1)
In several seconds (OS tries to find the proper
INF file for the new devices, accordingly our request in ROOT_WINDOWS_DIR\inf
folder) the window "FOUND NEW HARDWARE" disappeared,
you see the standard "Found New Hardware Wizard" window
. Do all necessary steps, installing the driver for the new device,
select the place (see pictures 1,2,3,4),
where you stored INF file and driver after that the installation
procedure has to be finished successfully (see picture 5).
A new group occurred in device's tree of Device Manager (see picture
6). This is "Multifunction
adapters" and this group contains our device "Virtual
Serial Device". If you already have this group, just a new
device (our one) will be added to the existed group. You can check
the properties of the new virtual device, having a look what driver
was installed (see picture 7).
Additional test can be done using test utility or checking the
SYSTEM EVENTS log(sample).
The next interesting question is : how does the
PnP manager determine what INF file is correspond to a particular
device, which has to be installed? The PnP manager is looking
for coincidence between the data stored in INF file (Product ID,
EISA ID) with the data received from the external serial port
based device. Later, after successful instalation (when all necessary
information about our device is stored in registry) PnP manager
compares these data with data, reported by our device.
If you stop PnP Com simulator, the new device will not disappear
in Device Manager( Multifunction adapters) automatically, you
should initialize this process yourself at the same way, when
you tried to initialize the procedure of search of new device.
After that, our test driver will be unloaded. You can run again
the test utility in order to check this fact (absence of virt.sys
in the memory), or use for example, an excellent tool from NUMEGA,
DriverWorkbench.
This installation made following changes in our system:
|
1.
|
ROOT_WINDOWS_DIR\inf folder contains OEMx.inf
file - renamed copy of our original SRD.inf; |
|
2.
|
ROOT_WINDOWS_DIR\SYSTEM32\DRIVERS folder contains virt.sys
file (driver of our virtual device); |
|
3.
|
Registry was changed (as usually, when a new PnP driver
is installed with support of system events log enabled); |
Probably not all related question were covered by
this small description (PART
2), from beginning I assumed, you already have certain experience
of programming but I think, the main goal is reached - we did
necessary software emulation and now ready to create a prototype
(microcontroller based) of PnP compatible serial port based device.
I would recommend next books and materials (I used them myself),
which can be useful for better understanding: