找到原因了,代码有点小问题,楼主,将打开代码修改如下'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '过程功能:打开指定串口 '功能描述: '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Private Sub btnOpenPort_Click() Dim lngP As Long Dim bolP As Boolean Dim dc As DCB On Error GoTo errSub If lngComHandle <= 0 Then strCommPort = "\\.\" & cmbCommPort.Text strSettings = cmbBaud.Text & "," & Mid(cmbCheck.Text, 1, 1) & "," & cmbDataBit.Text & "," & _ cmbStop.Text lngComHandle = OpenComm(strCommPort) If lngComHandle <> 0 Then bolP = SetCommParam(lngComHandle, strSettings) '设置串口通讯参数 If bolP Then shpPortStatus.FillColor = vbGreen If Not bolP Then shpPortStatus.FillColor = vbRed bolP = SetCommTimeOut(lngComHandle, 2, 3) '设置通信超时参数 btnOpenPort.Caption = "关" End If lngReciveCount = 0 lngCommCount = 0 lngCommTotalTime = 0 Else If CloseComm(lngComHandle) Then lngComHandle = 0 lngReciveCount = 0 btnOpenPort.Caption = "开" shpPortStatus.FillColor = vbRed End If End If Exit Sub errSub: Debug.Print Err.Description End Sub
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '过程功能:打开指定串口 '功能描述: '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Private Sub btnOpenPort_Click() Dim lngP As Long Dim bolP As Boolean Dim dc As DCB On Error GoTo errSub If lngComHandle <= 0 Then strCommPort = "\\.\" & cmbCommPort.Text strSettings = cmbBaud.Text & "," & Mid(cmbCheck.Text, 1, 1) & "," & cmbDataBit.Text & "," & _ cmbStop.Text lngComHandle = OpenComm(strCommPort) If lngComHandle <> 0 Then bolP = SetCommParam(lngComHandle, strSettings) '设置串口通讯参数 If bolP Then shpPortStatus.FillColor = vbGreen If Not bolP Then shpPortStatus.FillColor = vbRed bolP = SetCommTimeOut(lngComHandle, 2, 3) '设置通信超时参数 btnOpenPort.Caption = "关" End If lngReciveCount = 0 lngCommCount = 0 lngCommTotalTime = 0 Else If CloseComm(lngComHandle) Then lngComHandle = 0 lngReciveCount = 0 btnOpenPort.Caption = "开" shpPortStatus.FillColor = vbRed End If End If Exit Sub errSub: Debug.Print Err.Description End Sub
修改之后有很多地方提示错误啊 Private Sub btnOpenPort_Click() Dim lngP As Long Dim bolP As Boolean Dim dc As DCB On Error GoTo errSub If lngComHandle <= 0 Then strCommPort = "\\.\" & cmbCommPort.Text strSettings = cmbBaud.Text & "," & Mid(cmbCheck.Text, 1, 1) & "," & cmbDataBit.Text & "," & _ cmbStop.Text ''' lngComHandle = OpenComm(strCommPort) If lngComHandle <> 0 Then ''' bolP = SetCommParam(lngComHandle, strSettings) '设置串口通讯参数 ''' If bolP Then shpPortStatus.FillColor = vbGreen ''' If Not bolP Then shpPortStatus.FillColor = vbRed ''' bolP = SetCommTimeOut(lngComHandle, 2, 3) '设置通信超时参数 btnOpenPort.Caption = "关" End If ''' lngReciveCount = 0 ''' lngCommCount = 0 ''' lngCommTotalTime = 0 Else ''' If CloseComm(lngComHandle) Then lngComHandle = 0 ''' lngReciveCount = 0 btnOpenPort.Caption = "开" ''' shpPortStatus.FillColor = vbRed End If End If Exit Sub errSub: Debug.Print Err.Description End Sub
Private Sub btnComm_Click() Dim lngP As Long Dim lngT As Long Dim bytP() As String Dim strA() As String Dim strP As String ' Dim lngT As Long Dim lngL As Long On Error GoTo errSub If optTextMode(0).Value Then '文本发送 lngP = WriteComm(lngComHandle, StringToBytes(txtSend.Text)) '写串口 lngCommStartTime = GetTickCount lngCommCount = lngCommCount + 1 End If If optHexMode(0).Value Then '二进制发送 strA = Split(txtSend.Text, " ") strP = "" For lngP = LBound(strA) To UBound(strA) strP = strP & strA(lngP) Next lngP lngP = WriteComm(lngComHandle, ReadByte(strP)) lngCommStartTime = GetTickCount lngCommCount = lngCommCount + 1 End If
Sleep 40'延迟40ms,以便数据完全返回。 If optHexMode(1).Value Then '二进制接收 If lngP <> 0 Then If txtRecive.Text <> "" Then If chkHuanhang.Value = vbChecked Then txtRecive.Text = txtRecive.Text & vbCrLf & ReadString(ReadComm(lngComHandle)) '读串口 Else txtRecive.Text = txtRecive.Text & " " & ReadString(ReadComm(lngComHandle)) '读串口 End If Else txtRecive.Text = ReadString(ReadComm(lngComHandle)) '读串口 End If End If End If If optTextMode(1).Value Then '文本接收 If lngP <> 0 Then If txtRecive.Text <> "" Then If chkHuanhang.Value = vbChecked Then txtRecive.Text = txtRecive.Text & vbCrLf & BytesToString(ReadComm(lngComHandle)) '读串口 Else txtRecive.Text = txtRecive.Text & " " & BytesToString(ReadComm(lngComHandle)) '读串口 End If Else txtRecive.Text = BytesToString(ReadComm(lngComHandle)) '读串口 End If End If End If lngT = GetTickCount - lngCommStartTime lngCommTotalTime = lngCommTotalTime + lngT txtRecive.Text = txtRecive.Text & " (" & CStr(lngT) & "ms)" lblCommAVG.Caption = "通信次数:" & CStr(lngCommCount) & "次 平均通信时间:" & _ Format(CStr(lngCommTotalTime / lngCommCount), "##00.00") & "ms" lblReciveCount.Caption = "接收字节数: " & CStr(lngReciveCount) Exit Sub errSub: Debug.Print Err.Description End Sub
Private Sub btnComm_Click() Dim lngP As Long Dim lngT As Long Dim bytP() As String Dim strA() As String Dim strP As String ' Dim lngT As Long Dim lngL As Long On Error GoTo errSub If optTextMode(0).Value Then '文本发送 ''' lngP = WriteComm(lngComHandle, StringToBytes(txtSend.Text)) '写串口 ''' lngCommStartTime = GetTickCount ''' lngCommCount = lngCommCount + 1 End If If optHexMode(0).Value Then '二进制发送 strA = Split(txtSend.Text, " ") strP = "" For lngP = LBound(strA) To UBound(strA) strP = strP & strA(lngP) Next lngP ''' lngP = WriteComm(lngComHandle, ReadByte(strP)) ''' lngCommStartTime = GetTickCount ''' lngCommCount = lngCommCount + 1 End If
Sleep 40 '延迟40ms,以便数据完全返回。 If optHexMode(1).Value Then '二进制接收 If lngP <> 0 Then If txtRecive.Text <> "" Then ''' If chkHuanhang.Value = vbChecked Then ''' txtRecive.Text = txtRecive.Text & vbCrLf & ReadString(ReadComm(lngComHandle)) '读串口 Else ''' txtRecive.Text = txtRecive.Text & " " & ReadString(ReadComm(lngComHandle)) '读串口 End If Else ''' txtRecive.Text = ReadString(ReadComm(lngComHandle)) '读串口 End If End If ''' End If If optTextMode(1).Value Then '文本接收 If lngP <> 0 Then If txtRecive.Text <> "" Then ''' If chkHuanhang.Value = vbChecked Then ''' txtRecive.Text = txtRecive.Text & vbCrLf & BytesToString(ReadComm(lngComHandle)) '读串口 Else ''' txtRecive.Text = txtRecive.Text & " " & BytesToString(ReadComm(lngComHandle)) '读串口 End If Else ''' txtRecive.Text = BytesToString(ReadComm(lngComHandle)) '读串口 End If End If ''' End If ''' lngT = GetTickCount - lngCommStartTime ''' lngCommTotalTime = lngCommTotalTime + lngT txtRecive.Text = txtRecive.Text & " (" & CStr(lngT) & "ms)" ''' lblCommAVG.Caption = "通信次数:" & CStr(lngCommCount) & "次 平均通信时间:" & _ Format(CStr(lngCommTotalTime / lngCommCount), "##00.00") & "ms" ''' lblReciveCount.Caption = "接收字节数: " & CStr(lngReciveCount) Exit Sub errSub: Debug.Print Err.Description End Sub
首先你的有COM10这个硬件,不然是不可能打开的。
我最多用到COM6
我试一试先
'过程功能:打开指定串口
'功能描述:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Sub btnOpenPort_Click()
Dim lngP As Long
Dim bolP As Boolean
Dim dc As DCB
On Error GoTo errSub
If lngComHandle <= 0 Then
strCommPort = "\\.\" & cmbCommPort.Text strSettings = cmbBaud.Text & "," & Mid(cmbCheck.Text, 1, 1) & "," & cmbDataBit.Text & "," & _
cmbStop.Text
lngComHandle = OpenComm(strCommPort)
If lngComHandle <> 0 Then
bolP = SetCommParam(lngComHandle, strSettings) '设置串口通讯参数
If bolP Then shpPortStatus.FillColor = vbGreen
If Not bolP Then shpPortStatus.FillColor = vbRed
bolP = SetCommTimeOut(lngComHandle, 2, 3) '设置通信超时参数
btnOpenPort.Caption = "关"
End If
lngReciveCount = 0
lngCommCount = 0
lngCommTotalTime = 0
Else
If CloseComm(lngComHandle) Then
lngComHandle = 0
lngReciveCount = 0
btnOpenPort.Caption = "开"
shpPortStatus.FillColor = vbRed
End If
End If
Exit Sub
errSub:
Debug.Print Err.Description
End Sub
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'过程功能:打开指定串口
'功能描述:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Sub btnOpenPort_Click()
Dim lngP As Long
Dim bolP As Boolean
Dim dc As DCB
On Error GoTo errSub
If lngComHandle <= 0 Then
strCommPort = "\\.\" & cmbCommPort.Text
strSettings = cmbBaud.Text & "," & Mid(cmbCheck.Text, 1, 1) & "," & cmbDataBit.Text & "," & _
cmbStop.Text
lngComHandle = OpenComm(strCommPort)
If lngComHandle <> 0 Then
bolP = SetCommParam(lngComHandle, strSettings) '设置串口通讯参数
If bolP Then shpPortStatus.FillColor = vbGreen
If Not bolP Then shpPortStatus.FillColor = vbRed
bolP = SetCommTimeOut(lngComHandle, 2, 3) '设置通信超时参数
btnOpenPort.Caption = "关"
End If
lngReciveCount = 0
lngCommCount = 0
lngCommTotalTime = 0
Else
If CloseComm(lngComHandle) Then
lngComHandle = 0
lngReciveCount = 0
btnOpenPort.Caption = "开"
shpPortStatus.FillColor = vbRed
End If
End If
Exit Sub
errSub:
Debug.Print Err.Description
End Sub
Private Sub btnOpenPort_Click()
Dim lngP As Long
Dim bolP As Boolean
Dim dc As DCB
On Error GoTo errSub
If lngComHandle <= 0 Then
strCommPort = "\\.\" & cmbCommPort.Text
strSettings = cmbBaud.Text & "," & Mid(cmbCheck.Text, 1, 1) & "," & cmbDataBit.Text & "," & _
cmbStop.Text
''' lngComHandle = OpenComm(strCommPort)
If lngComHandle <> 0 Then
''' bolP = SetCommParam(lngComHandle, strSettings) '设置串口通讯参数
''' If bolP Then shpPortStatus.FillColor = vbGreen
''' If Not bolP Then shpPortStatus.FillColor = vbRed
''' bolP = SetCommTimeOut(lngComHandle, 2, 3) '设置通信超时参数
btnOpenPort.Caption = "关"
End If
''' lngReciveCount = 0
''' lngCommCount = 0
''' lngCommTotalTime = 0
Else
''' If CloseComm(lngComHandle) Then
lngComHandle = 0
''' lngReciveCount = 0
btnOpenPort.Caption = "开"
''' shpPortStatus.FillColor = vbRed
End If
End If
Exit Sub
errSub:
Debug.Print Err.Description
End Sub
你就把:strCommPort = cmbCommPort.Text
修改为:strCommPort = "\\.\" & cmbCommPort.Text
即可
Private Sub btnComm_Click()
Dim lngP As Long
Dim lngT As Long
Dim bytP() As String
Dim strA() As String
Dim strP As String
' Dim lngT As Long
Dim lngL As Long
On Error GoTo errSub
If optTextMode(0).Value Then
'文本发送
lngP = WriteComm(lngComHandle, StringToBytes(txtSend.Text)) '写串口
lngCommStartTime = GetTickCount
lngCommCount = lngCommCount + 1
End If
If optHexMode(0).Value Then
'二进制发送
strA = Split(txtSend.Text, " ")
strP = ""
For lngP = LBound(strA) To UBound(strA)
strP = strP & strA(lngP)
Next lngP
lngP = WriteComm(lngComHandle, ReadByte(strP))
lngCommStartTime = GetTickCount
lngCommCount = lngCommCount + 1
End If
Sleep 40'延迟40ms,以便数据完全返回。 If optHexMode(1).Value Then
'二进制接收
If lngP <> 0 Then
If txtRecive.Text <> "" Then
If chkHuanhang.Value = vbChecked Then
txtRecive.Text = txtRecive.Text & vbCrLf & ReadString(ReadComm(lngComHandle)) '读串口
Else
txtRecive.Text = txtRecive.Text & " " & ReadString(ReadComm(lngComHandle)) '读串口
End If
Else
txtRecive.Text = ReadString(ReadComm(lngComHandle)) '读串口
End If
End If
End If
If optTextMode(1).Value Then
'文本接收
If lngP <> 0 Then
If txtRecive.Text <> "" Then
If chkHuanhang.Value = vbChecked Then
txtRecive.Text = txtRecive.Text & vbCrLf & BytesToString(ReadComm(lngComHandle)) '读串口
Else
txtRecive.Text = txtRecive.Text & " " & BytesToString(ReadComm(lngComHandle)) '读串口
End If
Else
txtRecive.Text = BytesToString(ReadComm(lngComHandle)) '读串口
End If
End If
End If
lngT = GetTickCount - lngCommStartTime
lngCommTotalTime = lngCommTotalTime + lngT
txtRecive.Text = txtRecive.Text & " (" & CStr(lngT) & "ms)"
lblCommAVG.Caption = "通信次数:" & CStr(lngCommCount) & "次 平均通信时间:" & _
Format(CStr(lngCommTotalTime / lngCommCount), "##00.00") & "ms"
lblReciveCount.Caption = "接收字节数: " & CStr(lngReciveCount)
Exit Sub
errSub:
Debug.Print Err.Description
End Sub
Dim lngP As Long
Dim lngT As Long
Dim bytP() As String
Dim strA() As String
Dim strP As String
' Dim lngT As Long
Dim lngL As Long
On Error GoTo errSub
If optTextMode(0).Value Then
'文本发送
''' lngP = WriteComm(lngComHandle, StringToBytes(txtSend.Text)) '写串口
''' lngCommStartTime = GetTickCount
''' lngCommCount = lngCommCount + 1
End If
If optHexMode(0).Value Then
'二进制发送
strA = Split(txtSend.Text, " ")
strP = ""
For lngP = LBound(strA) To UBound(strA)
strP = strP & strA(lngP)
Next lngP
''' lngP = WriteComm(lngComHandle, ReadByte(strP))
''' lngCommStartTime = GetTickCount
''' lngCommCount = lngCommCount + 1
End If
Sleep 40 '延迟40ms,以便数据完全返回。 If optHexMode(1).Value Then
'二进制接收
If lngP <> 0 Then
If txtRecive.Text <> "" Then
''' If chkHuanhang.Value = vbChecked Then
''' txtRecive.Text = txtRecive.Text & vbCrLf & ReadString(ReadComm(lngComHandle)) '读串口
Else
''' txtRecive.Text = txtRecive.Text & " " & ReadString(ReadComm(lngComHandle)) '读串口
End If
Else
''' txtRecive.Text = ReadString(ReadComm(lngComHandle)) '读串口
End If
End If
''' End If
If optTextMode(1).Value Then
'文本接收
If lngP <> 0 Then
If txtRecive.Text <> "" Then
''' If chkHuanhang.Value = vbChecked Then
''' txtRecive.Text = txtRecive.Text & vbCrLf & BytesToString(ReadComm(lngComHandle)) '读串口
Else
''' txtRecive.Text = txtRecive.Text & " " & BytesToString(ReadComm(lngComHandle)) '读串口
End If
Else
''' txtRecive.Text = BytesToString(ReadComm(lngComHandle)) '读串口
End If
End If
''' End If
''' lngT = GetTickCount - lngCommStartTime
''' lngCommTotalTime = lngCommTotalTime + lngT
txtRecive.Text = txtRecive.Text & " (" & CStr(lngT) & "ms)"
''' lblCommAVG.Caption = "通信次数:" & CStr(lngCommCount) & "次 平均通信时间:" & _
Format(CStr(lngCommTotalTime / lngCommCount), "##00.00") & "ms"
''' lblReciveCount.Caption = "接收字节数: " & CStr(lngReciveCount)
Exit Sub
errSub:
Debug.Print Err.Description
End Sub
我用这个代码写过好几个工程,都没有在这几句上出过问题
如果是这几句出问题了,说明前面的代码可能也有问题。多问一句,错误提示是什么?你是全编译运行的吗?(Ctrl+F5)