當前位置:編程學習大全網 - 編程語言 - VB怪問題,望高手指點,可能是生成exe的關系~~~~

VB怪問題,望高手指點,可能是生成exe的關系~~~~

Attend.MSComm2.RThreshold = 10

這壹句代表當串口接收到10個數據後產生中斷。

建議設為1試試。

Attend.MSComm2.InBufferSize = 40

這壹句建議設為4096

我只能說,很多USB接口轉串口的轉換器都是虛擬串口的軟中斷,在連續通訊時,幾乎是不能正常工作的。

以前買過的USB轉串口都是不能用,後來買PCI轉串口的立刻正常了。

剛剛測試完畢的串口接收測試,低速測試通過。

拷貝下列代碼保存為.frm即可。

用這個編譯測試看看是不是會收到多次數據。

VERSION 5.00

Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "mscomm32.ocx"

Begin VB.Form Form1

BorderStyle = 3 'Fixed Dialog

Caption = "Form1"

ClientHeight = 6090

ClientLeft = 45

ClientTop = 330

ClientWidth = 5580

LinkTopic = "Form1"

MaxButton = 0 'False

MinButton = 0 'False

ScaleHeight = 6090

ScaleWidth = 5580

ShowInTaskbar = 0 'False

StartUpPosition = 3 '窗口缺省

Begin VB.ComboBox Combo1

Height = 300

Left = 2640

Style = 2 'Dropdown List

TabIndex = 4

Top = 5640

Width = 855

End

Begin VB.Timer Timer1

Enabled = 0 'False

Interval = 40

Left = 240

Top = 4560

End

Begin VB.CommandButton Command2

Caption = "清 除"

Height = 495

Left = 1440

TabIndex = 2

Top = 5400

Width = 975

End

Begin MSCommLib.MSComm MSComm1

Left = 840

Top = 4440

_ExtentX = 1005

_ExtentY = 1005

_Version = 393216

CommPort = 3

DTREnable = -1 'True

StopBits = 2

End

Begin VB.CommandButton Command1

Caption = "開 始"

Height = 495

Left = 120

TabIndex = 1

Top = 5400

Width = 1095

End

Begin VB.TextBox Text1

Height = 5055

Left = 120

MultiLine = -1 'True

ScrollBars = 2 'Vertical

TabIndex = 0

Top = 120

Width = 5295

End

Begin VB.Shape Shape1

BackColor = &H80000004&

BackStyle = 1 'Opaque

Height = 255

Left = 4920

Shape = 3 'Circle

Top = 5640

Width = 375

End

Begin VB.Label Label3

Caption = "Byte"

Height = 255

Left = 3600

TabIndex = 6

Top = 5640

Width = 495

End

Begin VB.Label Label2

Caption = "接收數據長度"

Height = 255

Left = 2640

TabIndex = 5

Top = 5280

Width = 1215

End

Begin VB.Label Label1

Alignment = 1 'Right Justify

AutoSize = -1 'True

Caption = "0 Byte"

Height = 180

Left = 3960

TabIndex = 3

Top = 5280

Width = 1380

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Private Sub Combo1_Click()

MSComm1.InputLen = Combo1.ListIndex + 1

End Sub

Private Sub Command1_Click()

MSComm1.PortOpen = Not MSComm1.PortOpen

If MSComm1.PortOpen Then

Command1.Caption = "停 止"

Else

Command1.Caption = "開 始"

End If

End Sub

Private Sub Command2_Click()

Text1.Text = ""

Label1.Caption = "0 Byte"

End Sub

Private Sub Form_Load()

'通訊口初始化:

For i = 1 To 99

Combo1.AddItem i

Next

Combo1.ListIndex = 8

MSComm1.Settings = "9600,n,8,2"

MSComm1.CommPort = 3

MSComm1.InputMode = comInputModeBinary

MSComm1.InputLen = Combo1.ListIndex + 1

MSComm1.InBufferCount = 0

MSComm1.OutBufferCount = 0

MSComm1.RThreshold = Combo1.ListIndex + 1

MSComm1.InBufferSize = 4096

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

If MSComm1.PortOpen Then MSComm1.PortOpen = False

End Sub

'串口接收事件

Private Sub MSComm1_OnComm()

Dim inx() As Byte

Dim strTemp As String

Dim strTemp1 As String

Dim strLen As Byte

Select Case MSComm1.CommEvent

Case comEvReceive '判斷為接收事件

inx = MSComm1.Input '接收數據

MSComm1.InBufferCount = 0

MSComm1.RThreshold = Combo1.ListIndex + 1

For i = 0 To UBound(inx)

strTemp1 = Hex(inx(i))

If Len(strTemp1) > 1 Then

strTemp = strTemp & strTemp1 & " "

Else

strTemp = strTemp & "0" & strTemp1 & " "

End If

Next i

Text1.Text = Text1.Text & strTemp & vbCrLf

Text1.SelStart = Len(Text1.Text)

Label1.Caption = Str(Val(Label1.Caption) + i) & " Byte"

Erase inx

Shape1.BackColor = vbRed

Timer1.Enabled = True

End Select

End Sub

Private Sub Text1_Change()

If Len(Text1.Text) > 9999 Then Text1.Text = ""

End Sub

Private Sub Timer1_Timer()

Shape1.BackColor = &H80000004

Timer1.Enabled = False

End Sub

  • 上一篇:關於64顆棋子裝滿麥粒的成語是什麽?
  • 下一篇:編程網格
  • copyright 2024編程學習大全網