導航:首頁 > 操作系統 > vb發數據給單片機

vb發數據給單片機

發布時間:2022-07-27 11:36:51

㈠ 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通信把PC中的數據發送給單片機

用MSCOMM控制項收發,發送前把發送類型改成2進制格式,發送字元也用byte格式,定義成數組格式,接收也使用這種格式,但是數組要設定成特殊格式。
用定時器定時調用也可,手工發送也可,主要是發送格式正確就行了,關鍵是發送前制定好通訊協議,確保數據全部能夠發送過去,最好有校驗。
有問題HI上找我。

㈢ VB可以直接發送字模數組到單片機嗎

當然可以。。MSCOMM控制項可以實現電腦與單片機之間串口通信,前題是你單片機的軟體要設計好
單片機可以接收數組存入片外存儲器。這些都是可以實現的,硬體來說沒問題,看你軟體怎麼設計了

㈣ VB和單片機進行串口通信 通過VB界面兩個簡單的按鈕 發送兩個數據給單片機

做一個整形到ASCII的轉換函數。如果你要發送的數據最大值可以確定,可以根據最大值定義一個ASCII數組,數組的每一個單元存放整形數據的一位。
發送前先對發送整形數組里的單元轉換成ASCII數組,然後再按照通用的發送函數進行發送。
void InttoChar (uint IntNumber)
//---------------------------------------------------------
// Name: void InttoChar (int IntNumber)
// Func.: Translate integer to ASCII charactor array
// Char.: IntNumber number to be translated to ASCII charactor
//---------------------------------------------------------
{
if (IntNumber < 10)
{
AsciiArray[0] = IntNumber + 0x30;
AsciiArray[1] = 0x20;
AsciiArray[2] = 0x20;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber < 100)
{
AsciiArray[0] = IntNumber / 10 + 0x30;
AsciiArray[1] = IntNumber % 10 + 0x30;
AsciiArray[2] = 0x20;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber < 1000)
{
AsciiArray[0] = IntNumber / 100 + 0x30;
AsciiArray[1] = IntNumber % 100 / 10 + 0x30;
AsciiArray[2] = IntNumber % 10 + 0x30;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber < 10000)
{
AsciiArray[0] = IntNumber / 1000 + 0x30;
AsciiArray[1] = IntNumber % 1000 / 100 + 0x30;
AsciiArray[2] = IntNumber % 100 / 10 + 0x30;
AsciiArray[3] = IntNumber % 10 + 0x30;
AsciiArray[4] = 0x20;
return;
}
else
{
AsciiArray[0] = IntNumber / 10000 + 0x30;
AsciiArray[1] = IntNumber % 10000 / 1000 + 0x30;
AsciiArray[2] = IntNumber % 1000 / 100 + 0x30;
AsciiArray[3] = IntNumber % 100 / 10 + 0x30;
AsciiArray[4] = IntNumber % 10 + 0x30;
return;
}
}

㈤ 如何用VB通過串口和51單片機通訊使得單片機能夠在IO口產生脈沖,脈沖的數量和頻率由VB設定

這個需要分步來完成
1、實現單片機串口通訊
2、編寫VB程序,添加串口控制項,實現VB與單片機串口通訊
3、寫單片機IO驅動,實現脈沖輸出
4、VB發數據給單片機,實現脈沖數量和頻率控制
5、更詳細的可以私信我完成

㈥ VB用Mscomm如何向單片機發送一個數據

Dim i As Integer Dim j As String i = 125 'j=125作為調試和講解用 j = Mid(Str(i), 2, Len(Str(i)) - 1) '將數據變數變成字元行變數,但轉換後成了"空格125",故對其進行處理 MSComm1.Output = j '將數據進行發送 MSComm1.InBufferCount = 0 '發送緩沖區清零 不知道這位問問好友為何要將數據以字元的形式發送

希望採納

㈦ VB中,點擊確定,把Text中的內容發送給單片機

你如果要把text文件中的內容發到單片機中,就必須進行文件操作,先打開text文件,再把打開的數據通過串口發送到單片機中,你最好要自己定義個軟體傳送同步協議,那樣就很好保證寫卡的正確率,你可以以數據包的形式發送,比如命令可以放第一位元組,接下來是整包數據長度。單片機接收厚寫卡就是單片機的問題了。同樣讀卡也可以以數據包形式把數據連同命令一起發送給電腦,並可以把卡中數據寫到text文件中。
希望對你有點幫助。

㈧ VB給單片機傳送數據問題!急!!

如果想傳遞的數值是0-255的,可以按位元組方式傳遞,但不可能同時,只能一前一後傳遞.
Private Sub Command1_Click()
Dim bytSj(1) As Byte
bytSj(0) = 50
bytSj(1) = 30
MSComm1.Output = bytSj
End Sub

Private Sub Form_Load()
MSComm1.PortOpen = True
End Sub

㈨ 請教一下關於VB與51單片機傳送數據的問題

VB程序 與 單片機程序,沒有問題,是波特率 誤差的問題
該 VB 程序 發送 范圍是 0 - 128 ,> 128 時發送 的 為 0 。

單片機 波特率 和 晶振 有關,與 PC 通訊一定要用 11.0592 MHz 的晶振
如果 用 6.000MHz 或者 12.000MHz 的 一般都 會有一定 的誤差
就是 這個 誤差 會 引起通訊 數據 失真。

閱讀全文

與vb發數據給單片機相關的資料

熱點內容
美國好的源碼出售 瀏覽:323
蘋果ipad文件夾怎麼添加文字 瀏覽:481
騰訊雲連接自己的伺服器地址 瀏覽:216
碩士英語綜合教程pdf 瀏覽:46
分段加密的安全性 瀏覽:507
咪咕直播為什麼沒有適配安卓系統 瀏覽:172
php模版大全 瀏覽:102
沒車能解壓嗎 瀏覽:634
php開發oa系統源碼 瀏覽:759
怎麼安裝蘋果ios的app 瀏覽:581
app拉新如何機刷 瀏覽:480
zendeclipseforphp 瀏覽:480
同時有幾個微信如何加密微信 瀏覽:86
大眾20t壓縮比 瀏覽:566
程序員要記住的500個單詞 瀏覽:830
wq快捷方式在哪個文件夾 瀏覽:965
雲南到河北源碼 瀏覽:92
安卓手機怎麼玩造夢3 瀏覽:60
多玩我的世界盒子怎麼創造伺服器地址 瀏覽:986
手機如何下載米家app 瀏覽:96