天呀,都4天了,妈的出鬼了!!这问题也忒奇怪了吧!强烈要求高手帮忙!!

rr 2004-04-25 04:36:32
小弟在做一个发送短信的控件。用到mscomm控件
先从vb6.0做起,然后升级到vb.net
在dll库中添加一form和一mscomm控件(vb6.0升级上来的)
代码如下:
Option Strict Off
Option Explicit On
Friend Class frmSMS
Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
Public Sub New()
MyBase.New()
If m_vb6FormDefInstance Is Nothing Then
If m_InitializingDefInstance Then
m_vb6FormDefInstance = Me
Else
Try
'对于启动窗体,所创建的第一个实例为默认实例。
If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
End If
End If
'此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
End Sub
'窗体重写处置,以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents tmrListen As System.Windows.Forms.Timer
Public WithEvents tmrQueue As System.Windows.Forms.Timer
Public WithEvents CommSMS As AxMSCommLib.AxMSComm
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器来修改它。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmSMS))
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.tmrListen = New System.Windows.Forms.Timer(Me.components)
Me.tmrQueue = New System.Windows.Forms.Timer(Me.components)
Me.CommSMS = New AxMSCommLib.AxMSComm
CType(Me.CommSMS, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'tmrListen
'
Me.tmrListen.Interval = 1000
'
'tmrQueue
'
Me.tmrQueue.Interval = 1000
'
'CommSMS
'
Me.CommSMS.Enabled = True
Me.CommSMS.Location = New System.Drawing.Point(8, 8)
Me.CommSMS.Name = "CommSMS"
Me.CommSMS.OcxState = CType(resources.GetObject("CommSMS.OcxState"), System.Windows.Forms.AxHost.State)
Me.CommSMS.Size = New System.Drawing.Size(38, 38)
Me.CommSMS.TabIndex = 0
'
'frmSMS
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.Window
Me.ClientSize = New System.Drawing.Size(72, 56)
Me.Controls.Add(Me.CommSMS)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Location = New System.Drawing.Point(175, 206)
Me.Name = "frmSMS"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "frmSMS"
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
CType(Me.CommSMS, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
#End Region
#Region "升级支持"
Private Shared m_vb6FormDefInstance As frmSMS
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As frmSMS
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New frmSMS()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set(ByVal Value As frmSMS)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
End Class
''''''''''''''''''''''''''''''''''''''
同时在另一个class:SmSAdapter中
先声明 Private WithEvents CommSMS As AxMSCommLib.AxMSComm
在初始化函数
Private Sub Class_Initialize_Renamed()
Dim frmSmS = New frmSMS
CommSMS = frmSmS.DefInstance.CommSMS
CommSMS.InputLen = 0
CommSMS.Settings = "19200,n,8,1"
CommSMS.InBufferSize = 1024
CommSMS.InputMode = MSCommLib.InputModeConstants.comInputModeText
CommSMS.Handshaking = MSCommLib.HandshakeConstants.comNone
''''''''''''''''''''''''省略
end sub
再调用的方法中:
Private Function ExecCommCMD(ByVal Command_Renamed As String, Optional ByRef Buffer As String = "", Optional ByVal Delay_MilliSecond As Single = 10.0#) As Integer
Dim EndTime As Single
Dim lngEndPos, lngStartPos, lngCmdLen As Integer
Dim retVal As Integer
'Dim strBuffer As String
'Dim recBuffer As String
Dim recCommBuffer() As String
Dim recProcCommBuffer() As String
Dim backCommand As String
Dim pause As Object
Dim start As Object
Dim i, j As Short '循环使用
j = 0
i = 0
EndTime = VB.Timer() + Delay_MilliSecond

If CommSMS.PortOpen Then
Command_Renamed = Left(Command_Renamed, InStr(Command_Renamed & vbCrLf, vbCrLf) - 1) & vbCrLf
lngCmdLen = Len(Command_Renamed)
CommSMS.Output = "AT^SCID" & vbCrLf '这句能执行,
Do
Sleep(1000)
m_strCommBuffer = m_strCommBuffer & CommSMS.Input '这句接收不到字符
MsgBox("Command_Renamed1:" + Command_Renamed & vbCrLf + m_strCommBuffer)’弹出Command_Renamed1:

'''处理代码
'''处理代码

Loop While VB.Timer() < EndTime
Else
retVal = -1
End If

ExecCommCMD = retVal
End Function
另小弟做了大量的实验,确定.net下调用mscomm控件没有问题。
在封装成dll的时候,如果直接在form1,也就是包含mscomm的窗体下直接写代码如 public sub cc()
dim rr as string
me.mscomm.output="at"&vbcrlf
sleep 1000
rr = me.mscomm.input'这句可以接收到数据
msgbox(rr)'可以看到有数据。
end sub
另外还测试了这么几种情况
在vb6.0下将将mscomm,form封装成dll时,如果在另一个class中写方法的话,也不能接收但是可以发送。
如果直接写到form下的话,在vb和.net下都可以捕做到数据????
请问这是什么原因??why??就这个问题让我搞了四天还没搞出来?为什么?
高手呀,求你了,这个周末我也在这耗着,都不知道还要耗多久,我还在试用期呀。。。。。。。。。。。。。
救命!!!
...全文
28 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wunifeng 2004-04-27
呵呵,不管怎么说,恭喜兄弟把这个问题解决了、
回复
shyyu 2004-04-27
来迟了,问题已经解决了呀!!!!呵呵!!!!!
回复
rr 2004-04-27
谢谢wunifeng(蛐蛐)的大力帮忙!!
回复
rr 2004-04-27
结帖!
回复
zhengyi2002grade 2004-04-26
up
回复
yuwen16 2004-04-26
问题出来了:
If CommSMS.PortOpen = False Then CommSMS.PortOpen = True
If CommSMS.PortOpen Then
lngCmdLen = Len(Command_Renamed & vbCrLf)
Do
Do
CommSMS.Output = Command_Renamed & vbCrLf
Sleep((1000))
m_strCommBuffer = CommSMS.Input
'MsgBox("Command_Renamed1:" + Command_Renamed & vbCr
If Len(m_strCommBuffer) > 0 Then Exit Do
Loop Until i > 6
如果把 CommSMS.Output = Command_Renamed & vbCrLf
放到循环外面那么就收不到信息,也就是说第一次发出comm.output的时候,
此时去接收comm.input是接收不到的。
但是把CommSMS.Output = Command_Renamed & vbCrLf放到循环内部去做的话。也就是说如果第一次发出指令返回为空的话,那么再发一次指令,这个时候可以接收到。最多在三次肯定可以接收到信息,这是为什么?
回复
wangjian 2004-04-25
去WWW.SHSOFTS.COM下把。在未分类中
回复
terryxin 2004-04-25
帮你看看
回复
tangxiaosan001 2004-04-25
^_^,太长了.
回复
yuwen16 2004-04-25
wangjian(山风)
那把它发给我吧。谢谢哦。tidy@vip.citiz.net
回复
wangjian 2004-04-25
如果实在不行用我的232库把,是纯.NET的
没有细看代码,也不晓得咋的:)
回复
rr 2004-04-25
另外小弟的调试环境为win2000server
回复
rr 2004-04-25
上面写的包含mscomm的form代码是vb====》vb.net自动生成的。
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2004-04-25 04:36
社区公告
暂无公告