在服务中使用SerialPort [英] using SerialPort in a service

查看:62
本文介绍了在服务中使用SerialPort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


有人可以告诉我为什么我不在服务中获得串口事件,我

创建了一个单独的线程来打开端口但是没有事件即将发生(

当我使用计时器组件时会发生同样的事情,使用System.Timers.Timer

它工作正常)

>
tnx提前


Jean Paul

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

推荐答案




我不知道。它应该起作用,但我没有在VS

2005下编写服务来测试它。我会尽快做到这一点,就像一个实验一样。


但是,使用System.Timers.Timer进行轮询是完全可以接受的。你将不会(IMO)获得性能降低,并且额外支出很少额外支出。


迪克


-

Richard Grier,MVP

Hard&软件

Visual Basic程序员串行通信指南的作者,第四版

版,

ISBN 1-890422-28-2( 391页,包括CD-ROM)。 2004年7月,修订3月

2006.

www.hardandsoftware.net 了解详情和联系方式。
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.


嗨迪克,


计时器问题解决了使用System.Timers.Timer,这不是一个问题,但是SerialPort似乎是一个更大的问题。


我已经创建了一个测试 - 只有一个SerialPort作为内容的服务。

从我的服务组件中的工具箱中选择串口,

baudrate等设置为属性's in设计师。

SerialPort构建在

服务构造函数的默认组件初始化中,我还将公共计数器设置为0 ..

在OnStart中我执行了一个SerialPort.Open()。

在OnStop中我执行SerialPort.Close()并将我的公共

计数器的内容写入eventlog,

在eventha中ndler I incr。公共柜台。


现在我发送一个字符串到串口,计数器不会增加

(我试过去做一个例子,但注意到发生了)


接下来我已经包含了一个1000毫秒的计时器来测试来自

SerialPort的IsOpen,并将我的公共计数器的结果写入事件日志。

端口是ans保持打开但计数器保持为0


当我在WindowsForm应用程序中使用相同的代码时,一切正常。


当我尝试在控制台应用程序中使用SerialPort时,它不起作用

更新。


奇怪但真实,我在整个互联网上搜索了关于串口或Windows服务的somone或者某些东西,但是所有曾经发过一些东西的人都使用了P / Invoke和Win32内核。


到目前为止这里的故事。


Greets


Jean Paul


" Dick Grier" < dick_grierNOSPAM @ .msn.comschreef in bericht

news:ui ************** @ 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...




我不知道。它应该起作用,但我没有在VS

2005下编写服务来测试它。我会尽快做到这一点,就像一个实验一样。


然而,使用System.Timers.Timer进行轮询完全可以接受。

你将会不是(IMO)引起性能降低,而且额外支出很少额外




迪克


-

Richard Grier,MVP

Hard&软件

Visual Basic程序员串行通信指南的作者,第四版

版,

ISBN 1-890422-28-2( 391页,包括CD-ROM)。 2004年7月,修订3月

2006.

www.hardandsoftware.net 了解详情和联系方式。
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.



请发布控制台应用程序代码,我们不能没有它可以帮助你。


Willy。


" news.microsoft.com" < je ******* @ opelwilly.com写了留言

新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ......

|嘿迪克,

|

|使用System.Timers.Timer解决了计时器问题,这样就不会是b / b $

|问题但是SerialPort似乎是一个更大的问题。

|

|我创建了一个只有Seri​​alPort作为内容的测试服务。

|串行端口是从我的服务组件中的工具箱中选取的,

|波特率等设置在设计师的属性'。

| SerialPort是在

|的默认组件初始化中构建的服务构造函数,我也将公共计数器设置为0 ..

|在OnStart中我做了一个SerialPort.Open()。

|在OnStop中我执行SerialPort.Close()并写下我的内容

public

|与事件日志相反,

|在事件处理程序中我加入。公共柜台。

|

|现在我发送一个字符串到串口,计数器不是

递增

| (我试过去做一个例子,但注意到了)

|

|接下来我已经包含了一个1000毫秒的计时器来测试来自

|的IsOpen SerialPort并将我公共计数器的结果写入事件日志。



|港口是ans保持开放,但柜台保持为0

|

|当我在WindowsForm应用程序中使用相同的代码时,一切正常。

|

|当我尝试在控制台应用程序中使用SerialPort时,它没有

工作

|更好。

|

|奇怪但真实,我在整个互联网上搜索了somone或somthing

about

| SerialPort和WindowsService但是所有发布过的东西都是

|在方向上使用了P / Invoke和Win32内核。

|

|到目前为止这里的故事。

|

|迎接

|

|让·保罗

|

| Dick Grier < dick_grierNOSPAM @ .msn.comschreef in bericht

|新闻:ui ************** @ TK2MSFTNGP03.phx.gbl ...

|

| >

|我不知道。它应该起作用,但我没有写下

VS

|的服务2005年进行测试。我会尽快做到这一点,就像一个实验一样。

| >

|但是,使用System.Timers.Timer进行轮询是完全可以接受的。

|你不会(IMO)产生性能降低,而且很少

额外

|开销。

| >

|迪克

| >

| -

| Richard Grier,MVP

|硬&软件

| Visual Basic程序员串行通信指南的作者,

Fourth

|版,

| ISBN 1-890422-28-2(391页,包括CD-ROM)。 2004年7月,修订

三月

| 2006.

|有关详细信息和联系信息,请参见 www.hardandsoftware.net

| >

|

|
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.
| >
|
|


这篇关于在服务中使用SerialPort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆