Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Driver filter for Serial Port

434 views
Skip to first unread message

Alex

unread,
May 29, 2004, 2:45:18 AM5/29/04
to
Hi all !
My teacher requires me to write a driver filter for Serial Port ( COM port )
with some requirements :
- Server synchronizes system times with the Client .
- in the Server side , there is one Application to communicate between User
and System .
- in the Client Side , there is no application .
- When User want to synchronize time , he only click on the Button in the
Application in Server , The server sends a request to Client , The Driver
in client side must receive the request and implement synchronization !

Ok, but I do not understand , How the Drivers can communicate without help
from User Application ??? I think that all the Routines in Drivers must be
called by User Application , and it's impossible to implement communication
between Routines in one Driver ( for Ex, Routine DispatchRead can not
communicate with Routine DispatchWrite ) . In My Work , how can The Driver
in Client Side do all works by itself without help from User Application ?
My Teacher said about usage of Mutex Object in Client Side to Wait and
implement request from Server , but I don't believe that is possible !
Pls help me to explaint !

Thank you so much !

Slobodan Brcin (eMVP)

unread,
May 28, 2004, 5:03:24 PM5/28/04
to
Hi Alex,

> Ok, but I do not understand , How the Drivers can communicate without help
> from User Application ???

Easy.

Look for function called PsCreateSystemThread. Well then if you have thread you can do what ever you want without user mode
application interaction, right?

Best regards,
Slobodan


Alex

unread,
May 30, 2004, 1:57:46 AM5/30/04
to

> Look for function called PsCreateSystemThread. Well then if you have
thread you can do what ever you want without user mode
> application interaction, right?
>
> Best regards,
> Slobodan
>
>

Ok , thank you so much !
I have read about the System Thread after U telt me . But Where must I put
the Start of the Thread ?? In DriverEntry ? or in DispatchCreate ?
I am using Numega , I don't know where to start my thread? .
Thanks!


Slobodan Brcin (eMVP)

unread,
May 29, 2004, 3:09:55 PM5/29/04
to
Alex,

That will depend on your driver design mostly.
Anyhow you can call it from what ever point that suits your needs as long as you are satisfying following requirement.
"Callers of this routine must be running at IRQL PASSIVE_LEVEL."

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:empBr5aR...@TK2MSFTNGP09.phx.gbl...

Alex

unread,
May 30, 2004, 3:22:40 PM5/30/04
to
Hi threre !

My filter driver has worked well . But after I modified like that ( only add
Systhread ) .
Now , when I start my driver , my Computer immediately restarts , Why ??
Pls help me !
thanks


#pragma code_seg("INIT")

DECLARE_DRIVER_CLASS(CommFilterDriver, NULL)

//////////////////////////////////////////////////////////////////////////
// DriverEntry
//

KSystemThread sysThread;


VOID ThreadMain( )
{
DbgPrint("Start Thread ...");

NTSTATUS status;
KTimer timer;
ASSERT( KeGetCurrentIrql() <= DISPATCH_LEVEL );
timer.Initialize( SynchronizationTimer );


LARGE_INTEGER duetime = {0};
#define TIME_INTERVAL 1000
timer.SetPeriodic( duetime, TIME_INTERVAL );


int dwCounter = 0;

while( dwCounter <= 10 )
{
timer.Wait( KernelMode, FALSE, NULL, Executive );
DbgPrint(" Timer counter = %d ", dwCounter );
dwCounter++;


}
timer.Cancel();
DbgPrint(("POLLING - Terminating polling thread\n"));

sysThread.Terminate(STATUS_SUCCESS);


}


VOID StopThread( )
{

if (sysThread)
{ // wait for thread to die
sysThread.Wait( KernelMode, FALSE, NULL ,Executive);

} // wait for thread to die
}

NTSTATUS CommFilterDriver::DriverEntry(PUNICODE_STRING RegistryPath)
//
// Driver initialization entry point
//
{
// Create an instance of the control device
m_ControlDevice = new (NonPagedPool) FilterControlDevice();
ASSERT( KeGetCurrentIrql() <= PASSIVE_LEVEL );
sysThread.Start( (PKSTART_ROUTINE)ThreadMain, NULL, THREAD_ALL_ACCESS );

if ( m_ControlDevice )
{
NTSTATUS status = m_ControlDevice->ConstructorStatus();
if ( !NT_SUCCESS(status) )
delete m_ControlDevice;
return status;
}
else
return STATUS_INSUFFICIENT_RESOURCES;
}
#pragma code_seg()

VOID CommFilterDriver::Unload(void)
{
KPRIORITY OrigPriority;

StopThread( );

// Lower priority temporarily, in order to ensure that all
// other threads in driver have completed before unloading.
// (TBD: find a better way to handle this problem)
OrigPriority = KeSetPriorityThread( KeGetCurrentThread(), 1 );
KeSetPriorityThread( KeGetCurrentThread(), OrigPriority );

// Delete any devices not already deleted
DeleteDevices();
}


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message
news:egKOVDbR...@TK2MSFTNGP09.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 30, 2004, 6:01:17 AM5/30/04
to
Sorry Alex,

I can't help you here. I'm using DDK functions without third party wrappers.

> Now , when I start my driver , my Computer immediately restarts , Why ??

There is a good thing called remote debugger. KD or windbg from MS you can download new version from site. So you will be able to
step trough your code if you have two computers, if not you are probably doomed while writing drivers blindly.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:%23c65Z7h...@TK2MSFTNGP12.phx.gbl...

Alex

unread,
May 30, 2004, 7:04:40 PM5/30/04
to
Thank you , Slobodan !
I have felt in deadlock ! :-)
Pls tell me , How can i use Mutex ( replace for Thread ) to Notice when the
signal came to the Comm Port ?? ( remember that , my task is keeping track
for Signal came to Comm Port without any call from User mode ).. can u
explain the Mechanism ? ( I am still student , so I can not understand well
allthing as you )

Thank you so much !

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:uZF7a1iR...@TK2MSFTNGP12.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 30, 2004, 8:51:38 AM5/30/04
to
What do you mean by Mutex replace for Thread.

In your case you should have one thread. That will use standard read and write major codes for reading and writing data, and for
configuring port you can use IOCTL_SERIAL_* codes.

Use google groups to search trough old NG archives. This will probably give you all help you need for this particular project.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&group=microsoft.public.win32.programmer.kernel

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:e814b3jR...@tk2msftngp13.phx.gbl...

Alex

unread,
May 30, 2004, 8:58:04 PM5/30/04
to
Ok , thank you very much !
Now my Thread has worked .
But I wonder, how Can i " hook " the standard Read and Write to my Thread
? ( I use one Event to wake up my thread )
Pls help me to explain !
Thanks again !

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#qdAnUkR...@TK2MSFTNGP12.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 30, 2004, 10:56:09 AM5/30/04
to
Hi Alex,

I have no idea what you are trying to do in your project, and why you want to use filter driver.
You can make non filter driver that will open desired com, and use it in similar manners that you would use it from user mode.

You will have your driver (no user mode application). And it will do work you wanted it to do.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:uUdcy2kR...@TK2MSFTNGP12.phx.gbl...

Alex

unread,
May 30, 2004, 11:31:43 PM5/30/04
to
Thank you so much!
Maybe you tell me about .dll driver ( no Usermode Application , but it's
User mode Driver )... right??
Regards!

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#gP3NalR...@tk2msftngp13.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 30, 2004, 1:00:49 PM5/30/04
to
Alex,

You can write regular driver (.sys) that execute as any other driver. All drivers share same address space, and can create as many
threads as you need.
From threads you can open/close other driver use all IOCTL, read, write and use all functions defined in DDK for your OS.

I hope that this is understandable enough.

Regards,
Slobodan

PS:
Maybe you have troubles to understand how to install this type of driver?

"Alex" <Chip...@hotmail.com> wrote in message news:%23bj8rMm...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
May 31, 2004, 6:47:46 AM5/31/04
to
Thank Slobodan a lot !
I am continuing to write my filter driver , and now my thread has worked
well as expected !
by the way , I have read a documentation of MS about Serial Port, I have not
found Which Request ( IOCTL_XXX) to perform the event when the Signal came
to Com port . If you know , please tell me !
regards!

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:OGRlPgm...@TK2MSFTNGP10.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 30, 2004, 7:58:05 PM5/30/04
to
Alex,

I have no idea how you are planning to write filter driver.
Lower filter, upper filter, reason behind this (why?).

What is signal?
Byte receive?
Handshake input pins? break condition?
I thought that you want to make regular serial communication, right?

First you need to open and reserve port.
Then you will have to initialize parameters like BPS, flow control etc.

If you do that successfully you will have idea how to proceed with next step using threads.

BTW:
Filter driver is usually only good if you want to monitor and modify already initialized communication.

Regards,
Slobodan


"Alex" <Chip...@hotmail.com> wrote in message news:Oje3XAqR...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
May 31, 2004, 2:17:00 PM5/31/04
to
Thank you !
Because of a stupid reason , I have never opened successfully my Com port ,
So i can not write the driver as you advised me .I am trying to open ,
but..not success by ZwOpenFile or ZwCreateFile ..The fragment like that .
Pls see the code below .. by the way. I want my thread to be awaked when
there is a byte came in my Port...it's a signal i need !
Thanks for the enthusiasm !

HANDLE fileOpen;
OBJECT_ATTRIBUTES objAttr;
UNICODE_STRING sLinkName;
#define COM_NAME L"\\DosDevices\\COM1" /* or L''COM1" */
RtlInitUnicodeString( &sLinkName, COM_NAME);

InitializeObjectAttributes(
&objAttr,
&sLinkName,
OBJ_INHERIT | OBJ_PERMANENT | OBJ_EXCLUSIVE | OBJ_CASE_INSENSITIVE
|OBJ_OPENIF,
NULL,
NULL);
LARGE_INTEGER nSize;
IO_STATUS_BLOCK ioStat;


NTSTATUS statusOpen = ZwCreateFile(
OUT &fileOpen,
IN FILE_READ_DATA,
IN &objAttr,
OUT &ioStat,
&nSize,
IN FILE_ATTRIBUTE_NORMAL,
IN FILE_SHARE_READ,
IN FILE_OPEN,
IN FILE_WRITE_THROUGH,
IN NULL,
IN 0
);
#if DBG
DbgPrint("statusOpen= %X.", statusOpen);
#endif


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:ui7GpJqR...@TK2MSFTNGP12.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 7:52:08 AM5/31/04
to
Alex,

Make sure that your driver start after the serial driver. The easiest way to do this is trough inf file.
Although you can always do this programmatically.
BTW: This is relevant only when you boot your computer. If you just start your driver when system is running then this is no issue.

Also consider using something like functions:
IoCallDriver
IoBuildDeviceIoControlRequest

#define SERIAL_DEVICE_NAME0 L"\\Device\\Serial0"

UNICODE_STRING ntDeviceName;
RtlInitUnicodeString(&ntDeviceName, SERIAL_DEVICE_NAME0);
PFO=0;
PDO=0;
NTSTATUS status=IoGetDeviceObjectPointer(&ntDeviceName, FILE_ALL_ACCESS,
&PFO, &PDO);

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:%23%23EfU7tR...@TK2MSFTNGP09.phx.gbl...

Alex

unread,
May 31, 2004, 8:47:48 PM5/31/04
to
Sicenrely thank Slobodan !
But , my COM port always ready to work, I can open it by CreateFile in user
mode normally .
and by the way , why Serials0 ?? I think that , Serial0 is only Function
driver , We call COM1 to call to the Stack ( Drivers Chain ) . right?
Even I tried with COM1 or Serial0 , I have never openned it...hic hic
Regards !
Alex!

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:##XxVYwRE...@TK2MSFTNGP12.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 9:50:18 AM5/31/04
to
Error you mentioned in email:

// {Access Denied}
// A process has requested access to an object, but has not been granted those access rights.
//
#define STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L)

Try some unopened serial port.

And please use this NG for all your posts so someone else can help you.

Regards,
Slobodan

BTW: What OS are you using for testing purposes.

"Alex" <Chip...@hotmail.com> wrote in message news:%23bpg0Vx...@TK2MSFTNGP10.phx.gbl...

Alexander Grigoriev

unread,
May 31, 2004, 11:01:48 AM5/31/04
to
Remove OBJ_PERMANENT, OBJ_EXCLUSIVE, OBJ_INHERIT, FILE_WRITE_THROUGH, nSize.

"Alex" <Chip...@hotmail.com> wrote in message

news:%23%23EfU7tR...@TK2MSFTNGP09.phx.gbl...

Alex

unread,
May 31, 2004, 10:23:36 PM5/31/04
to

Ok !
Thank you !
I have successfully open COM port after restarting computer (by The fragment
you gave me , but notice the same name , I use with ZwCreateFile , It did
not work ! ) .( I use Windows XP ).
But now , The other problem that I can not Write to the Port now .

#define SERIAL_DEVICE_NAME0 L"\\Device\\Serial0"
UNICODE_STRING ntDeviceName;
RtlInitUnicodeString(&ntDeviceName, SERIAL_DEVICE_NAME0);

NTSTATUS status=IoGetDeviceObjectPointer(&ntDeviceName,
FILE_ALL_ACCESS,
&pdx->m_hComm, &pdx->m_pdoComm);

HANDLE m_hFile = (HANDLE) pdx->m_hComm;
IO_STATUS_BLOCK ioStat;
UCHAR pBuffer[8];
ULONG dwRead = 8;

NTSTATUS statusWrite = ZwWriteFile(
m_hFile,
NULL,
NULL,
NULL,
&ioStat,
pBuffer,
dwRead,
NULL,
NULL
);

if( !NT_SUCCESS( status ) )
DbgPrint("Open COMM failed !");
else
DbgPrint("Open COMM OK !\n");
DbgPrint(" status open COMM = %X ", status );
if( !NT_SUCCESS( statusWrite ) )
DbgPrint("Write COMM failed !");
else
DbgPrint("Write COMM OK !\n");
DbgPrint(" status Write COMM = %X ", statusWrite );

and the result :

Example= DriverEntry successfully completed.
Open COMM OK !
status open COMM = 0
Write COMM failed !
status Write COMM = C0000008
Unloaded successfully


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:uH7$BaxREH...@TK2MSFTNGP12.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 12:32:34 PM5/31/04
to
This should be exercise for you. (To read documentations and samples)

Do the read similarly as write.
And please notice that event is used. So you can implement this is a way that fit your requirements.

NTSTATUS SerialWrite( const void *WriteBuffer, ULONG NumBytes, IO_STATUS_BLOCK &iosb)
{
if(!PDO) return STATUS_UNSUCCESSFUL;
NTSTATUS status;
PIRP irp;
LARGE_INTEGER startingOffset;
KEVENT event;
startingOffset.QuadPart = (LONGLONG) 0;
KeInitializeEvent(&event, NotificationEvent, FALSE);
irp = IoBuildSynchronousFsdRequest( IRP_MJ_WRITE, PDO, (PVOID)WriteBuffer, NumBytes, &startingOffset, &event, &iosb );
if(!irp) return STATUS_INSUFFICIENT_RESOURCES;
status = IoCallDriver(PDO, irp);
if (status == STATUS_PENDING)
status = KeWaitForSingleObject( &event, Executive, KernelMode, FALSE, NULL);
if(status!=STATUS_SUCCESS) return status;
return iosb.Status;
}

Regards,
Slobodan


"Alex" <Chip...@hotmail.com> wrote in message news:Oj0IRLyR...@TK2MSFTNGP10.phx.gbl...

Doron Holan [MS]

unread,
May 31, 2004, 12:35:11 PM5/31/04
to
the PFILE_OBJECT is not a HANDLE and you can't cast it to a handle. There
is no way for you to convert it to a handle either because a handle is an
entry in a process table and you have already opened the "file". Instead,
call IoBuildSynchronousFsdRequest passing IRP_MJ_WRITE as the function, the
(4th) parameter returned to you from IoGetDeviceObjectPointer as the 2nd
parameter and you can figure out the rest of them. The call IoCallDriver
with the returned PIRP and the device object returned to you from
IoGetDeviceObjectPointer

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


"Alex" <Chip...@hotmail.com> wrote in message

news:Oj0IRLyR...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
Jun 1, 2004, 1:36:23 AM6/1/04
to
Hi Slobodan !
A Speacial THANK to your enthusiasm !
OK , it's very useful for me now , And I am now reading some documentation
about those functions .
By the Way , Only tell me Which other functions i need to read know to
implement my Work by your manner ! I have no more time now , So I need help
from U , only tell me Which Functions I must to find and read in
Documentation !
Thanks again !
Best Regards!


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#9ipv0yR...@tk2msftngp13.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 3:15:49 PM5/31/04
to
Alex,

You will have to use things like
irp=IoBuildDeviceIoControlRequest(IOCTL_SERIAL_SET_BAUD_RATE

Read about all IOCTL_SERIAL_* codes since you will need them to initialize your serial port.

So:
1. Open thread.
2. Open serial port.
3. Initialize serial port.
4. Make loop.
5 Wait for read event or thread close.
When you get some info parse them and do the time stuff.
6. go to 4.
7. You can even forget PnP start/stop support so this will simplify your driver even more.

This is very crude but it will work.

Good luck with that.
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:uDtT$2zREH...@tk2msftngp13.phx.gbl...

Alex

unread,
Jun 1, 2004, 2:37:02 AM6/1/04
to
Ok , thanks a lot !
maybe they are enough for me !
But , Slobodan !
I have tested the Function u gave me . But I jumped into Infinite loop of
WaitForSingleObject .


NTSTATUS SerialWrite( const void *WriteBuffer, ULONG NumBytes,

PIO_STATUS_BLOCK iosb, PDEVICE_OBJECT pdo)
{
if(!pdo) return STATUS_UNSUCCESSFUL;


NTSTATUS status;
PIRP irp;
LARGE_INTEGER startingOffset;
KEVENT event;
startingOffset.QuadPart = (LONGLONG) 0;
KeInitializeEvent(&event, NotificationEvent, FALSE);

DbgPrint("initial Event OK in Write ");
irp = IoBuildSynchronousFsdRequest( IRP_MJ_WRITE, pdo,
(PVOID)WriteBuffer, NumBytes, &startingOffset, &event, iosb );
if(!irp) return STATUS_INSUFFICIENT_RESOURCES;
DbgPrint("Build IRP OK !");
status = IoCallDriver( pdo, irp );
DbgPrint("maybe waiting...");


if (status == STATUS_PENDING)
status = KeWaitForSingleObject( &event, Executive, KernelMode, FALSE,
NULL);
if(status!=STATUS_SUCCESS) return status;

return iosb->Status;
}

and I have get this result :

initial Event OK in Write
Build IRP OK !
maybe waiting...


Best regards !


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#z96JQ0R...@tk2msftngp13.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 4:01:52 PM5/31/04
to
Alex you never mentioned if you ever written one functional program for this purpose in user mode.
I suggest you to make one trough API only not some fancy third party classes but trough CreateFile etc.
Then write on paper all calls and structures you used for initialization of your port.
After that it will be easy for you to port all calls to driver using IOCTL codes.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:%23NT$4Y0REH...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
Jun 1, 2004, 3:27:06 AM6/1/04
to
Hi Slobodan !
Yeah , I have write my Program in API and Use it to Communicate via Comm
Port normally , it works very well , of course .
Maybe you mean, I need set All Parameter of COMM port before call it inmy
Driver ??
Ok , I do it now !
Thanks alot
Regards!


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:OzBDqp0R...@TK2MSFTNGP10.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 5:01:52 PM5/31/04
to
Yup.

"Alex" <Chip...@hotmail.com> wrote in message news:uCJi300R...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
Jun 1, 2004, 7:16:45 AM6/1/04
to
Hi Slobodan !
pls help me !

I have set all necessary parameters for COM port, but still not get Result ,
Write always in the wait state ...


////////////////////////////////////////////////////////////////////////

SERIAL_BAUD_RATE serBaudRate;
serBaudRate.BaudRate = SERIAL_BAUD_2400;
NTSTATUS statusBaudRate = SetBaudRate( &serBaudRate, &ioStat, pdo );

////////////////////////////////////////////////////////////////////////

SERIAL_LINE_CONTROL serLineControl;
serLineControl.Parity = SERIAL_EVEN_PARITY ;
serLineControl.StopBits = SERIAL_2_STOP;
serLineControl.WordLength = SERIAL_8_DATA ;
NTSTATUS statusLineControl = SetLineControl( &serLineControl, &ioStat,
pdo );

////////////////////////////////////////////////////////////////////////

#define MAXDWORD 0xffffffff
SERIAL_TIMEOUTS serTimeOuts;
serTimeOuts.ReadIntervalTimeout = MAXDWORD ;
serTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD ;
serTimeOuts.ReadTotalTimeoutConstant = MAXDWORD - 1;
serTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
serTimeOuts.WriteTotalTimeoutConstant = 0 ;
NTSTATUS timeOutStats = SetTimeOuts( &serTimeOuts, &ioStat, pdo );

////////////////////////////////////////////////////////////////////////

UCHAR pBuffer[2];
ULONG dwRead = 2;
statusWrite = SerialWrite( pBuffer, dwRead, &ioStat, pdo );

NTSTATUS SerialWrite( const void *WriteBuffer, ULONG NumBytes,
PIO_STATUS_BLOCK iosb, PDEVICE_OBJECT pdo)
{
if(!pdo) return STATUS_UNSUCCESSFUL;
NTSTATUS status;
PIRP irp;
LARGE_INTEGER startingOffset;
KEVENT event;
startingOffset.QuadPart = (LONGLONG) 0;
KeInitializeEvent(&event, NotificationEvent, FALSE);
DbgPrint("initial Event OK in Write ");
irp = IoBuildSynchronousFsdRequest( IRP_MJ_WRITE, pdo,
(PVOID)WriteBuffer, NumBytes, &startingOffset, &event, iosb );
if(!irp) return STATUS_INSUFFICIENT_RESOURCES;
DbgPrint("Build IRP OK !");
status = IoCallDriver( pdo, irp );

DbgPrint("Waiting in SerialWrite...");


if (status == STATUS_PENDING)
status = KeWaitForSingleObject( &event, Executive, KernelMode, FALSE,
NULL);

DbgPrint("Jumped out of Wait in SerialWrite !");
if(status!=STATUS_SUCCESS) return status;
DbgPrint(" Write COMM successfully !");
return iosb->Status;

}


NTSTATUS SetBaudRate( PSERIAL_BAUD_RATE serBaudRate, PIO_STATUS_BLOCK iosb,
PDEVICE_OBJECT pdo )
{


NTSTATUS status;
PIRP irp;
LARGE_INTEGER startingOffset;
KEVENT event;
startingOffset.QuadPart = (LONGLONG) 0;
KeInitializeEvent(&event, NotificationEvent, FALSE);

DbgPrint("initial Event OK in SetBaudRate ");


irp = IoBuildDeviceIoControlRequest(
IOCTL_SERIAL_SET_BAUD_RATE,
pdo,
serBaudRate,
sizeof(SERIAL_BAUD_RATE),
NULL,
0,
FALSE,
&event,
iosb );

if(!irp) return STATUS_INSUFFICIENT_RESOURCES;
DbgPrint("Build IRP SetBaudRate OK !");


status = IoCallDriver( pdo, irp );

DbgPrint("Waiting in setBaundRate...");


if (status == STATUS_PENDING)
status = KeWaitForSingleObject( &event, Executive, KernelMode, FALSE,
NULL);

DbgPrint("Jumped out of Wait in SetBaudRate !");
if(status!=STATUS_SUCCESS) return status;
DbgPrint("SetBaudRate successfully !");
return iosb->Status;

}

NTSTATUS SetTimeOuts( PSERIAL_TIMEOUTS serTimeOuts, PIO_STATUS_BLOCK iosb,
PDEVICE_OBJECT pdo )
{


NTSTATUS status;
PIRP irp;
LARGE_INTEGER startingOffset;
KEVENT event;
startingOffset.QuadPart = (LONGLONG) 0;
KeInitializeEvent(&event, NotificationEvent, FALSE);

DbgPrint("initial Event OK in SetTimeOuts ");


irp = IoBuildDeviceIoControlRequest(
IOCTL_SERIAL_SET_TIMEOUTS,
pdo,
serTimeOuts,
sizeof(SERIAL_TIMEOUTS),
NULL,
0,
FALSE,
&event,
iosb );

if(!irp) return STATUS_INSUFFICIENT_RESOURCES;
DbgPrint("Build IRP SetTimeOuts OK !");


status = IoCallDriver( pdo, irp );

DbgPrint("Waiting in SetTimeOuts...");


if (status == STATUS_PENDING)
status = KeWaitForSingleObject( &event, Executive, KernelMode, FALSE,
NULL);

DbgPrint("Jumped out of Wait in SetTimeOuts !");
if(status!=STATUS_SUCCESS) return status;
DbgPrint("SetTimeOuts successfully !");
return iosb->Status;
}

And this is the result :

Open COMM successfully !
initial Event OK in SetBaudRate
Build IRP SetBaudRate OK !
Waiting in setBaundRate...
Jumped out of Wait in SetBaudRate !
SetBaudRate successfully !
initial Event OK in SetTimeOuts
Build IRP SetTimeOuts OK !
Waiting in SetTimeOuts...
Jumped out of Wait in SetTimeOuts !
SetTimeOuts successfully !


initial Event OK in Write
Build IRP OK !

Waiting in SerialWrite...


Best Regards!

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 9:00:37 PM5/31/04
to
What about handshake and your cable?
IOCTL_SERIAL_SET_HANDFLOW?

Idea about this if used is that receiving end can block transmition of data that it can't handle. Also this requires apropriate
cable. If you don't have full cable or you don't want to use this option then you must disable this behaviour.

Also start by receiving data. Make your server send data constantly and try seeing in debuger what happens.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:ePrVL12R...@TK2MSFTNGP12.phx.gbl...

Alex

unread,
Jun 1, 2004, 8:32:24 AM6/1/04
to
Thank Slobodan !
I do not use IOCTL_SERIAL_SET_HANDFLOW in my API User mode Application , and
it works normally , So i think , for a bit simplify ,I did not touch it in
my Driver too . In fact, I have tried to set HandFlow , but I could not
find the apropriate things in file ntddkser.h and serial.h , so I could
not do it ! My Cable is full ( 9 pins ) . So if you can , pls tell me some
about setting parameters for IOCTL_SERIAL_SET_HANDFLOW , I tried to find,
but found nothing ....hic hic
Thanks you again . permit me to call you my TEACHER .
Regards!
Alex .

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:OSeYJR3R...@TK2MSFTNGP12.phx.gbl...

Slobodan Brcin (eMVP)

unread,
May 31, 2004, 10:23:48 PM5/31/04
to
Examine DCB structure from Platform SDK you will find meaning of values there.
Although this won't help you since you said that this is working on full cable.

BTW: Make sure that you are using/accessing right serial port number. I don't see the reason for access denied error in one of your
previous posts.

If you have oscilloscope or Voltmeter or nice pass-through with leds that indicate state of you communication lines. You should
drive manually trough codes some of handshake outputs to certain state just to make sure that you are on the right post and that
everything is ok. This is simple operations and do not require proper param initialization to work.

Regards,
Slobodan


"Alex" <Chip...@hotmail.com> wrote in message news:erEtdf3R...@tk2msftngp13.phx.gbl...

Alex

unread,
Jun 1, 2004, 12:13:05 PM6/1/04
to

Slobodan !
I read some different documentations about IoBuildSynchronousFsdRequest.
IoCallDriver , they always talk about Driver Lower in a Driver Chain ,
maybe We must attach it to a driver stack ? and why IoBuildSynchronous , a
not IoBuildAsynchronous ?
Best Regards!
Alex

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:eOpau$3REH...@TK2MSFTNGP10.phx.gbl...

Slobodan Brcin (eMVP)

unread,
Jun 1, 2004, 7:11:45 AM6/1/04
to
Alex,

IoBuildSynchronousFsdRequest IoCallDriver, they always talk about Driver Lower in a Driver Chain. That is only matter of point of
view. If you have PDO obtained trough IoGetDeviceObjectPointer that is enough for IoCallDriver to work.

>and why IoBuildSynchronous , a not IoBuildAsynchronous

And why not creating and filling that IRP manually? Do it as you think that it is good for your specific case . All these functions
are there for us to use them.

Slobodan

BTW:
Before you continue asking more and more questions.
How many RS232 cables do you have?
Do you have firewire?

How do you debug your computer?
Do you have started application for receive at all times on your server?


"Alex" <Chip...@hotmail.com> wrote in message news:e%23WGxa5R...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
Jun 1, 2004, 9:30:27 PM6/1/04
to
Hi Slobodan !
OK, thanks a lot !
I will now answer your questions:
- I have a Null Modem cable with 9 legs . I have tested it by communicating
in Windows Application , It works very well .
- I have no firewire .
I have tried to test like that :
- I run my Driver in My computer , and Run a Windows Application ( user
mode ) in the other for Getting all bytes from Comm Port( my driver sent )
. I get a strange result :
In my driver , i write only one byte ( have tried with different
values ) . But In the Other computer , I always get 3 bytes result ( all af
them = 0 ) .
yup!. I don't understand , maybe i have set wrong parameters for Comm port
in my driver ???

Regards !
Waiting for help from you !


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:O0rwFm8R...@TK2MSFTNGP09.phx.gbl...

Slobodan Brcin (eMVP)

unread,
Jun 1, 2004, 10:34:32 AM6/1/04
to
What I do not understand is how you use remote debugger if you have only one cable.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:etOhoT%23REH...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
Jun 2, 2004, 12:09:57 AM6/2/04
to

Hi !
I only manual debug by Watching the result in DbgViewer . I get start with
WinDbg , but did not understand , it's completely complicated.
Regards!


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:uJuwZX#REHA...@tk2msftngp13.phx.gbl...

Slobodan Brcin (eMVP)

unread,
Jun 1, 2004, 1:30:13 PM6/1/04
to
OK. Then I have no idea except what I already said. Try controlling some output pins directly to some state to see if you have at
least control, if you do then only what remain for you is to set all "valid" values.

BTW: Try setting time outs for write just to see if it will ever timeout write request.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:%23sCXWr$REHA...@TK2MSFTNGP10.phx.gbl...

Alex

unread,
Jun 2, 2004, 4:21:21 AM6/2/04
to
Ura.......
I have found a useful link :
http://www.wd-3.com/archive/SerialAttachedDevices.htm
Maybe We forgot to call IRP_MJ_CREATE to Open the Comm Port.
I have modified some thing , as this Link , and now the SerialWrite has
finished well ( without infinite loop in WaitForSingleObject ). But.... The
result has not been got ...
Regards!


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#muDC6$REHA...@TK2MSFTNGP10.phx.gbl...

Doron Holan [MS]

unread,
Jun 1, 2004, 11:00:19 PM6/1/04
to
IoGetDeviceObjectPointer will send the IRP_MJ_CREATE for you.

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"Alex" <Chip...@hotmail.com> wrote in message

news:uSDx03BS...@TK2MSFTNGP12.phx.gbl...

Alex

unread,
Jun 2, 2004, 3:55:05 PM6/2/04
to
Thank you so much ! Doron Holan !
It's OK now , but I call ObDereferenceObject ( pCommFileObject ) not
successfully to close Comm port .
And , I do not understand why , when I send 3 bytes, in other computer
always gets 6 bytes ( all = 0 )
regards!


"Doron Holan [MS]" <dor...@nospam.microsoft.com> wrote in message
news:#dRR#2ESEH...@TK2MSFTNGP10.phx.gbl...

Slobodan Brcin (eMVP)

unread,
Jun 2, 2004, 5:54:51 AM6/2/04
to
ObDereferenceObject make sure that there are no pending calls. Also make sure which pointer you use.
What is the error code?

Perhaps your speed does not match on both sides.

Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:%23OL9d7H...@TK2MSFTNGP09.phx.gbl...

Alex

unread,
Jun 2, 2004, 5:48:54 PM6/2/04
to
Hi Slobodan !
ObDereferenceObject does not return an error code ( i think so , it returns
void ) . I have set the speed in two = 2400 . I wonder that , all necessary
parameters for Comm port must be set? I think , In API User mode , if we
don't set anything , it means that Inside API functions sets it by default .
But now We are stay in Kernel mode, There are no default values for these
parameters???? so ...We must set all of them ?


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#JnDxfIS...@tk2msftngp13.phx.gbl...

Alex

unread,
Jun 3, 2004, 1:42:15 AM6/3/04
to
O la la !
I have found my Mistakessssssssss... all the parameters have been set
INVALID... because I saw in serial.h ( in ddk ) , and though that they are
valid...But now I pass all manual . like that . And it works OKKKKKKK..... A
special THANK to Slobodan and Doron Holan...!

SERIAL_BAUD_RATE serBaudRate;
serBaudRate.BaudRate = 2400; //SERIAL_BAUD_2400;
NTSTATUS statusBaudRate = SetBaudRate( &serBaudRate, fdo );

////////////////////////////////////////////////////////////////////////

SERIAL_LINE_CONTROL serLineControl;
serLineControl.Parity = 2; //SERIAL_EVEN_PARITY ;
serLineControl.StopBits = 2; //SERIAL_2_STOP;
serLineControl.WordLength = 8; //SERIAL_8_DATA ;
NTSTATUS statusLineControl = SetLineControl( &serLineControl, fdo );

////////////////////////////////////////////////////////////////////////
#define MAXDWORD 0xffffffff
SERIAL_TIMEOUTS serTimeOuts;
serTimeOuts.ReadIntervalTimeout = MAXDWORD ;
serTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD ;
serTimeOuts.ReadTotalTimeoutConstant = MAXDWORD - 1;

serTimeOuts.WriteTotalTimeoutMultiplier = MAXDWORD ;
serTimeOuts.WriteTotalTimeoutConstant = MAXDWORD - 1 ;
NTSTATUS timeOutStatus = SetTimeOuts( &serTimeOuts, fdo );


"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#JnDxfIS...@tk2msftngp13.phx.gbl...

Slobodan Brcin (eMVP)

unread,
Jun 2, 2004, 6:09:41 PM6/2/04
to
Glad to be able to help.

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:OmrWcDNS...@TK2MSFTNGP09.phx.gbl...

Alex

unread,
Jun 3, 2004, 3:58:15 PM6/3/04
to
Hi all!
I have a small problem . I don't understand why , when I have unloaded my
Driver like below , but the Comm Port maybe has been not closed , through I
see that It has called IRP_MJ_CLOSE successfully ( the Programm PortMon -
Russinovich will help u to trace all the Ports ) .When I open the Comm port
in second time , It can't be opned , and the computer immediately restarts
...( maybe there is a conflict of resource - CommPort- , so my Driver did
not successfully closed Comm )

NTSTATUS StopCommPort( PDEVICE_OBJECT fdo )
{
ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);

NTSTATUS status= STATUS_SUCCESS;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
if (!pdx->isPortOpen)
return STATUS_SUCCESS;

pdx->isPortOpen = FALSE;
KEVENT event;
IO_STATUS_BLOCK iosb;
KeInitializeEvent(&event, NotificationEvent, FALSE);
PIRP Irp = IoBuildSynchronousFsdRequest(IRP_MJ_FLUSH_BUFFERS,
pdx->LowerDeviceObject,
NULL, 0, NULL, &event, &iosb);
if (!Irp)
return STATUS_UNSUCCESSFUL;
PIO_STACK_LOCATION stack = IoGetNextIrpStackLocation(Irp);
stack->MajorFunction = IRP_MJ_CLOSE;
status = IoCallDriver(pdx->LowerDeviceObject, Irp);


if (status == STATUS_PENDING)
status = KeWaitForSingleObject(&event, Executive, KernelMode, FALSE,
NULL);

return status;
}

VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject)
{

PDEVICE_OBJECT pdo = pDriverObject->DeviceObject;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) pdo->DeviceExtension;
StopCommPort( pdo );
ObDereferenceObject( pdx->LowerDeviceObject);
UNICODE_STRING *pLinkName = &(pdx->ustrSymLinkName);
IoDeleteSymbolicLink(pLinkName);
IoDeleteDevice( pdx->fdo);
DbgPrint("Unloaded successfully !");
}

and the result :

0.00006342 System IRP_MJ_CREATE
Serial0 SUCCESS Options: Open
0.00000391 System IRP_MJ_CLEANUP
Serial0 SUCCESS
0.00001620 System IOCTL_SERIAL_SET_BAUD_RATE Serial0
SUCCESS Rate: 2400
0.00000726 System IOCTL_SERIAL_SET_LINE_CONTROL Serial0
SUCCESS StopBits: ERROR Parity: EVEN WordLength: 8
0.00000251 System IOCTL_SERIAL_SET_TIMEOUTS Serial0
* 0xC0000023
0.00004889 System IRP_MJ_WRITE
Serial0 SUCCESS Length 8: CFIO{...
0.05021700 System IRP_MJ_CLOSE
Serial0 SUCCESS

"Slobodan Brcin (eMVP)" <sbr...@ptt.yu> wrote in message

news:#9xUY6OS...@TK2MSFTNGP11.phx.gbl...

Slobodan Brcin (eMVP)

unread,
Jun 4, 2004, 11:23:15 PM6/4/04
to
Hi,

Why not just simple: ObDereferenceObject(PFO);

You never used IRP_MJ_CREATE, so why do you want to use IRP_MJ_CLOSE?

Regards,
Slobodan

"Alex" <Chip...@hotmail.com> wrote in message news:ul8D5hUS...@tk2msftngp13.phx.gbl...

0 new messages