⑴ VB與單片機串口通訊並採集數據,實現實時曲線顯示
這個問題有點太籠統了,不知道你具體需要什麼?
首先把0~6V信號轉換為AD采樣的采樣電壓范圍內,轉換為數字信號後,通過匯流排送給單片機,單片機把數據整理後,通過串口送給PC,PC根據時間單位的要求,定時(比如每秒一次)把採集到的3路信號分別在坐標上描出一個點,點多了就成為了一條實時曲線.
⑵ vb 與單片機通信 &hbb數據收不到
MSComm1.Output = Chr(send3) 這句有問題。
請不要將send3轉化為字元,使用MSComm1.Output = send3
====================================
dim sende(0) as byte
send3(0)=&Hbb
mscomm1.output=send3
⑶ 如何通過vb控制單片機的四個IO口
這跟你約定的VB上位機和MCU協議有關了!
首先確定你的控制模式是上位機VB是串口Send給MCU數據吧?
MCU應該讀串口發的數據吧?
你定義控制幾個IO就應控制幾個IO啊!
如果MCU的不是你編寫的代碼,就應該按照MCU的約定功能去做!反正你首先要確定雙方協議
⑷ VB給單片機發數據問題
Option Explicit
Dim intTime As Integer
Private strSendText As String '發送文本數據
Private bytSendByte() As Byte '發送二進制數據
Private blnReceiveFlag As Boolean
Private blnAutoSendFlag As Boolean
Private intPort As Integer
Private strSet As String
Private intReceiveLen As Integer
Private bytReceiveByte() As Byte
Private strAscii As String '設置初值
Private strHex As String
Private intHexWidth As Integer
Private intLine As Integer
Private m As Integer
Private strAddress As String
'字元表示的十六進制數轉化為相應的整數,錯誤則返回 -1
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1 '出錯信息
End If
ConvertHexChr = test
End Function
'字元串表示的十六進制數據轉化為相應的位元組串,返回轉化後的位元組數
Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六進制(二進制)數據位元組對應值
Dim hstr As String * 1 '高位字元
Dim lstr As String * 1 '低位字元
Dim HighHexData As Integer '高位數值
Dim LowHexData As Integer '低位數值
Dim HexDataLen As Integer '位元組數
Dim StringLen As Integer '字元串長度
Dim Account As Integer
Dim n As Integer
'計數
'txtSend = "" '設初值
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen \ 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do '清除空格
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = " "
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = " "
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字元中斷轉化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then '修正最後一次循環改變的數值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,則不會進入循環體
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
Private Sub cmdManualSend_Click()
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Call ctrTimer_Timer
If Not blnAutoSendFlag Then
Me.MSComm.PortOpen = False
End If
End Sub
Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
Me.ctrTimer.Enabled = False
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
Me.cmdAutoSend.Caption = "自動發送"
Else
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.ctrTimer.Interval = intTime
Me.ctrTimer.Enabled = True
Me.cmdAutoSend.Caption = "停止發送"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End Sub
Private Sub ctrTimer_Timer()
Dim longth As Integer
strSendText = Me.txtSend.Text
longth = strHexToByteArray(strSendText, bytSendByte())
If longth > 0 Then
Me.MSComm.Output = bytSendByte
End If
End Sub
'輸入處理,處理接收到的位元組流,並保存在全局變數
Private Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
Dim n As Integer '定義變數及初始化
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n
intReceiveLen = intReceiveLen + intInputLenth
End Sub
'為輸出准備文本,保存在全局變數
'總行數保存在intLine
Public Sub GetDisplayText()
Dim n As Integer
Dim intValue As Integer
Dim intHighHex As Integer
Dim intLowHex As Integer
Dim strSingleChr As String * 1
Dim intAddress As Integer
Dim intAddressArray(8) As Integer
Dim intHighAddress As Integer
strAscii = "" '設置初值
strHex = ""
strAddress = ""
'獲得16進制碼和ASCII碼的字元串
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
If intValue < 32 Or intValue > 128 Then '處理非法字元
strSingleChr = Chr(46) '對於不能顯示的ASCII碼,
Else '用"."表示
strSingleChr = Chr(intValue)
End If
strAscii = strAscii + strSingleChr
intHighHex = intValue \ 16
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
strHex = strHex + Chr$(intHighHex) + Chr$(intLowHex) + " "
If (n Mod intHexWidth) = 0 Then
strAscii = strAscii + Chr$(13) + Chr$(10)
strHex = strHex + Chr$(13) + Chr$(10)
Else
End If
Next n
txtAsc = strAscii 'Ascii
txtHex = strHex '16進制
'獲得地址字元串
intLine = intReceiveLen \ intHexWidth
If (intReceiveLen - intHexWidth * intLine) > 0 Then
intLine = intLine + 1
End If
'設置換行
For n = 1 To intLine
intAddress = (n - 1) * intHexWidth
intHighAddress = 8
intAddressArray(0) = intAddress
For m = 1 To intHighAddress
intAddressArray(m) = intAddressArray(m - 1) \ 16
Next m
For m = 1 To intHighAddress
intAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16
Next m
For m = 1 To intHighAddress
If intAddressArray(intHighAddress - m) < 10 Then
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("0")
Else
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("A") - 10
End If
strAddress = strAddress + Chr$(intAddressArray(intHighAddress - m))
Next m
strAddress = strAddress + Chr$(13) + Chr$(10)
Next n
txtAdd = strAddress '地址
End Sub
Private Sub cmdReceive_Click()
If blnReceiveFlag Then
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
Me.cmdReceive.Caption = "開始接收"
Else
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.MSComm.InputLen = 0
Me.MSComm.InputMode = 0
Me.MSComm.InBufferCount = 0
Me.MSComm.RThreshold = 1
Me.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
Private Sub Form_Load()
intHexWidth = 8
txtAdd = ""
txtHex = ""
txtAsc = ""
txtSend = "11"
txtAdd.Width = 1335
txtHex.Width = 2535
txtAsc.Width = 1215
'設置默認發送接收關閉狀態
blnAutoSendFlag = False
blnReceiveFlag = False
'接收初始化
intReceiveLen = 0
'默認發送方式為16進制
'intOutMode = 1
'初始化串列口
intPort = 1
intTime = 1000
strSet = "9600,n,8,1"
Me.MSComm.InBufferSize = 1024
Me.MSComm.OutBufferSize = 512
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.MSComm.PortOpen = False
End Sub
Private Sub cmdClear_Click()
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
Call GetDisplayText
Call disPlay
End Sub
Private Sub MsComm_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case Me.MSComm.CommEvent
Case comEvReceive
If blnReceiveFlag Then
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
'此處添加處理接收的代碼
Me.MSComm.InputMode = comInputModeBinary '二進制接收
intInputLen = Me.MSComm.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm.Input
Call InputManage(bytInput, intInputLen)
Call GetDisplayText
'Call disPlay
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
End If
End Select
End Sub
Private Sub disPlay()
txtHex = ""
txtAsc = ""
txtAdd = ""
End Sub
⑸ 通過VB編程,如何實現計算機同8位單片機的串口通信
**********************VB代碼 PC**************************
Dim s As String
Dim sent() As Byte
Private Sub Form_Load()
With MSComm1
If .CommPort <> 2 Then .CommPort = 2 '設置Com2為通信埠
.Settings = "9600,N,8,1" '設置通信埠參數 9600赫茲、無校驗、8個數據位、1個停止位.(這里需要進一步說明的是:.Setting=」BBBB,P,D,S」。
'含義是:B:Baud Rate(波特率);P:Parity(奇偶);D:Data Bit;S:Stop Bit)
.InBufferSize = 1024 '設置緩沖區接收數據為1024位元組
.OutBufferSize = 512
.OutBufferCount = 0 '清空發送緩沖區
.InBufferCount = 0 '滑空接收緩沖區
If Not .PortOpen Then
.PortOpen = True '打開通信埠
End If
End With
End Sub
Private Sub Command1_Click()
s = Text1.Text
Call sendmsg
End Sub
Private Sub sendmsg()
Dim i As Integer
ReDim sent(Len(Text1.Text) + 1) As Byte
For i = 1 To Len(Text1.Text)
sent(i) = Asc(Mid(s, i, 1)) '發送ASC碼
Next i
sent(i) = Asc(vbCrLf) '發送換行表示數據結束
MSComm1.Output = sent
MSComm1.OutBufferCount = 0
End Sub
************************C代碼,單片機*****************************
#include <STDIO.H>
#include <AT89LV52.h>
void initial(void); //初始化
vvoid initial(void)
{
IP = 0X00; //定義串口高優先及
IE=0x00; //允許串口,定時器0
TCON=0X05;
TMOD=0x21; //定時器1為自動裝入方式
PCON=0x00; //速率bu翻倍
SCON=0X50; //串口工作方式:8位UART,數據傳輸速率可變
TH1=0XFD; //OXFF(22.184M)57600 baud
TL1=0XFD; //設置定時器
//PCON=0X80|PCON; //速率為9600 baud(晶振11.059M),SMOD=0數據傳輸速率4.8KB,th1=tl1=fd,smod=1,速率為19.2kb
TR1 = 1; //啟動定時器1
}
main()
{
int i=0,j,getbuf[5];
initial();
while(i<5)
{
getbuf[i]=_getkey(); //接受ASC碼
if (getbuf[i] == 13) break; //接收到換行表示數據結束
i++;
}
}
⑹ 利用VB實現串口接收單片機數據
InputModeBinary
=
1
'通過
Input
屬性以二進制方式檢取回數據
寫法有錯,正確寫法:
MSComm1.InputMode
=
comInputModeBinary
'以二進制方式接收
或:
MSComm1.InputMode
=
1
'以二進制方式接收
⑺ 單片機串口分別三次向VB上位機發送數組數據
第一,你要解決數據是否同步的問題。電腦的頻率比單片機的時鍾頻率快很多,所以串列通信選擇同步傳輸。當單片機發送完數組的第一個數據,上位機接收到這個數據後發送一個應答信號給單片機,然後單片機收到應答信號接著發送第二個數據給上位機,以此類推(應答信號的具體數值是你的通訊協議定義的)
第二,你的vb要添加MSComm控制項,參考文章
http://wenku..com/linkurl=gaBSjBHSDk9b_THl3f1XYPtpRZgpIt6wczgHWXe_h_1q8ICdfPRP_Q-Fq4JLsbT_r_D8gzTRs_-sa9956OHq0XD9ahbc6sa4d-_omQVAlXG
控制項MSComm的接收數據程序:
Private Sub MSComm1_OnComm()
Dim indata As Variant
Dim bte(0) As Byte
Select Case MSComm1.CommEvent
Case 2
indata = MSComm1.Input
bte(0) = AscB(indata)
If bte(0) = 數組的第一個數值
Then MSComm1.Output = 應答信號1
text1.text=bte(0)
end if
If bte(0) =數組的第二個數值 Then MSComm1.Output =應答信號2
text2.text=bte(0)
end if
If bte(0) =數組的第三個數值 Then MSComm1.Output =應答信號3
text3.text=bte(0)
end if
MSComm1.OutBufferCount = 0
End Select
End Sub
⑻ vb和單片機通信的問題。mscomm.RThreshold=1的疑問
當vb收到一個位元組了,vb產生OnComm事件,但是vb是windows 操作系統下的程序,執行到事件的mscomm1.Input 時已經過去一定時間,串口是有緩沖區的,這時候讀input已經傳過來很多位元組了。所以就發生了你所說的結果
⑼ 我用VB編的和單片機232通訊的程序,我如何判斷通訊超時呢,比如把通訊線拔了,如何讓它提示通訊斷開呢,
通訊超時:用輪詢法接收數據,當3秒內無接收到某地址的單片機的任何數據,即可判斷為通訊超時;超時後,把標簽置為通訊中斷標志就可以了。
PC只接收數據:
1 首先打開定時器1時間設為1ms,作為輪詢接收數據;
2 定時器1中斷是,接收串口數據,當接收到數據時,保存數據,同時關閉定時器2通訊超時3秒定時器,發送反饋信息給單片機(可選擇);
當沒接收到數據,打開定時器2;
3 當定時器2中斷時,認為相隔3秒未收到數據,判斷為通訊超時;
PC收發數據:
作為主機更簡單,主機發數據給單片機後,立刻打開定時器2;
如果在定時器2中斷前接收到單片機的反饋數據,關閉定時器2;
當定時器2中斷時,直接認為通訊中斷。
⑽ vb可以編輯單片機程序嗎
可以呀,只是編輯的話,就是寫程序。用vb的編輯器可以的,只是不好用。
單片機程序用任何編輯器都 可以寫的,包括windows自帶的記事本。
但編譯就不行了,要用keil 、wave等 。