跪求:VB mscomm串口通讯不能接收数据,利用串口调试助手却可以接收数据,急!!!见鬼了
源代码如下:PublicClass位置及气压PublicFunctionsend(ByVals()AsByte)AsByte()Dimm(17)AsByteDimiAsB...
源代码如下:
Public Class 位置及气压
Public Function send(ByVal s() As Byte) As Byte()
Dim m(17) As Byte
Dim i As Byte = 0 '求解异或和效验码
For j = 0 To 6
i = i Xor s(j)
Next j
ReDim m(17) '转变成ASCII码
m(0) = Hex(Asc("g"))
m(17) = Hex(Asc("G"))
For h = 1 To 7
m(h * 2 - 1) = Hex(Asc(Hex(s(h - 1) \ &H10)))
m(h * 2) = Hex(Asc(Hex(s(h - 1) Mod &H10)))
Next h
m(15) = Hex(Asc(Hex(i \ &H10)))
m(16) = Hex(Asc(Hex(i Mod &H10)))
send = m
End Function
Private Sub 位置及气压_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With AxMSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InputMode = MSCommLib.InputModeConstants.comInputModeBinary
.RThreshold = 1 '接收到的字符数大于等于1就会产生接收事件
.InputLen = 0 '读出接收缓冲区的所有内容
.OutBufferCount = 0 '清空发送缓冲区
.InBufferCount = 0 '清空接收缓冲区
End With
If AxMSComm1.PortOpen = False Then '打开串口
AxMSComm1.PortOpen = True
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim s() As Byte = {&H5, 2, 1, &H4, &H0, 0, 0} '七位字符 十六进制
Dim m(17) As Byte
m = send(s)
AxMSComm1.Output = m
For i = 0 To 17
TextBox6.Text = TextBox6.Text + Str$(m(i))
Next i
'AxMSComm1.OutBufferCount = 0
End Sub
Private Sub 退出_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出.Click
Me.DialogResult = DialogResult.OK
End Sub
Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs)
AxMSComm1.RThreshold = 0 '触发axmscomm1_oncomm()事件后,接收缓冲区中所有数据
Dim buff() As Byte = {0}
If AxMSComm1.CommEvent = 2 Then
buff = AxMSComm1.Input
End If
For i = 0 To AxMSComm1.InBufferCount - 1
'buff(i) = x(i)
TextBox5.Text = TextBox5.Text + (buff(i))
Next i
' AxMSComm1.InBufferCount = 0
End Sub
End Class 展开
Public Class 位置及气压
Public Function send(ByVal s() As Byte) As Byte()
Dim m(17) As Byte
Dim i As Byte = 0 '求解异或和效验码
For j = 0 To 6
i = i Xor s(j)
Next j
ReDim m(17) '转变成ASCII码
m(0) = Hex(Asc("g"))
m(17) = Hex(Asc("G"))
For h = 1 To 7
m(h * 2 - 1) = Hex(Asc(Hex(s(h - 1) \ &H10)))
m(h * 2) = Hex(Asc(Hex(s(h - 1) Mod &H10)))
Next h
m(15) = Hex(Asc(Hex(i \ &H10)))
m(16) = Hex(Asc(Hex(i Mod &H10)))
send = m
End Function
Private Sub 位置及气压_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With AxMSComm1
.CommPort = 1
.Settings = "9600,n,8,1"
.InputMode = MSCommLib.InputModeConstants.comInputModeBinary
.RThreshold = 1 '接收到的字符数大于等于1就会产生接收事件
.InputLen = 0 '读出接收缓冲区的所有内容
.OutBufferCount = 0 '清空发送缓冲区
.InBufferCount = 0 '清空接收缓冲区
End With
If AxMSComm1.PortOpen = False Then '打开串口
AxMSComm1.PortOpen = True
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim s() As Byte = {&H5, 2, 1, &H4, &H0, 0, 0} '七位字符 十六进制
Dim m(17) As Byte
m = send(s)
AxMSComm1.Output = m
For i = 0 To 17
TextBox6.Text = TextBox6.Text + Str$(m(i))
Next i
'AxMSComm1.OutBufferCount = 0
End Sub
Private Sub 退出_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出.Click
Me.DialogResult = DialogResult.OK
End Sub
Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs)
AxMSComm1.RThreshold = 0 '触发axmscomm1_oncomm()事件后,接收缓冲区中所有数据
Dim buff() As Byte = {0}
If AxMSComm1.CommEvent = 2 Then
buff = AxMSComm1.Input
End If
For i = 0 To AxMSComm1.InBufferCount - 1
'buff(i) = x(i)
TextBox5.Text = TextBox5.Text + (buff(i))
Next i
' AxMSComm1.InBufferCount = 0
End Sub
End Class 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询