导航:首页 > 操作系统 > vb与单片机通信程序

vb与单片机通信程序

发布时间:2022-07-29 12:48:12

Ⅰ 如何用VB通过串口和51单片机通讯使得单片机能够在IO口产生脉冲,脉冲的数量和频率由VB设定

这个需要分步来完成
1、实现单片机串口通讯
2、编写VB程序,添加串口控件,实现VB与单片机串口通讯
3、写单片机IO驱动,实现脉冲输出
4、VB发数据给单片机,实现脉冲数量和频率控制
5、更详细的可以私信我完成

Ⅱ VB实现单片机PC串口通讯

最好不要用
Private
Sub
MSComm1_OnComm事件
向串口发完指令后就等待回应就行了
首先将
MSComm1.InputLen
=
0
读出所有数据
MSComm1.InputMode=comInputModeBinary
with
mscomm1
do
doevents
if
.InBufferCount>9
then
exit
do
loop
dim
varstr
as
Variant
varstr=.input
end
with
所有数据都在varstr数组里了

Ⅲ VB和单片机通讯

1. 3F没有显示在Text1.text 里面呀,先把数放在Text1.text 里面,观察接收到的3F是不是字符型的,"3F"是字符的3和F两个字符,如果是十六进制的3F是"?"
2. 如果显示在Text1.text 里面了,观察是不是还有其他字符在Text1.text 里面,如空格,回车、换行,tab键等,
3. 没有代码不知道还有没有其他问题,如:发送时把“执行”使能禁止,发送结束后在程序的返回前再允许“执行”键的使能,可观察发送没有有交出控制权。还有是不是232接收的中断开启了,程序有跳进了其他程序等等,只有见到代码才能确定具体的问题

Ⅳ 我想利用VB和单片机进行实时通讯

单片机都大同小异。
在初始化或程序的开头部分,设置UART,UART就是单片机的串口通信口,有的单片机UART口和正常的IO口是公用一个引脚的,这样就得在初始化的时候设置该IO口为外设端口。另外说明一下,单片机的IO口 和串口(UART)一般只接受TTL电平,不能接收232接口的,因此要有外围电路进行转换,一般有专用芯片直接接上就可以的。 在接收到之后,就可以处理数据了。
如果你的单片机上不带串口通讯(UART) 那么就要自己用平常的IO口模拟出一个UART了,程序写起来虽然不多,但比较麻烦。

Ⅳ vb与单片机串口通讯

两个程序,都是新编的,也许都有毛病。

最好使用“串口调试助手”软件,分别调试你其中的一个程序;

分别都调试成功后,再用你的两个程序通信。

Ⅵ VB和单片机串口通信

下面是我做的一个测试程序:
'TextReceive =
' 初始化程序:主要完成对串口的设置,包括选择串口、设置波特率、设置数据格式、打开串口等。
' 注意:在程序开始之前,必须清空发送和接收缓冲区,以免出错。
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1" ' 设置波特率和发送字符格式
MSComm1.CommPort = 1 ' 设置通讯串口
MSComm1.InputLen = 0 ' 设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
MSComm1.InBufferSize = 2 ' 设置接收缓冲区2Byte
MSComm1.InBufferCount = 0
MSComm1.OutBufferSize = 2 ' 设置发送缓冲区2Byte
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1 ' 每个字符到接收缓冲区都触发接收事件
MSComm1.SThreshold = 1
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.PortOpen = True ' 打开串口
End Sub
Private Sub Command1_Click()
Dim number As Integer
Dim outbyte(0) As Byte
number = Val(170)
outbyte(0) = CByte(number)
MSComm1.Output = outbyte
Label4 = outbyte(0)
Print outbyte(0)
End Sub

Ⅶ 单片机与VB的通信

分有没多点啊?

程序在这:

VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5235
ClientLeft = 60
ClientTop = 465
ClientWidth = 6990
Icon = "Form1.frx":0000
LinkTopic = "Form1"
ScaleHeight = 5235
ScaleWidth = 6990
StartUpPosition = 3 '窗口缺省
Begin MSCommLib.MSComm MS
Left = 3840
Top = 4080
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.CommandButton Command5
Caption = "清除"
Height = 375
Left = 3600
TabIndex = 26
Top = 3240
Width = 615
End
Begin VB.CommandButton Command4
Caption = "清除"
Height = 375
Left = 3600
TabIndex = 25
Top = 2040
Width = 615
End
Begin VB.Frame Frame2
Height = 555
Left = 120
TabIndex = 14
Top = 0
Width = 6795
Begin VB.ComboBox Comset0
Height = 300
Index = 0
ItemData = "Form1.frx":0442
Left = 540
List = "Form1.frx":046A
Style = 2 'Dropdown List
TabIndex = 19
Top = 180
Width = 735
End
Begin VB.ComboBox Comset
Height = 300
Index = 1
ItemData = "Form1.frx":04B9
Left = 1920
List = "Form1.frx":04F0
Style = 2 'Dropdown List
TabIndex = 18
Top = 180
Width = 915
End
Begin VB.ComboBox Comset
Height = 300
Index = 2
ItemData = "Form1.frx":0561
Left = 3480
List = "Form1.frx":056E
Style = 2 'Dropdown List
TabIndex = 17
Top = 180
Width = 915
End
Begin VB.ComboBox Comset
Height = 300
Index = 3
ItemData = "Form1.frx":0589
Left = 4920
List = "Form1.frx":059C
Style = 2 'Dropdown List
TabIndex = 16
Top = 180
Width = 615
End
Begin VB.ComboBox Comset
Height = 300
Index = 4
ItemData = "Form1.frx":05AF
Left = 6120
List = "Form1.frx":05B9
Style = 2 'Dropdown List
TabIndex = 15
Top = 180
Width = 615
End
Begin VB.Label Label2
Caption = "串口"
Height = 195
Index = 5
Left = 120
TabIndex = 24
Top = 240
Width = 435
End
Begin VB.Label Label2
Caption = "波特率"
Height = 195
Index = 1
Left = 1380
TabIndex = 23
Top = 240
Width = 555
End
Begin VB.Label Label2
Caption = "数据位"
Height = 255
Index = 2
Left = 4380
TabIndex = 22
Top = 240
Width = 615
End
Begin VB.Label Label2
Caption = "奇偶位"
Height = 255
Index = 3
Left = 2880
TabIndex = 21
Top = 240
Width = 675
End
Begin VB.Label Label2
Caption = "停止位"
Height = 255
Index = 4
Left = 5580
TabIndex = 20
Top = 240
Width = 555
End
End
Begin VB.TextBox Text3
Height = 735
Left = 360
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 12
Text = "Form1.frx":05C3
Top = 2040
Width = 3015
End
Begin VB.TextBox Text1
Height = 1215
Left = 360
MultiLine = -1 'True
TabIndex = 11
Text = "Form1.frx":05CA
Top = 720
Width = 1695
End
Begin VB.Timer Timer1
Interval = 500
Left = 4680
Top = 4440
End
Begin VB.CommandButton Command3
Caption = "DTR-ON"
Height = 375
Left = 5640
TabIndex = 6
Top = 4080
Width = 975
End
Begin VB.CommandButton Command2
Caption = "RTS-OFF"
Height = 375
Left = 5640
TabIndex = 5
Top = 3480
Width = 975
End
Begin VB.CommandButton Command1
Caption = "端口状态"
Height = 375
Left = 4920
TabIndex = 3
Top = 2400
Visible = 0 'False
Width = 255
End
Begin VB.TextBox Text2
Height = 1455
Left = 360
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 2
Top = 3240
Width = 3060
End
Begin VB.CommandButton Send
Caption = "发送"
Height = 615
Left = 5640
TabIndex = 1
Top = 2280
Width = 975
End
Begin VB.CommandButton Opencom
Caption = "打开串口"
Height = 615
Left = 5640
TabIndex = 0
Top = 960
Width = 975
End
Begin VB.PictureBox MS2
Height = 480
Left = 5400
ScaleHeight = 420
ScaleWidth = 1140
TabIndex = 27
Top = 4680
Width = 1200
End
Begin VB.Label Label6
Caption = "接收:"
Height = 375
Left = 360
TabIndex = 13
Top = 3000
Width = 495
End
Begin VB.Label Label5
Caption = "外设状态:"
Height = 375
Left = 2160
TabIndex = 10
Top = 1320
Width = 855
End
Begin VB.Label Label4
Caption = "测试外设是否存在"
Height = 495
Left = 4680
TabIndex = 9
Top = 4080
Width = 855
End
Begin VB.Label Label3
Caption = "请求发送"
Height = 255
Left = 4680
TabIndex = 8
Top = 3600
Width = 855
End
Begin VB.Label Label2
Caption = "端口状态"
Height = 375
Index = 0
Left = 2160
TabIndex = 7
Top = 720
Width = 735
End
Begin VB.Label Label1
Height = 375
Left = 3240
TabIndex = 4
Top = 1320
Width = 1815
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private CCount As Integer
Private Hexchange As Boolean
Private RTS As Boolean
Private Sendhexdata() As Byte
Private Nosend As Boolean

Private Sub Combo1_Click()
On Error GoTo Combo1_Error
If MS.PortOpen Then
If MS.CommPort <> (Combo1.ListIndex + 1) Then
If MsgBox("端口" & MS.CommPort & "正在使用,你要停止它并使用新的端口吗?", vbYesNo + vbExclamation + vbDefaultButton2, "更改端口?") = vbYes Then
Call Opencom_Click
MS.CommPort = Combo1.ListIndex + 1
Label1 = " 端口更改为" & MS.CommPort
Else
Combo1.ListIndex = MS.CommPort - 1
Exit Sub
End If
End If
Else
MS.CommPort = Combo1.ListIndex + 1
End If

Exit Sub
Combo1_Error:
MsgBox "端口打开错误", vbInformation

End Sub

Private Sub Command1_Click()
Text1 = "CDHolding:" & MS.CDHolding & Chr(13) & " CTSHolding:" & MS.CTSHolding & Chr(13) _
& " DSRHolding:" & MS.DSRHolding & Chr(13) & " InBufferCount:" & MS.InBufferCount & Chr(13) _
& " OutBufferCount:" & MS.OutBufferCount & Chr(13) & " PortOpen:" & MS.PortOpen

End Sub

Private Sub Command2_Click()
If MS.RTSEnable Then
MS.RTSEnable = False
Command2.Caption = "RTS-OFF"
Else
MS.RTSEnable = True
Command2.Caption = "RTS-ON"
End If
Call Command1_Click
End Sub

Private Sub Command3_Click()
If MS.DTREnable Then
MS.DTREnable = False
Command3.Caption = "DTR-OFF"
Else
MS.DTREnable = True
Command3.Caption = "DTR-ON"
End If
Call Command1_Click
End Sub

Private Sub Command4_Click()
Text3 = ""
End Sub

Private Sub Command5_Click()
Text2 = ""
End Sub

Private Sub Comset_Click(Index As Integer) '端口属性值设置
On Error GoTo ComsetError
comsetd1 = Comset(2).ListIndex
comsett1 = "n"
Select Case comsetd1
Case 0
comsett1 = "n"
Case 1
comsett1 = "o"
Case 2
comsett1 = "e"
End Select

MS.Settings = Comset(1).Text & "," & comsett1 & "," & Comset(3).Text & "," & Comset(4).Text

Exit Sub
ComsetError: '错误处理
MsgBox "端口属性值无效!" & Te & Te & "将恢复为默认值(9600,N,8,1)", vbCritical
MS.Settings = "9600,n,8,1"
Comset(1).ListIndex = 9
Comset(2).ListIndex = 0
Comset(3).ListIndex = 0
Comset(4).ListIndex = 0
End Sub

Private Sub Comset0_Click(Index As Integer)
If MS.PortOpen Then Call Opencom_Click
MS.CommPort = Comset0(0).ListIndex + 1
End Sub

Private Sub Form_Load()
Comset0(0).ListIndex = 3
Comset(1).ListIndex = 9
Comset(2).ListIndex = 0
Comset(3).ListIndex = 0
Comset(4).ListIndex = 0
Call Timer1_Timer
End Sub

Private Sub MS_OnComm()
On Error GoTo OncommError
Dim Breceive() As Byte
Dim Sreceive As String
Dim intValue As Integer

Select Case MS.CommEvent

Case comEvReceive '接收数据

Sreceive = MS.Input
Text2 = Text2 & Sreceive '字符接收

' MsgBox "wate:"

'--------------------------------

Case 1001
MsgBox "收到断开信号!", vbInformation
Case 1002
MsgBox "准备发送时,CTS为低", vbInformation
Case 1003
MsgBox "准备发送时,DSR为低", vbInformation
Case 1004
MsgBox "数据帧错误!", vbCritical
Case 1006
MsgBox "端口溢出!", vbCritical
Case 1007
MsgBox "准备发送时,CD为低", vbInformation
Case 1008
MsgBox "接收缓冲区溢出!", vbCritical
Case 1009
MsgBox "奇偶校验错!", vbCritical
Case 1010
MsgBox "发送缓冲区满!", vbCritical
Case 1011
MsgBox "检取端口时,发生了没预料的错误!", vbCritical
'------------
Case 1
MsgBox "发送缓冲区字符小于设定值!", vbExclamation
Case 2
MsgBox "接收到" & MS.RThreshold & "个字符"
Case 3
'MsgBox IIf(MS.ctsRHolding, "可以发送", "不能发送")
Label1 = IIf(MS.CTSHolding, "可以发送", "不能发送")
Case 4
'MsgBox IIf(MS.dsrHolding, "外设接入", "外设移出")
Label1 = IIf(MS.DSRHolding, "外设接入", "外设移出")
Case 5
'MsgBox IIf(MS.CDHolding, "响铃", "")
Label1 = IIf(MS.CDHolding, "CD置位", "CD复位")
Case 6
Label1 = IIf(MS.CDHolding, "响铃", "不响")
Case Else
MsgBox "意外事件!代码:" & MS.CommEvent, vbCritical
End Select

Exit Sub
OncommError:
MsgBox "设备出错!代码:" & Err.Number, vbCritical

End Sub

Private Sub Opencom_Click()
On Error GoTo PortError
If Not MS.PortOpen Then
MS.PortOpen = True
Opencom.Caption = "关闭串口"
Label1 = "设备 " & MS.Object & " Id: " & MS.CommID
Else
MS.PortOpen = False
Opencom.Caption = "打开串口"
End If
Call Command1_Click
Exit Sub
PortError: '错误处理
Select Case Err.Number
Case 8002
MsgBox "串口 " & MS.CommPort & " 不存在!", vbExclamation
Case 8005
MsgBox "串口 " & MS.CommPort & " 被占用!", vbInformation
Case 380
MsgBox "无效的属性值!", vbInformation
Case 8012
MsgBox "串口 " & MS.CommPort & " 没有打开!", vbInformation
Case 8015
MsgBox "不能设置串口 " & MS.CommPort & "通信状态!", vbInformation
Case 8019
MsgBox "串口 " & MS.CommPort & " 忙!", vbInformation
Case 8020
MsgBox "串口 " & MS.CommPort & " 错误!", vbInformation
Case Else
MsgBox "其它错误:" & Err.Number, vbInformation
End Select
End Sub

Private Sub Send_Click()
Dim Send_text As String

If Not MS.PortOpen Then Call Opencom_Click
If Not MS.PortOpen Then Exit Sub

If Not MS.DSRHolding Then GoTo NoConnect
If Not MS.CTSHolding Then
'MsgBox "外设不允许发送!"
'Exit Sub
End If
Send_text = Text3
MS.Output = Send_text
Exit Sub

NoConnect:
MsgBox "外设" & MS.CommPort & "没接入"
End Sub

Private Sub Timer1_Timer()
Call Command1_Click
Form1.Caption = Now
End Sub

阅读全文

与vb与单片机通信程序相关的资料

热点内容
受信任的服务器是什么意思 浏览:252
服务器地址的分析过程 浏览:957
麦当劳点餐app哪个好 浏览:585
手机网站图片压缩 浏览:172
前端程序员干嘛 浏览:564
女权主义pdf 浏览:458
阿里云服务器低价续费 浏览:337
python监控日志脚本 浏览:134
云服务器实例是什么意思 浏览:710
小寻app是做什么的 浏览:649
c语言中编译和运行 浏览:1000
画流图找循环编译原理 浏览:158
oppo手机西瓜视频的文件夹 浏览:867
骑手一般用哪个app 浏览:610
程序员老板用什么手机 浏览:848
比心app头像不通过为什么 浏览:105
加密币市值前十走势 浏览:190
单片机学习推荐课程 浏览:474
对数ln的运算法则图片 浏览:735
仿微博app源码 浏览:781