首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB MScomm读的数没有了字母,该怎么处理

2012-04-05 
VB MScomm读的数没有了字母我用串口调试助手每1000MS发送一个16进制的数如“01 02 0A AB 0001 03 5C”.但我

VB MScomm读的数没有了字母
我用串口调试助手每1000MS发送一个16进制的数如“01 02 0A AB 00 01 03 5C”.但我接收的时候,就没有了中间带字母的内容。请大家指点一下,我在学如何用MScomm接收数据。
Dim datatemp(1000) As Single
Dim num As Integer '用于存储采样个数
Dim temp As Integer '仪表号循环变量
Dim bytSend(1 To 8) As Byte

Private Sub CmdStart_Click()
  Timer1.Enabled = True
End Sub

Private Sub CmdStop_Click()
  Timer1.Enabled = False
End Sub

Private Sub Form_Load()
  MSComm1.CommPort = 4
  MSComm1.InputMode = comInputModeBinary
  MSComm1.RThreshold = 1
  MSComm1.SThreshold = 1
  MSComm1.Settings = "9600,n,8,1"
  MSComm1.PortOpen = True
   
End Sub
'获取存储器测量值并显示
'每发送一条指令触发下面事件,并返回数据串
Private Sub MSComm1_OnComm()
  Dim Inbyte() As Byte
  Dim buffer As String
  Dim datatemp2a, datatemp2b As String
  Dim datdtemp2 As String
  If num > 199 Then Call renew
  Select Case MSComm1.CommEvent
  Case comEvReceive
  Inbyte = MSComm1.Input
  For i = 0 To UBound(Inbyte)
  If Len(Hex(Inbyte(i))) = 1 Then
  buffer = buffer + "0" + Hex(Inbyte(i)) + Chr(32)
  Else
  buffte = buffte + Hex(Inbyte(i))
  End If

  'buffer = buffer + Hex(Inbyte(i)) + Chr(32)
  Text1.Text = buffer
  Next i
  Case comEvSend
  'Call Timer2_Timer
  End Select
End Sub

'每隔1000ms向9个测量仪发送读数据指令
Private Sub Timer1_Timer()
  temp = temp + 1
  If temp > 9 Then temp = 0
End Sub
'发送需要知道的测量设备的地址+读命令+寄存器地址+读的长度+CRC16校验
Sub order_num()
  If temp = 1 Then
  bytSend(1) = &H1
  bytSend(2) = &H3
  bytSend(3) = &H0
  bytSend(4) = &H0
  bytSend(5) = &H0
  bytSend(6) = &H6
  bytSend(7) = &HC5
  bytSend(8) = &HC8
  MSComm1.Output = bytSend
  ElseIf temp = 2 Then
  bytSend(1) = &H2
  bytSend(7) = &HC5 'CRC16校验低位
  bytSend(8) = &HFB 'CRC16校验高位
  MSComm1.Output = bytSend
  ElseIf temp = 3 Then
  bytSend(1) = &H3
  bytSend(7) = &HC4
  bytSend(8) = &H2A
  MSComm1.Output = bytSend
  ElseIf temp = 4 Then
  bytSend(1) = &H4
  bytSend(7) = &HC5
  bytSend(8) = &H9D
  MSComm1.Output = bytSend
  ElseIf temp = 5 Then
  bytSend(1) = &H5
  bytSend(7) = &HC4
  bytSend(8) = &H4C
  MSComm1.Output = bytSend
  ElseIf temp = 6 Then
  bytSend(1) = &H6
  bytSend(7) = &HC4
  bytSend(8) = &H7F
  MSComm1.Output = bytSend
  ElseIf temp = 7 Then
  bytSend(1) = &H7
  bytSend(7) = &HC5
  bytSend(8) = &HAE
  MSComm1.Output = bytSend
  ElseIf temp = 8 Then
  bytSend(1) = &H8
  bytSend(7) = &HC5
  bytSend(8) = &H51
  MSComm1.Output = bytSend
  ElseIf temp = 9 Then
  bytSend(1) = &H9
  bytSend(7) = &HC5
  bytSend(8) = &HC8
  MSComm1.Output = bytSend


  Else
  Exit Sub
  End If
 End Sub
  '接收到一组数据后,即清空

Private Sub Timer2_Timer()
  buffer = ""
   
End Sub


[解决办法]
MSComm1.InputMode = comInputModeBinary
这句改为:comInputModeText

热点排行