📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "Simulated FIFO Buffer Handling"
ClientHeight = 4590
ClientLeft = 60
ClientTop = 345
ClientWidth = 7110
LinkTopic = "Form1"
ScaleHeight = 4590
ScaleWidth = 7110
StartUpPosition = 3 'Windows Default
Begin VB.TextBox Text2
Height = 1455
Left = 300
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 5
Text = "Form1.frx":0000
Top = 2400
Width = 6375
End
Begin VB.OptionButton Option2
Caption = "2 Buffer System"
Height = 195
Left = 3180
TabIndex = 4
Top = 1620
Width = 1515
End
Begin VB.OptionButton Option1
Caption = "1 Buffer System"
Height = 195
Left = 1560
TabIndex = 3
Top = 1620
Value = -1 'True
Width = 1455
End
Begin VB.Timer Timer2
Interval = 250
Left = 840
Top = 1560
End
Begin VB.Timer Timer1
Interval = 1
Left = 300
Top = 1560
End
Begin VB.TextBox Text1
Height = 1035
Left = 300
TabIndex = 0
Text = "Text1"
Top = 420
Width = 6375
End
Begin VB.Label Label1
Caption = "Simulated Output"
Height = 255
Index = 1
Left = 300
TabIndex = 2
Top = 2040
Width = 2295
End
Begin VB.Label Label1
Caption = "Simulated Incoming Data"
Height = 255
Index = 0
Left = 360
TabIndex = 1
Top = 60
Width = 2295
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'This source code simulates how to use a 2 buffer
'system to prevent overflows and other problems
'dealing with async modem communications. A 16-byte
'hardware FIFO (present in serial communications) is
'simulated by Text1. If it ever gets above 16
'characters, the Text1_Change event notifies us.
'To simulate serial communications, simply type in
'Text1 as fast as you can. This simulates your
'serial FIFO buffer filling up with data from
'the serial port (modem, straight connection, etc).
'
'If the first option box is checked, we use a 1 buffer
'and 1-timer system. The empty for next loops
'in the control represent time eaten up by all of
'the serial parsing usually necessary in such software.
'You'll notice that if you type as fast as you can
'you can quickly fill the 16 byte simulated serial
'FIFO buffer. This overflow would happen even faster
'in an actual serial communcations routine where data
'transmits many times faster than you can type.
'Overflowing the hardware buffer is never good, as
'AT THE MINIMUM it leads to dropped characters and
'data loss.
'
'If the second box is checked, you'll see that
'all of the routines get shunted to a second timer.
'At that point, the only thing that the first timer
'is supposed to do is to is to empty the simulated
'input FIFO buffer into buffIn as fast as possible.
'buffIN is a form-public variable length string.
'In VB6, a variable-length string can be up to 2gigs
'(2 billion bytes) in size or so, giving you
'worlds more leeway in handling this buffer than the
'16 byte serial buffer.
'
'In practice, you throw a DoEvents in the 2nd
'timer to handle things even more smoothly.
'That way, if the 2nd routine is eating up
'lots of processor time, it releases some of it
'to the first timer so it can continue to
'fill up the input buffer. If you put a DoEvents
'in the first timeer, it would simply increase the
'amount of time necessary to complete the routine,
'causing an even faster buffer overflow.
'Notice that when we are using the 2-buffer system,
'processing may lag behind input by quite a bit
'but we never actually lose any information, nor
'do we overflow the simulated 16-byte FIFO.
Dim buffIn As String
Private Sub Option1_Click()
If Option1.Value = True Then
Timer2.Enabled = False
Else
Timer2.Enabled = True
End If
End Sub
Private Sub Option2_Click()
If Option1.Value = True Then
Timer2.Enabled = False
Else
Timer2.Enabled = True
End If
End Sub
Private Sub Text1_Change()
If Len(Text1) > 16 Then MsgBox "Simulated hardware buffer full!"
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
Private Sub Timer1_Timer()
If Option1.Value = True Then
For x = 1 To 400
For i = 1 To 20000
'just eat up time
Next i
Next x
'the above loop simulates time eaten up because
'of buffer handling code (parsing, etc)
'do everything in this buffer
Text2 = Text2 & Text1.Text
Text1 = ""
Else
buffIn = buffIn & Text1.Text
Text1 = ""
End If
End Sub
Private Sub Timer2_Timer()
'all parsing and code timeouts happen here.
For x = 1 To 400
For i = 1 To 20000
'just eat up time
Next i
DoEvents
Next x
'the above loop simulates time eaten up because
'of buffer handling code (parsing, etc)
Text2 = Text2 & buffIn
buffIn = ""
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -