发表于:2002/3/11 12:33:00
#0楼
下面是我写过的用VB与51单片机以9600bps进行串行通信的试验程序,仅供参考
'----------------------------------------------------------------'
'vb与单片机的全双工串行通信,'
'----------------------------------------------------------------'
Dim data As Byte
Dim OutByte() As Byte '待发送的数据
Dim InByte() As Byte '接收到的数据
Dim h$
Private Sub Command1_Click()
Dim j
i$ = InputBox("请输入待发送的字节型数据(0~255)", "您输入的数据将从串口发出", "")
'For k = 0 To 255 '此循环检查输入的设备号是不是0~255之间的数
' If i = k Then
' j = 1
' Exit For '如果判断出输入的设备号处于0~255之间则退出循环
' Else
' j = 0
' End If
'Next k
If j = 0 Then
OutByte() = StrConv(i, vbFromUnicode)
MSComm1.Output = OutByte()
Else
msg2$ = MsgBox("傻瓜!你必须输入0~255之间的整数!", 16, "严重警告")
End If
End Sub
Private Sub Command2_Click()
Dim j
msg1$ = "请输入待发送的字节型数据(0~255)"
i$ = InputBox(msg1$, "您输入的数据将从串口发出", "")
For k = 0 To 255 '此循环检查输入的设备号是不是0~255之间的数
If i = k Then
j = 1
Exit For '如果判断出输入的设备号处于0~255之间则退出循环
Else
j = 0
End If
Next k
data = i
If j = 1 Then
MSComm1.Output = Chr(data)
Else
msg2$ = MsgBox("傻瓜!你必须输入0~255之间的整数!", 16, "严重警告")
End If
End Sub
Private Sub Form_Load()
h = 0
Text1.Text = ""
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm() 'MSComm1.InputMode=Binary,只有用这种方式才能处
'----------------------------------------------------------------'
'vb与单片机的全双工串行通信,'
'----------------------------------------------------------------'
Dim data As Byte
Dim OutByte() As Byte '待发送的数据
Dim InByte() As Byte '接收到的数据
Dim h$
Private Sub Command1_Click()
Dim j
i$ = InputBox("请输入待发送的字节型数据(0~255)", "您输入的数据将从串口发出", "")
'For k = 0 To 255 '此循环检查输入的设备号是不是0~255之间的数
' If i = k Then
' j = 1
' Exit For '如果判断出输入的设备号处于0~255之间则退出循环
' Else
' j = 0
' End If
'Next k
If j = 0 Then
OutByte() = StrConv(i, vbFromUnicode)
MSComm1.Output = OutByte()
Else
msg2$ = MsgBox("傻瓜!你必须输入0~255之间的整数!", 16, "严重警告")
End If
End Sub
Private Sub Command2_Click()
Dim j
msg1$ = "请输入待发送的字节型数据(0~255)"
i$ = InputBox(msg1$, "您输入的数据将从串口发出", "")
For k = 0 To 255 '此循环检查输入的设备号是不是0~255之间的数
If i = k Then
j = 1
Exit For '如果判断出输入的设备号处于0~255之间则退出循环
Else
j = 0
End If
Next k
data = i
If j = 1 Then
MSComm1.Output = Chr(data)
Else
msg2$ = MsgBox("傻瓜!你必须输入0~255之间的整数!", 16, "严重警告")
End If
End Sub
Private Sub Form_Load()
h = 0
Text1.Text = ""
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub MSComm1_OnComm() 'MSComm1.InputMode=Binary,只有用这种方式才能处