Ⅰ 如何用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