3个回答
展开全部
使用MSComm控件,设置MSComm1.InputMode = comInputModeBinary
以二进制方式接收字节数据:
Option Explicit
Dim BytReceived() As Byte
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 5 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
Dim i As Long
For i = 0 To UBound(BytReceived)
Debug.Print BytReceived(i)
Next
'数据处理代码
End Select
End Sub
以二进制方式接收字节数据:
Option Explicit
Dim BytReceived() As Byte
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 5 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
Dim i As Long
For i = 0 To UBound(BytReceived)
Debug.Print BytReceived(i)
Next
'数据处理代码
End Select
End Sub
展开全部
新建工程,加入一个mscomm控件,一个textbox控件,设置mscomm控件的端口号码属性为你使用的端口,
修改下面代码中MSComm1.Settings = "9600,n,8,1"的各种设定为你使用的设定
将下面的代码拷贝到代码区。
Private inbuf() As Byte
Private Sub Form_Load()
MSComm1.InputLen = 5
MSComm1.RThreshold = 5
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen
End Sub
Private Sub MSComm1_OnComm()
Dim tmp As String
If MSComm1.CommEvent = 2 Then
inbuf = MSComm1.Input
For i = 0 To UBound(inbuf)
tmp = tmp & CStr(inbuf(i)) & " "
Text1.Text = Text1.Text & tmp & vbCrLf
Next
End If
End Sub
修改下面代码中MSComm1.Settings = "9600,n,8,1"的各种设定为你使用的设定
将下面的代码拷贝到代码区。
Private inbuf() As Byte
Private Sub Form_Load()
MSComm1.InputLen = 5
MSComm1.RThreshold = 5
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen
End Sub
Private Sub MSComm1_OnComm()
Dim tmp As String
If MSComm1.CommEvent = 2 Then
inbuf = MSComm1.Input
For i = 0 To UBound(inbuf)
tmp = tmp & CStr(inbuf(i)) & " "
Text1.Text = Text1.Text & tmp & vbCrLf
Next
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
VB 最简单,上手最快的
推荐 书籍
Visual_Basic与_RS-232_串行通信控制
推荐 书籍
Visual_Basic与_RS-232_串行通信控制
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询