求各位帮帮忙mscomm控件的接收数据
Dim data, data1 As Variant '串口初始化
Private Sub Form_Load()
MSComm1.CommPort = 1 ' 设置通信端口号为COM1
MSComm1.Settings = "9600,n,8,1" ' 设置串口1参数
MSComm1.InputMode = comInputModeBinary ' 接收文本型数据
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True ' 打开通信端口1
End Sub
Private Sub Command1_Click()
MSComm1.OutBufferCount = 0
MSComm1.Output = "#018" + (vbCr)
End Sub
Private Sub MSComm1_OnComm()
Dim s() As Byte
Dim SS(1024) As Byte
Static N As Long
Static T As Variant
If (MSComm1.CommEvent = comEvReceive) Then
s = MSComm1.Input '只要有数据就收进来,哪怕只是一个
If (Timer - T > 0.0001) Then '间隔10MS以上就认为是一个新的包
Text2 = "" 'text2用于搜集和显示接收(HEX格式)
N = 0
End If
T = Timer
For i = 0 To UBound(s) '一个数据包可能产生若干个oncomm事件
Text2.Text = Text2.Text & Right("0" & Hex(s(i)) & "H", 3) + " "
SS(N + i) = s(i) '接收数据包缓存于SS()
N = N + UBound(s)
Next i
End If
End Sub
Private Sub CmdReceive_Click()
Dim InString As String
MSComm1.InputLen = 0 '读取所有可用数据。
If MSComm1.InBufferCount Then '检查数据。
InString = MSComm1.Input ' 读入数据
End If
End Sub
这是东拼西凑的数据。发送一个命令每次只能接收8个字节。我想要完整显示27个字节。然后在listbox里面显示。谁能帮帮忙,说是一次接收一个字节。然后用数组的方式。求指导啊
[解决办法]
http://download.csdn.net/detail/veron_04/1262066
[解决办法]
Option ExplicitDim data, data1 As Variant '串口初始化Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Sub Form_Load() MSComm1.CommPort = 1 ' 设置通信端口号为COM1 MSComm1.Settings = "9600,n,8,1" ' 设置串口1参数 MSComm1.InputMode = comInputModeBinary ' 接收文本型数据 MSComm1.RThreshold = 1 MSComm1.InputLen = 0 If MSComm1.PortOpen = True Then MSComm1.PortOpen = False If MSComm1.PortOpen = False Then MSComm1.PortOpen = True ' 打开通信端口1End SubPrivate Sub Command1_Click() Dim outbyte(0 To 4) As Byte outbyte(0) = Asc("#") outbyte(1) = Asc("0") outbyte(2) = Asc("1") outbyte(3) = Asc("8") outbyte(4) = Asc(vbCr) MSComm1.OutBufferCount = 0 MSComm1.Output = outbyteEnd SubPrivate Sub MSComm1_OnComm() Dim i As Integer Dim s() As Byte Dim SS(1024) As Byte Static N As Long Static T As Variant If (MSComm1.CommEvent = comEvReceive) Then MSComm1.RThreshold = 0 '设置此参数为0,禁止OnComm再次触发 Sleep 10 '等待10ms以便数据全部上传 N = 0 Text2.Text = "" Do data = MSComm1.Input '只要有数据就收进来,哪怕只是一个 If Not IsNull(data) Then SS(N) = data(0) N = N + 1 Text2.Text = Text2.Text & Hex(SS(N)) & " " End If Loop Until MSComm1.InBufferCount = 0 End IfEnd Sub