By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,357 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,357 IT Pros & Developers. It's quick & easy.

using SerialPort in a service

P: n/a
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
Aug 21 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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.
Aug 21 '06 #2

P: n/a
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" <dick_grierNOSPAM@.msn.comschreef in bericht
news:ui**************@TK2MSFTNGP03.phx.gbl...
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.

Aug 21 '06 #3

P: n/a
Please post the console application code, we can't help you without it .

Willy.

"news.microsoft.com" <je*******@opelwilly.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
| 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" <dick_grierNOSPAM@.msn.comschreef in bericht
| news:ui**************@TK2MSFTNGP03.phx.gbl...
| 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.
| >
|
|
Aug 21 '06 #4

P: n/a
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(thi s.serialPort1_DataReceived);
//
// Service1
//
this.ServiceName = "Service1";
}

Greetings,

Jean Paul

Aug 22 '06 #5

P: n/a
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...
Aug 22 '06 #6

P: n/a
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" <ON****@newsgroups.nospamschreef in bericht
news:e6**************@TK2MSFTNGP04.phx.gbl...
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(thi s.serialPort1_DataReceived);
//
// Service1
//
this.ServiceName = "Service1";
}

Greetings,

Jean Paul

Aug 22 '06 #7

P: n/a
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.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP02.phx.gbl...
| 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" <ON****@newsgroups.nospamschreef in bericht
| news:e6**************@TK2MSFTNGP04.phx.gbl...
| 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(thi s.serialPort1_DataReceived);
| //
| // Service1
| //
| this.ServiceName = "Service1";
| }
| >
| Greetings,
| >
| Jean Paul
| >
| >
| >
|
|
Aug 22 '06 #8

P: n/a
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
"Willy Denoyette [MVP]" <wi*************@telenet.beschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
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.

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP02.phx.gbl...
| Hello to all,
|
Snip...
>

Aug 22 '06 #9

P: n/a
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.
Aug 22 '06 #10

P: n/a
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.
Aug 22 '06 #11

P: n/a

"Dick Grier" <dick_grierNOSPAM@.msn.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
| 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.


Aug 22 '06 #12

P: n/a

"Jean Paul Mertens" <ON****@newsgroups.nospamwrote in message
news:e%****************@TK2MSFTNGP04.phx.gbl...
| 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.

Aug 22 '06 #13

P: n/a
>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
Aug 22 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.