虚心请教MSCOMM接收问题。。。谢谢
Dim Send_Repeater() As ByteDim OutRep As Variant
RepNum = 0
RepNum = Hex(RepNum)
RepBuf(1) = &HFF
RepNum = RepNum + 1
MSComm1.InBufferCount = 0
ReDim Send_Repeater(7)
Send_Repeater(1) = &HA7
Send_Repeater(2) = &HA6
Send_Repeater(3) = &HB6
Send_Repeater(4) = &HB7
Send_Repeater(5) = &HB8
Send_Repeater(6) = RepNum
Send_Repeater(7) = RepOutDat
MSComm1.Output = Send_Repeater
Do While MSComm1.InBufferCount >= 10
DoEvents
Loop
Count = MSComm1.Input
'RepOutDat = Send_Repeater(1) + Send_Repeater(2) + Send_Repeater(3) + Send_Repeater(4) + Send_Repeater(5)
If RepOutDat <> 10 Then
TextRep = "中继器累加和不正确,请检查!"
ElseIf RepOutDat = 10 Then
TextRep = "中继器号:" & RepNum
End If
Text3.Text = Text3.Text & TextRep & vbNewLine
这样写有哪儿不正确吗??我总接收不正确。
=============================
我想用MSCOMM发送一组数据
&HA7,&HA6,&HB6,&HB7,&HB8,累加和(RepOutDat),表号(RepNum)
PC接收到10个字节为正常
谢谢。。。。。。。。。
--------------------编程问答-------------------- RThreshold设为10试试
你应该把收到的数据Debug.print出来,看看究竟收到的哪里错了 --------------------编程问答-------------------- 不能设置为10,这个发送完,还要发另外一组数据 --------------------编程问答-------------------- 再设回来嘛…… --------------------编程问答-------------------- ReDim Send_Repeater(7) 和Count 数组,应该定义为 AS BYTE.你发的是二进制数据,控件的inputmode属性改为BINARY. --------------------编程问答--------------------
Option Explicit--------------------编程问答-------------------- 向高手学习了~~~~~~~
Dim RepNum As Integer
Dim strData As String
Private Sub Command1_Click() '发送一组数据
Dim Send_Repeater() As Byte
Dim OutRep As Variant
Dim RepOutDat As Byte
'RepNum = 0
'RepNum = Hex(RepNum)
'RepBuf(1) = &HFF
RepNum = 1
MSComm1.InBufferCount = 0
ReDim Send_Repeater(1 To 7)
Send_Repeater(1) = &HA7
Send_Repeater(2) = &HA6
Send_Repeater(3) = &HB6
Send_Repeater(4) = &HB7
Send_Repeater(5) = &HB8
Send_Repeater(6) = RepNum
RepOutDat = (Val(Send_Repeater(1)) + Val(Send_Repeater(2)) + Val(Send_Repeater(3)) + Val(Send_Repeater(4)) + Val(Send_Repeater(5))) Mod 256
Send_Repeater(7) = RepOutDat
MSComm1.Output = Send_Repeater
End Sub
Private Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Dim BytReceived() As Byte
Dim i As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
For i = 0 To UBound(BytReceived) '接收数据处理为16进制
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
If Len(strData) = 20 Then
Text1 = strData
strData = ""
Else
'strData = ""
End If
End Select
End Sub
补充:VB , 控件