如何在串口通信中使用VB编程实现单机的自发自收?

发布时间:2024-05-16 05:57 发布:上海旅游网

问题描述:

我已经将串口的2脚RXD和3脚TXD短路连在一块,要在自己的屏幕上看到收发字符,纯编程实现,不能借助其他工具,谢谢啦!
大哥,你的答案我试过了,实现不了啊,我是想用一个串口做到自发自收,能不能再帮忙给看下?谢谢啦

问题解答:

将以下代码另存为form1.frm
然后导入你的程序
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text2
Height = 420
Left = 1170
TabIndex = 2
Text = "Text2"
Top = 945
Width = 960
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 420
Left = 2565
TabIndex = 1
Top = 1035
Width = 600
End
Begin VB.TextBox Text1
Height = 420
Left = 1170
TabIndex = 0
Text = "Text1"
Top = 360
Width = 915
End
Begin MSCommLib.MSComm MSComm1
Left = 4095
Top = 45
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
MSComm1.Output = Text1.Text
End Sub

Private Sub Form_Load()
MSComm1.CommPort = 2
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case 2
Text2.Text = MSComm1.Input
End Select
End Sub

热点新闻