.NET Forum / .NET Framework / New Users / August 2006
using SerialPort in a service
|
|
Thread rating:  |
Jean Paul Mertens - 21 Aug 2006 11:02 GMT 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
Dick Grier - 21 Aug 2006 17:13 GMT 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
 Signature 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.
news.microsoft.com - 21 Aug 2006 19:03 GMT 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
> Hi, > [quoted text clipped - 6 lines] > > Dick Willy Denoyette [MVP] - 21 Aug 2006 19:29 GMT Please post the console application code, we can't help you without it .
Willy.
| Hey Dick, | [quoted text clipped - 43 lines] | > | > Dick Jean Paul Mertens - 22 Aug 2006 06:49 GMT 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
Jean Paul Mertens - 22 Aug 2006 10:17 GMT 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
> Hello, > [quoted text clipped - 50 lines] > > Jean Paul Willy Denoyette [MVP] - 22 Aug 2006 12:08 GMT 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, | [quoted text clipped - 60 lines] | > this.serialPort1.BaudRate = 2400; | > this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
| > // | > // Service1 [quoted text clipped - 5 lines] | > | > Jean Paul Jean Paul Mertens - 22 Aug 2006 14:10 GMT 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
> 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 [quoted text clipped - 3 lines] > > | Hello to all, Snip...
Willy Denoyette [MVP] - 22 Aug 2006 21:44 GMT | 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.
Dick Grier - 22 Aug 2006 17:39 GMT 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
 Signature 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.
Willy Denoyette [MVP] - 22 Aug 2006 21:40 GMT | 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.
Ed Sutton - 22 Aug 2006 21:57 GMT >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
Dick Grier - 22 Aug 2006 17:45 GMT 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
 Signature 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.
Pieter - 22 Aug 2006 08:11 GMT 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...
Dick Grier - 30 Aug 2006 16:13 GMT For anyone who is following this thread... I have placed the example mentioned above on my site. See Software Downloads.
 Signature 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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|