一个VB与单片机通讯问题

来源:百度知道 编辑:UC知道 时间:2024/05/04 17:20:03
'字节数据类型数组,用来存储接收到的一组字节数据
Dim ab(4) As Byte
Dim av As Variant '用来从接收缓冲区读取数据
Dim i As Integer
Dim j As Integer
Dim w As Integer '接收数据个数计数器
Dim b1 As Single
Dim b2 As Single
Dim WW As Single '十进制检测值
Dim MaxW As Single '最大值
Dim MinW As Single '最小值

Private Sub Command1_Click() '数据发送

MSComm1.Output = Text1.Text
'??

End Sub

Private Sub Command2_Click() '数据接收

With MSComm1

.CommPort = 1 '使用COM1

.Setting = "9600,N,8,1" '设置通信口参数

.InBufferSize = 40 '设置MSComm1接收缓冲区为40字节

.OutBufferSize = 2 '设置MSComm1发送缓冲区为2字节

.InputMode = comInputModeBinary '设置接收数据模式为二进制形式

.InputLen = 1 '设置Input 一次从接收缓冲读取字节数为1

.SThreshold = 1 '设置Output 一次从发送缓冲读取字节数为1

.InBufferCount = 0 '清除接收缓冲区

.OutBufferCount = 0 '清除发送

你的程序还差很重要的一个环节:OnComm事件里所要执行的语句没写出来。
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputMode = comInputModeBinary
intInputLen = frmMain.ctrMSComm.InBufferCount
ReDim bytInput(intInputLen)
bytInput = frmMain.ctrMSComm.Input

Call InputManage(bytInput, intInputLen)
End Select
End Sub

Private Sub InputManage(bytInput() As Byte, intInputLenth As Integer)

Dim n As Integer '定义变量及初始化

ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)

For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)

intvalue = bytReceiveByte(n - 1) '把接收到的数据转换成10进制
Wmax(i)=intvalue
Next n

End Sub