关于串口通信 本人初次接触串口通信的东西 解释下每一行.我没学过
本帖最后由 bcrun 于 2013-04-28 15:15:43 编辑
Option Explicit串口通信 String 函数
'==================================================================================
'公共事件
'==================================================================================
Public Event OnAddPoint(vChNum As Integer, vChData() As Long, vTrainCode As String)
Public Event OnCFGValue(vParam As String)
Public Event OnErrFrm()
Public Event OnOverFlow()
'==================================================================================
'属性本地变量
'==================================================================================
Private DevOpen As Boolean
Private WithEvents m_Process As CProcess
'==================================================================================
'本地变量
'==================================================================================
Private BinDataBuf As Variant '读取数据缓存
Private Sub com_Ctrl_Click()
Dim portNum As Integer
portNum = Mid(Combo_ComNum.Text, 4)
On Error GoTo ErrorHandler
If MSComm1.PortOpen = False Then
Call commOpen
Else
Call commClose
End If
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 8005
MsgBox ("串口" & portNum & "已打开!")
Case 8002
MsgBox ("无效串口号!")
Case Else
MsgBox (Err.Description)
End Select
Exit Sub
End Sub
Public Function Send(SendStr As String) As Boolean
If MSComm1.PortOpen = True Then
MSComm1.Output = SendStr
Send = True
Else
MsgBox ("请打开接收端口")
Send = False
End If
End Function
Public Function RcvData() As Boolean
If MSComm1.PortOpen = True Then
If MSComm1.InputMode = comInputModeBinary Then
If MSComm1.InBufferCount <> 0 Then
BinDataBuf = BinDataBuf & MSComm1.Input
m_Process.ReadDataPack BinDataBuf
End If
End If
End If
End Function
Public Function IsDevOpen() As Boolean
IsDevOpen = DevOpen
End Function
Public Sub ClrDataBuf()
BinDataBuf = ""
End Sub
Private Sub commOpen()
On Error GoTo ErrorHandler
Dim portNum As Integer
DevOpen = False
portNum = Mid(Combo_ComNum.Text, 4)
If MSComm1.PortOpen = False Then
MSComm1.CommPort = portNum
MSComm1.Settings = Combo_baud.Text & ",n,8,1"
MSComm1.PortOpen = True
End If
shape_comState.FillColor = &HFF00&
com_Ctrl.Caption = "关闭串口"
Combo_ComNum.Enabled = False
DevOpen = True
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 8005
MsgBox ("串口" & portNum & "已打开!")
Case 8002
MsgBox ("无效串口号!")
Case Else
MsgBox (Err.Description)
End Select
Exit Sub
End Sub
Private Sub commClose()
DevOpen = False
On Error GoTo ErrorHandler
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
shape_comState.FillColor = &H8000000F
com_Ctrl.Caption = "打开串口"
Combo_ComNum.Enabled = True
DevOpen = True
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 8002
MsgBox ("无效串口号!")
Case Else
MsgBox (Err.Description)
End Select
Exit Sub
End Sub
Private Sub m_Process_OnAddPoint(vChNum As Integer, vChData() As Long, vTrainCode As String)
RaiseEvent OnAddPoint(vChNum, vChData, vTrainCode)
End Sub
Private Sub m_Process_OnCFGValue(vParam As String)
RaiseEvent OnCFGValue(vParam)
End Sub
Private Sub m_Process_OnErrFrm()
RaiseEvent OnErrFrm
End Sub
Private Sub m_Process_OnOverFlow()
RaiseEvent OnOverFlow
End Sub
Private Sub UserControl_Initialize()
On Error Resume Next
'baud rate setting
Combo_baud.addItem (1200)
Combo_baud.addItem (2400)
Combo_baud.addItem (4800)
Combo_baud.addItem (9600)
Combo_baud.addItem (19200)
Combo_baud.addItem (38400)
Combo_baud.addItem (57600)
Combo_baud.addItem (115200)
Combo_baud.addItem (230400)
Combo_baud.addItem (460800)
Combo_baud.addItem (921600)
Combo_baud.Text = 115200
Combo_ComNum.addItem ("COM1")
Combo_ComNum.addItem ("COM2")
Combo_ComNum.addItem ("COM3")
Combo_ComNum.addItem ("COM4")
Combo_ComNum.addItem ("COM5")
Combo_ComNum.addItem ("COM6")
Combo_ComNum.addItem ("COM7")
Combo_ComNum.Text = "COM1"
DevOpen = False
If MSComm1.PortOpen = True Then
shape_comState.FillColor = &HFF00&
com_Ctrl.Caption = "关闭串口"
DevOpen = True
End If
Set m_Process = New CProcess
End Sub
Private Sub UserControl_Terminate()
Set m_Process = Nothing
End Sub
因此,无论是打开或是关闭串口,均事先将此标志设置为 False。由于函数中均写了 On Error GoTo ErrorHandler,所以当出现错误时,此标志将保持为 False。而无论是打开或是关闭串口函数,正常出口均将标志设置为 True 的。
从上下文看,楼主贴出的疑似标准模块代码。此标志为 Private。因此,外部模块要获取此状态,就要通过作者的函数。
如果楼主真的是没有经验,就不必将精力消耗在这些无关的细节。重点看 MSComm 控件的设置和操作。
[解决办法]
那是一个自定义函数名,与 VB 没什么关系。