有没有办法“继承"?第二种形式的一个串行端口类实例? [英] Is there a way to "Inherit" a serialport class instance on a second form?
问题描述
大家好,我在处理IO.Ports.Serialport时遇到了问题
我有这堂课,
Hi guys, quick question, in dealing with IO.Ports.Serialport
I have this class,
''' <summary>
''' Routines for finding and accessing COM ports.
''' </summary>
Public Class ComPorts
'....this class contains different properties and subs that handle the serial port communication. for example:
Friend Shared Sub FindComPorts()
'.....omitted
End Sub
Friend Function OpenComPort() As Boolean
'...omitted
End Function
"等等.
(这来自lvr.com的串行通信示例)
我在第一种形式上使用此类,例如
''and such..
(this came from a serial communication example from lvr.com)
I''m using this class on my first form , like such
Public UserPort1 As ComPorts
'...
UserPort1.OpenComPort()
我的问题在这里出现:现在,在第一个窗体上打开了端口(UserPort1声明为:Public UserPort1作为第一个窗体上的ComPorts),当我关闭/隐藏第一个窗体并给出焦点/时,如何保持端口打开?显示第二种形式?
---------------------------
更新:
感谢您的答复.我添加了一个公共模块
My question arises here: now that the port is open on the first form(UserPort1 is declared:Public UserPort1 As ComPorts on the first form), how do I keep the port open,when I close/hide the first form and give focus/show the second form??
---------------------------
UPDATE:
Thank you for the reply. I added a public module
Module PublicSerialPortModuel
Public UserPort1 As ComPorts
End Module
现在我可以在Form2中使用UserPort1,而无需使用"form1.xxxx"前缀.
但是我遇到了另一个问题,这让我真的很不高兴(好..困惑)
我在form1上使用了偶数处理程序来处理DataReceived事件
and now I can use UserPort1 in form2 without the "form1.xxxx" prefix.
I run into another problem though, which got me really nonplussed(well..confused)
I used a even handler on form1 to handle DataReceived event
AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler
那个子就像
and that sub is something like
Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
COMportReadBuffer1 = UserPort1.SelectedPort.ReadExisting
While UserPort1.SelectedPort.BytesToRead > 0
COMportReadBuffer1 &= UserPort1.SelectedPort.ReadExisting
End While
End Sub
在第一种形式上一切正常.但是当我在form2上做同样的事情时,
which works all fine on the first form. But when I do the same thing on form2,
AddHandler UserPort1.SelectedPort.DataReceived, AddressOf DataReceivedHandler2
当触发DataReceived事件并进入DataReceivedHandler2()时,读取缓冲区为空白,并且其长度为0
您能想到任何可能的原因吗?我现在无法在公共模块中将UserPort1公开定义为
when DataReceived event is triggered and I get into DataReceivedHandler2(), the read buffer is blank and its length is 0
Can you think of any possible reasons for this? I can''t ..with UserPort1 now being defined public in the public module
推荐答案
我不认为您的问题很清楚.如果您只想联系并使用该类,则必须对委托进行此操作:
http://msdn.microsoft.com/en-us/magazine/cc301810.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/ms173171%28v = vs.100%29.aspx [ ^ ]
或本网站上的内容:
http://www.codeproject.com/search.aspx?q = delegate& x = 0& y = 0& sbo = kw& usfc = false [
I dont think your question is comepletely clear. If you just want to have contact and use the class you would have to do that with delegates:
http://msdn.microsoft.com/en-us/magazine/cc301810.aspx[^]
http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.100%29.aspx[^]
Or something on this site:
http://www.codeproject.com/search.aspx?q=delegate&x=0&y=0&sbo=kw&usfc=false[^]
这篇关于有没有办法“继承"?第二种形式的一个串行端口类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!