下面是我的上位机与单片机串口通信的程序,但是在每次刚给下位机上电的时候,串口通信的程序总是会溢出,要执行好多次以后,上位机与单片机才能正常通信,这是为什么呢?
Public Function sample() As Boolean
Dim delay As Integer
Dim TMP As Integer
Dim T As Integer
'Dim Abuf As String
Dim Num As Integer
Dim WTim As Integer
Dim ErrF As Boolean
Dim RecBuf() As Byte
Dim dat1(1000) As Byte
WTim = 0 '重发次数为0
resend:
If Form1.COM.PortOpen = True Then
Form1.COM.Output = "255" '发“传输数据的命令”给单片机
End If
ErrF = False
For TMP = 0 To 9 '延时,以保证单片机正确接受
Call Form1.delay
Next TMP
If Form1.COM.InBufferCount > 0 Then
RecBuf = Form1.COM.Input '取接收缓冲区数据
ElseIf Form1.COM.InBufferCount < 1 Then
Do While Form1.COM.InBufferCount < 1
delay = delay + 1
If (delay = 30000) Then
Form1.ComF.BackColor = RGB(255, 50, 125)
WTim = WTim + 1 '计重发次数
If (WTim < 100) Then
For TMP = 0 To 9
Call Form1.delay
Next TMP
GoTo resend:
Else: GoTo Err:
End If
End If
Loop
End If
[此贴子已经被作者于2007-4-23 9:33:02编辑过]