如何从子菜单中使用Serialport [英] How to use Serialport out of the sub
本文介绍了如何从子菜单中使用Serialport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部,
我正在VS 2010中开发一个VB应用程序.我正在使用Serialport属性通过微型USB电缆将查询发送到GSM调制解调器.在通过此方法使用此serialport属性时,出现运行时错误...任何人都请告诉我如何在同一类中的此事件之外使用此属性.
Hai All,
I am developing one VB Application in VS 2010. I am using Serialport property for sending Quiries to GSM Modem through Micro USB Cable. While using of this serialport property out of this method, i got run time errors ... any one please tell me how to use this property out of this event in the same class.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
Dim mySerialPort As New SerialPort(ComboBox1.Text)
mySerialPort.BaudRate = 19200
mySerialPort.Parity = Parity.None
mySerialPort.StopBits = StopBits.One
mySerialPort.DataBits = 8
mySerialPort.Handshake = Handshake.None
AddHandler mySerialPort.DataReceived, AddressOf DataReceivedHandler
mySerialPort.Open()
If mySerialPort.IsOpen = True Then
''For i As Integer = 1 To 3
'' mySerialPort.Write("AT+CMGR=" & i & vbCrLf)
'' MsgBox("AT+CMGR=" & i & vbCrLf)
''Next
'' ComboBox1.Enabled = False
mySerialPort.Write("AT+CMGL=" & Chr(34) & "REC UNREAD" & Chr(34) & vbCrLf)
End If
ToolStripStatusLabel1.Text = ToolStripStatusLabel1.Text & " " & ComboBox1.Text & "," & "19200" & "," & "N" & "," & "1" & "," & "8" & "," & "N"
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
End Sub
推荐答案
您的应用程序中存在严重的设计缺陷.您应该有一个单独的类来管理串行端口的连接.您将以表单的形式维护该类的全局类实例,然后只需从处理程序中调用该类中的方法即可告诉其如何操作串行端口.
You''ve got a serious design flaw in your app. You should have a seperate class that manages the connection of the serial port. You''d maintain a class-global instance of this class in your form, then just call methods in that class from your handlers that tell it how to manipulate the serial port.
这篇关于如何从子菜单中使用Serialport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文