using SerialPort in a service

J

Jean Paul Mertens

Hello,

Someone can tell me why I dont get serial port events in a Service, I
created a separate Thread to open the port but no events are coming up (the
same happens when I use the timer component, using the System.Timers.Timer
it works fine)

tnx in advance

Jean Paul
 
D

Dick Grier

Hi,

I do not know. It "should work," but I haven't written a service under VS
2005 to test it. I will do that sometime soon, just as an experiment.

However, it is perfectly acceptable to use System.Timers.Timer to poll. You
will not (IMO) incure an perfomance degredation, and very little extra
overhead.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
N

news.microsoft.com

Hey Dick,

The Timer question is solved with the System.Timers.Timer so that is not a
problem but the SerialPort seems to be a bigger problem.

I've created a test-service wich only a SerialPort as content.
The serial port is picked from the toolbox in my service component, the
baudrate etc is set with the property's in the designer.
The SerialPort is constructed in the defaul component initialise of the
service constructor where I also set a public counter to 0..
In the OnStart I do an SerialPort.Open().
In the OnStop I do the SerialPort.Close() and write the content of my public
counter to the eventlog,
In the eventhandler I incr. the public counter.

When now I send a string tot the serial port the counter is not incremented
(I have tried to trow an exeption to but noting happens)

Next I have included a timer of 1000 ms testing the IsOpen from the
SerialPort and writing the result of my public counter to the eventlog. the
port is ans stays open but the counter stays to 0

When I use the same code in a WindowsForm app all works fine.

When I try to use the SerialPort in a console application, It does not work
neighter.

Strange but true, I searched the whole internet for somone or somthing about
the SerialPort and the WindowsService but all who ever published something
in thes direction used a P/Invoke and the Win32 kernel.

So far the story here.

Greets

Jean Paul
 
W

Willy Denoyette [MVP]

Please post the console application code, we can't help you without it .

Willy.

| Hey Dick,
|
| The Timer question is solved with the System.Timers.Timer so that is not
a
| problem but the SerialPort seems to be a bigger problem.
|
| I've created a test-service wich only a SerialPort as content.
| The serial port is picked from the toolbox in my service component, the
| baudrate etc is set with the property's in the designer.
| The SerialPort is constructed in the defaul component initialise of the
| service constructor where I also set a public counter to 0..
| In the OnStart I do an SerialPort.Open().
| In the OnStop I do the SerialPort.Close() and write the content of my
public
| counter to the eventlog,
| In the eventhandler I incr. the public counter.
|
| When now I send a string tot the serial port the counter is not
incremented
| (I have tried to trow an exeption to but noting happens)
|
| Next I have included a timer of 1000 ms testing the IsOpen from the
| SerialPort and writing the result of my public counter to the eventlog.
the
| port is ans stays open but the counter stays to 0
|
| When I use the same code in a WindowsForm app all works fine.
|
| When I try to use the SerialPort in a console application, It does not
work
| neighter.
|
| Strange but true, I searched the whole internet for somone or somthing
about
| the SerialPort and the WindowsService but all who ever published something
| in thes direction used a P/Invoke and the Win32 kernel.
|
| So far the story here.
|
| Greets
|
| Jean Paul
|
| "Dick Grier" <[email protected]> schreef in bericht
| | > Hi,
| >
| > I do not know. It "should work," but I haven't written a service under
VS
| > 2005 to test it. I will do that sometime soon, just as an experiment.
| >
| > However, it is perfectly acceptable to use System.Timers.Timer to poll.
| > You will not (IMO) incure an perfomance degredation, and very little
extra
| > overhead.
| >
| > Dick
| >
| > --
| > Richard Grier, MVP
| > Hard & Software
| > Author of Visual Basic Programmer's Guide to Serial Communications,
Fourth
| > Edition,
| > ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised
March
| > 2006.
| > See www.hardandsoftware.net for details and contact information.
| >
|
|
 
J

Jean Paul Mertens

Hello,

Hereby my most simple testcode for the windows service.


namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
serialPort1.Open();
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
serialPort1.Close();
}

private void serialPort1_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
EventLog.WriteEntry("byte", "byte!", EventLogEntryType.Warning);
}
}
}


private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
//
// serialPort1
//
this.serialPort1.BaudRate = 2400;
this.serialPort1.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
//
// Service1
//
this.ServiceName = "Service1";
}

Greetings,

Jean Paul
 
P

Pieter

Strange? I used the VS.NET 2005 Beta 1 to do exactly the same: A Windows
Service with 1 (or more) serial ports, connected to 1 (or more) SMS
Modems... Everything worked fine... I got the events etc...
 
J

Jean Paul Mertens

Hello to all,

I think I've found the reason, I'm using a USB-Serial port and there was
something wrong with the drivers. I first tried it out with the build in
serial port and averything worked fine. With the USB serial port it worked
only in a windows form. I upgraded the USB-Serial drivers tot a newer
version and now the ports works in a service too.

tnx for coorperation anyway, mankind can still search but has to know where
:)

For every one who ever encounters the same problem:
the used hardware: Prolific USB-to-Serial Comm Port
Hardware ID: USB\VID_067B&PID_2303\5&106AD93B&0&2
Working driver: ser2pl.sys Version 2,0,0,18

Greeting

Jean Paul
 
W

Willy Denoyette [MVP]

Weird.... The drivers don't know or don't (shouldn't) behave differently
whether they are driven by a Windows application versus a Service or
whatever.

Willy.

| Hello to all,
|
| I think I've found the reason, I'm using a USB-Serial port and there was
| something wrong with the drivers. I first tried it out with the build in
| serial port and averything worked fine. With the USB serial port it worked
| only in a windows form. I upgraded the USB-Serial drivers tot a newer
| version and now the ports works in a service too.
|
| tnx for coorperation anyway, mankind can still search but has to know
where
| :)
|
| For every one who ever encounters the same problem:
| the used hardware: Prolific USB-to-Serial Comm Port
| Hardware ID: USB\VID_067B&PID_2303\5&106AD93B&0&2
| Working driver: ser2pl.sys Version 2,0,0,18
|
| Greeting
|
| Jean Paul
|
| "Jean Paul Mertens" <[email protected]> schreef in bericht
| | > Hello,
| >
| > Hereby my most simple testcode for the windows service.
| >
| >
| > namespace WindowsService1
| > {
| > public partial class Service1 : ServiceBase
| > {
| > public Service1()
| > {
| > InitializeComponent();
| > }
| >
| > protected override void OnStart(string[] args)
| > {
| > // TODO: Add code here to start your service.
| > serialPort1.Open();
| > }
| >
| > protected override void OnStop()
| > {
| > // TODO: Add code here to perform any tear-down necessary to
| > stop your service.
| > serialPort1.Close();
| > }
| >
| > private void serialPort1_DataReceived(object sender,
| > System.IO.Ports.SerialDataReceivedEventArgs e)
| > {
| > EventLog.WriteEntry("byte", "byte!",
| > EventLogEntryType.Warning);
| > }
| > }
| > }
| >
| >
| > private void InitializeComponent()
| > {
| > this.components = new System.ComponentModel.Container();
| > this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
| > //
| > // serialPort1
| > //
| > this.serialPort1.BaudRate = 2400;
| > this.serialPort1.DataReceived += new
| >
System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
| > //
| > // Service1
| > //
| > this.ServiceName = "Service1";
| > }
| >
| > Greetings,
| >
| > Jean Paul
| >
| >
| >
|
|
 
J

Jean Paul Mertens

Hey Willy,

Well it's not the first time that I encounter applications who don't work
with the USB to Serial converters. Touth those are the best I could find the
are only few apps who don't work with them and always when the drivers are
accessed on a 'special' way. Whe have here a Cutter-plotter who works in a
serial port with no hardware handshaking and it also don't work with the
convertors. When I use the standard serial ports there are no problems.

Neverless,in the mean time my service is up and running fine now. Lets hope
it stays so ...

Tnx a lot cu.

Jean Paul
 
D

Dick Grier

Hi,

You are right, the driver should work in a Service if it works with a
WinForms app. An update of the driver IS a great idea, though. I've run
into a number of USB adapters that simply didn't work correctly
"off-the-shelf."

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
D

Dick Grier

Hello Jean,

I will place my "Service example and article" on my web site, as soon as
I've completed it. Just for fun, I created a GPS Location Service, that
writes current latitude/longitude date/time to the Event Log, as decoded
from a discovered GPS receiver. This is not a "practical app," since I
wouldn't want it running often on my machine. However, it may be
illustrative.

It should be available in a few days.

Naturally, anyone interested will have to suffer with VB, but... The code
will (virtually) be the same for C#.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
W

Willy Denoyette [MVP]

| Hi,
|
| You are right, the driver should work in a Service if it works with a
| WinForms app. An update of the driver IS a great idea, though. I've run
| into a number of USB adapters that simply didn't work correctly
| "off-the-shelf."

Dick, when they did not work they didn't work at all right, no matter the
application calling into it? Anyway, hope to see this driver mess
disapearing (well say in the next decade ;-)) in Vista.

Willy.
 
W

Willy Denoyette [MVP]

| Hey Willy,
....
| Lets hope it stays so ...
|

Same here, but remember, unless you know exactly what the reason was, no
single problem is realy solved, s**t can always happen ;-) if you don't;

Willy.
 
E

Ed Sutton

I've run into a number of USB adapters that simply didn't work correctly
"off-the-shelf."

I will not longer purchase a USB Serial adapter unless I can determine
it is using one of the FT232BM or related devices made by FTDIChip.
Their virtual COM port drivers are WHQL certified and made my
application installation very smooth. I have had bad luck with Belkin
and other USB/serial devices. A pretty inexpensive supplier of these
USB/Serial adapters is a place named CoolGear.

http://www.ftdichip.com

-Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top