在VB中MSComm1控件有发送但MSComm1_OnComm()一直不触发?急急 255
DimkkAsIntegerPrivateSubForm_Load()IfMSComm1.PortOpen=TrueThenMSComm1.PortOpen=FalseM...
Dim kk As Integer
Private Sub Form_Load()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = 8 '设置端口号
MSComm1.Settings = "38400,n,8,1" '设置波特率等
MSComm1.InBufferSize = 1 '设置接收器缓冲区大小
MSComm1.OutBufferSize = 1 '设置发送器缓冲区大小 '串口初始化
MSComm1.RThreshold = 1 '设置当当接收多少字符后触发oncomm事件
MSComm1.InputMode = 0 '设置输入寄存器接收数据的形式,0为文本、1为二进制形式
MSComm1.OutBufferCount = 0 '清除输出缓冲区的数据
MSComm1.InBufferCount = 0 '清除输出入缓冲区的数据
Timer1.Enabled = False
Timer2.Enabled = False
kk = 0
If strRW <> "" Then
Call FUN_FSSJ(strRW)
End If
End Sub
Private Function FUN_FSSJ(STR_RWSJ As String)
ReDim Sent(Len(strRW) / 2 - 1)
ReDim int_RWFS(Len(strRW) / 2)
Dim int_n As Integer
Dim Sent1() As Byte
For int_n = 0 To Len(strRW) / 2 - 1
int_RWFS(int_n) = Val("&h" & Mid(STR_RWSJ, int_n * 2 + 1, 2)) '将任务数据拆分成6B
Sent(int_n) = int_RWFS(int_n)
Next int_n
On Error GoTo FUN_FSSJ_ERROR
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.OutBufferCount = 0 '清空缓存
MSComm1.InBufferCount = 0
MSComm1.Output = Sent() '发送数据
Timer1.Enabled = True '开启回信超时检测定时器
Exit Function
FUN_FSSJ_ERROR:
On Error Resume Next
MsgBox "串口通信不正常,请检查!" '清空通信错误
MSComm1.PortOpen = False '关闭串口
End Function
Private Sub Timer1_Timer()
If kk <= 2 Then
kk = kk + 1
Call FUN_FSSJ(strRW)
Else
Timer1.Enabled = False
MsgBox ("通信超时!!")
kk = 0
strRW = ""
End If
End Sub
'***********************
'串口接收事件
'***********************
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputMode = 1
CKHX = MSComm1.Input
Timer1.Enabled = False
strRW = ""
End Select
End Sub 展开
Private Sub Form_Load()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = 8 '设置端口号
MSComm1.Settings = "38400,n,8,1" '设置波特率等
MSComm1.InBufferSize = 1 '设置接收器缓冲区大小
MSComm1.OutBufferSize = 1 '设置发送器缓冲区大小 '串口初始化
MSComm1.RThreshold = 1 '设置当当接收多少字符后触发oncomm事件
MSComm1.InputMode = 0 '设置输入寄存器接收数据的形式,0为文本、1为二进制形式
MSComm1.OutBufferCount = 0 '清除输出缓冲区的数据
MSComm1.InBufferCount = 0 '清除输出入缓冲区的数据
Timer1.Enabled = False
Timer2.Enabled = False
kk = 0
If strRW <> "" Then
Call FUN_FSSJ(strRW)
End If
End Sub
Private Function FUN_FSSJ(STR_RWSJ As String)
ReDim Sent(Len(strRW) / 2 - 1)
ReDim int_RWFS(Len(strRW) / 2)
Dim int_n As Integer
Dim Sent1() As Byte
For int_n = 0 To Len(strRW) / 2 - 1
int_RWFS(int_n) = Val("&h" & Mid(STR_RWSJ, int_n * 2 + 1, 2)) '将任务数据拆分成6B
Sent(int_n) = int_RWFS(int_n)
Next int_n
On Error GoTo FUN_FSSJ_ERROR
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.OutBufferCount = 0 '清空缓存
MSComm1.InBufferCount = 0
MSComm1.Output = Sent() '发送数据
Timer1.Enabled = True '开启回信超时检测定时器
Exit Function
FUN_FSSJ_ERROR:
On Error Resume Next
MsgBox "串口通信不正常,请检查!" '清空通信错误
MSComm1.PortOpen = False '关闭串口
End Function
Private Sub Timer1_Timer()
If kk <= 2 Then
kk = kk + 1
Call FUN_FSSJ(strRW)
Else
Timer1.Enabled = False
MsgBox ("通信超时!!")
kk = 0
strRW = ""
End If
End Sub
'***********************
'串口接收事件
'***********************
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputMode = 1
CKHX = MSComm1.Input
Timer1.Enabled = False
strRW = ""
End Select
End Sub 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询