|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
using SerialPort in a serviceHello,
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 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. 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 Show quote "Dick Grier" <dick_grierNOSPAM@.msn.com> schreef in bericht news:ui0UzyTxGHA.2260@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. > Please post the console application code, we can't help you without it .
Willy. Show quote "news.microsoft.com" <jean.p***@opelwilly.com> wrote in message news:%23xFVUwUxGHA.4576@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.com> schreef in bericht | news:ui0UzyTxGHA.2260@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. | > | | 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 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 Show quote "Jean Paul Mertens" <ON7AMI@newsgroups.nospam> schreef in bericht news:e6gAI7axGHA.4460@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(this.serialPort1_DataReceived); > // > // Service1 > // > this.ServiceName = "Service1"; > } > > Greetings, > > 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 whatever. Willy. Show quote "Jean Paul Mertens" <ON7AMI@newsgroups.nospam> wrote in message System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);news:eJUZbvcxGHA.4444@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" <ON7AMI@newsgroups.nospam> schreef in bericht | news:e6gAI7axGHA.4460@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 | > Show quote | > // | > // Service1 | > // | > this.ServiceName = "Service1"; | > } | > | > Greetings, | > | > Jean Paul | > | > | > | | 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 Show quote "Willy Denoyette [MVP]" <willy.denoye***@telenet.be> schreef in bericht news:%23NABbtdxGHA.2432@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" <ON7AMI@newsgroups.nospam> wrote in message > news:eJUZbvcxGHA.4444@TK2MSFTNGP02.phx.gbl... > | Hello to all, > | Snip... > "Jean Paul Mertens" <ON7AMI@newsgroups.nospam> wrote in message Same here, but remember, unless you know exactly what the reason was, no news:e%23KhhxexGHA.4876@TK2MSFTNGP04.phx.gbl... | Hey Willy, .... | Lets hope it stays so ... | single problem is realy solved, s**t can always happen ;-) if you don't; Willy. 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. "Dick Grier" <dick_grierNOSPAM@.msn.com> wrote in message Dick, when they did not work they didn't work at all right, no matter the news:%23uuN8lgxGHA.4256@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." application calling into it? Anyway, hope to see this driver mess disapearing (well say in the next decade ;-)) in Vista. Willy. >I've run into a number of USB adapters that simply didn't work correctly I will not longer purchase a USB Serial adapter unless I can determine > "off-the-shelf." 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 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. |
|||||||||||||||||||||||