VB编程MSCOMM控件从串口接收数据该怎么设置

还要写些什么程序?请高手指点
2024年11月15日 03:56
有3个网友回答
网友(1):

MSComm接收数据的方法很多,简单的如下:
(拷贝代码保存为Form1.frm,打开VB添加MSComm32控件)

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 = 4170
ClientLeft = 45
ClientTop = 330
ClientWidth = 10005
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4170
ScaleWidth = 10005
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command2
Caption = "清 空"
Height = 375
Left = 1680
Style = 1 'Graphical
TabIndex = 3
Top = 3720
Width = 1335
End
Begin MSCommLib.MSComm MSComm1
Left = 720
Top = 2040
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.CommandButton Command1
Caption = "打 开"
Height = 375
Left = 120
Style = 1 'Graphical
TabIndex = 0
Top = 3720
Width = 1335
End
Begin VB.Timer Timer1
Enabled = 0 'False
Interval = 1
Left = 240
Top = 2160
End
Begin VB.TextBox Text1
Height = 3495
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 2
Text = "Form1.frx":0000
Top = 120
Width = 9735
End
Begin VB.Label Label1
BackStyle = 0 'Transparent
Caption = "Label1"
Height = 255
Left = 3840
TabIndex = 1
Top = 3840
Width = 2535
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Timer1.Enabled = True
Command1.BackColor = vbGreen
End Sub

Private Sub Command2_Click()
Text1.Text = ""
End Sub

Private Sub Form_Load()
'通讯口初始化:

With MSComm1
.Settings = "9600,n,8,2"
.CommPort = 3
.InputMode = comInputModeBinary
.InBufferCount = 0
.OutBufferCount = 0
.RThreshold = 0
.SThreshold = 0
.PortOpen = True
End With
Text1.Text = ""
End Sub

Private Sub Timer1_Timer()
'采用轮循法采集数据
Dim inx() As Byte
Dim strTemp As String
Dim strTemp1 As String
Dim ReceivedLen As Integer

Timer1.Enabled = False '关闭定时器

If MSComm1.InBufferCount > 0 Then
ReceivedLen = MSComm1.InBufferCount
inx = MSComm1.Input
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 & Format(Second(Now), "00") & Right(Format(Str(Timer), "0.00"), 3) & " " & strTemp & vbCrLf
Text1.SelStart = Len(Text1.Text)
End If

Timer1.Enabled = True '打开定时器
Label1.Caption = Now()

End Sub

Private Sub Text1_Change()
If Len(Text1.Text) > 10000 Then Text1.Text = ""
End Sub

网友(2):

根据 需要,甚至 可以不做任何设置,都用 默认 的 就可以 的

多看 msdn 吧

有VB 串口 的 入门书籍
Visual_Basic与_RS-232_串行通信控制

网络上找不到,可以 联系!

网友(3):

根据 需要,甚至 可以不做任何设置,都用 默认 的 就可以 的

多看 msdn 吧

有VB 串口 的 入门书籍
Visual_Basic与_RS-232_串行通信控制

网络上找不到,可以 联系。