初学串口通信,求指导。。。(已经给单片机写程序了,单片机仅发送一个字符串,串口助手能收到消息)

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
Text1.Text = Text1.Text + MSComm1.Input
End Select
End Sub
串口端口为4,RThreshold设置为1,其他的就没什么了?只是想把单片机发送的信息读取出来,求指导。。。

你把下面的程序 复制后改名为
.frm(加载上comm控件后)
编译一下,这个程序调试通过了

祝你好运

VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmMain
BorderStyle = 1 'Fixed Single
Caption = "MSComm串口通信示例"
ClientHeight = 3195
ClientLeft = 45
ClientTop = 435
ClientWidth = 4920
Icon = "frmMain.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 3195
ScaleWidth = 4920
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 1
Left = 600
Style = 1 'Graphical
TabIndex = 6
Top = 1800
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 1
Left = 2400
Style = 1 'Graphical
TabIndex = 15
Top = 2640
Width = 255
End
Begin VB.Frame Frame3
Height = 615
Left = 0
TabIndex = 13
Top = 2400
Width = 4815
Begin VB.CommandButton button
Height = 300
Index = 7
Left = 240
Style = 1 'Graphical
TabIndex = 21
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 6
Left = 600
Style = 1 'Graphical
TabIndex = 20
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 5
Left = 960
Style = 1 'Graphical
TabIndex = 19
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 4
Left = 1320
Style = 1 'Graphical
TabIndex = 18
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 3
Left = 1680
Style = 1 'Graphical
TabIndex = 17
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 2
Left = 2040
Style = 1 'Graphical
TabIndex = 16
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 0
Left = 2760
Style = 1 'Graphical
TabIndex = 14
Top = 240
Width = 255
End
Begin VB.Label Label3
Height = 255
Left = 3120
TabIndex = 22
Top = 240
Width = 1575
End
End
Begin VB.CommandButton Command1
Caption = "打开串口"
Height = 495
Left = 3720
TabIndex = 0
Top = 240
Width = 975
End
Begin VB.Frame Frame2
Height = 735
Left = 0
TabIndex = 3
Top = 1560
Width = 4815
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 7
Left = 2760
Style = 1 'Graphical
TabIndex = 12
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 6
Left = 2400
Style = 1 'Graphical
TabIndex = 11
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 5
Left = 2040
Style = 1 'Graphical
TabIndex = 10
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 4
Left = 1680
Style = 1 'Graphical
TabIndex = 9
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 3
Left = 1320
Style = 1 'Graphical
TabIndex = 8
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 2
Left = 960
Style = 1 'Graphical
TabIndex = 7
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 0
Left = 240
Style = 1 'Graphical
TabIndex = 5
Top = 240
Width = 255
End
Begin VB.Label Label4
Height = 255
Left = 3120
TabIndex = 4
Top = 240
Width = 1455
End
End
Begin MSCommLib.MSComm MSComm1
Left = 5160
Top = 120
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.Frame Frame1
Height = 1455
Left = 0
TabIndex = 1
Top = 0
Width = 4815
Begin VB.CommandButton exit_com
Caption = "退出程序"
Height = 495
Left = 3720
TabIndex = 24
Top = 720
Width = 975
End
Begin VB.Timer Timer1
Interval = 500
Left = 4920
Top = 1080
End
Begin VB.Label Label2
Alignment = 2 'Center
Caption = "串口等待打开"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 360
Left = 240
TabIndex = 2
Top = 480
Width = 2040
End
End
Begin VB.Label Label5
Caption = "0"
Height = 255
Left = 5400
TabIndex = 23
Top = 1320
Width = 255
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim dat As Integer

'点击后 数据将发送到MCU
'把按钮数据 转换为 十进制数字 进行发送给下位机
Private Sub button_Click(Index As Integer)
If button.Item(Index).Caption = 1 Then
button.Item(Index).Caption = 0
button.Item(Index).BackColor = vbGreen
Else
button.Item(Index).Caption = 1
button.Item(Index).BackColor = vbRed
End If

Dim i, tmp
tmp = 0
For i = 0 To 7
tmp = tmp + (button.Item(i).Caption) * (2 ^ i)
Next i
Label3.Caption = "发送区:" & tmp & " " & Hex(tmp) '监视显示
dat = tmp
'send (tmp)
End Sub

Private Sub Command1_Click()
On Error GoTo ErrExit
MSComm1.PortOpen = True '关闭端口
Command1.Caption = "关闭串口"
Label2.Caption = "串口已打开"
Label2.ForeColor = vbBlue
'初始化 button 8个按钮
Dim i
For i = 0 To 7
button.Item(i).Caption = 0
button.Item(i).BackColor = vbGreen
button.Item(i).Enabled = True
Next i
Exit Sub

ErrExit: '打开端口
Command1.Caption = "打开串口"
Label2.Caption = "串口未打开"
Label2.ForeColor = vbRed
'初始化 button 8个按钮
' Dim i
For i = 0 To 7
button.Item(i).Caption = 0
button.Item(i).BackColor = vbGreen
button.Item(i).Enabled = False
Next i
ClosePort
End Sub

Public Sub ClosePort()
dat = 0
On Error GoTo ErrExit
MSComm1.PortOpen = False '关闭端口
Exit Sub
ErrExit:
End Sub

Private Sub exit_com_Click()
ClosePort '关闭串口
End
End Sub

Private Sub Form_Load()
If App.PrevInstance = True Then MsgBox "程序已经运行", , "创达友情提示": Exit Sub
Com_Init '串口 初始化

'初始化 button 8个按钮 显示0,白色,不可用状态(只用等待开启串口)
Dim i
For i = 0 To 7
button.Item(i).Caption = 0
button.Item(i).BackColor = vbWhite
button.Item(i).Enabled = False
Next i
End Sub

Private Sub Form_Unload(Cancel As Integer)
exit_com_Click '退出子程序
End Sub

Private Sub Timer1_Timer()
MSComm1_OnComm '实时读取下位机传上的数据
send (dat)
On Error Resume Next
chuli (Val(Label5.Caption)) '对数据进行一些处理 并体现在按钮上 必须Label5.Caption,用别的总是失败
End Sub

'//对数据实时读取
Sub MSComm1_OnComm()
Dim aa_input '数据暂时缓存
Dim tr_input

Select Case frmMain.MSComm1.CommEvent
Case comEvReceive
On Error Resume Next
tr_input = frmMain.MSComm1.Input
If tr_input = "" Then GoTo err1
aa_input = Format(Val(tr_input), "0") ' "#0.000")
txtMsg.Text = txtMsg.Text & aa_input & " "
Label4.Caption = "接收区:" & aa_input & " " & Hex(aa_input)
Label5.Caption = aa_input

err1:
Case Else
frmMain.MSComm1.InBufferCount = 0 '对缓存操作
End Select
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-21
什么类型的单片机,51,avr,pic?追问

c52

相似回答