應(yīng)該是你的轉(zhuǎn)換格式不正確,下面代碼就是將文本框中以一個(gè)空格隔開(kāi)的十六進(jìn)制轉(zhuǎn)為字節(jié)的代碼,文本框中數(shù)字格式為:01 02 03
專(zhuān)注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)徽州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
Dim TestArray() As String = Split(TextBox1.Text)
Dim hexBytes() As Byte
ReDim hexBytes(TestArray.Length - 1)
Dim i As Integer
For i = 0 To TestArray.Length - 1
hexBytes(i) = Val("h" TestArray(i))
Next
SerialPort.Write(hexBytes, 0, hexBytes.Length)
如果有問(wèn)題可以再聯(lián)系。
上面的代碼看不出問(wèn)題,如果初始化串口控件時(shí)已經(jīng)注冊(cè)了AxMSComm1_OnComm,有數(shù)據(jù)接收應(yīng)該能觸發(fā),至于為什么沒(méi)有觸發(fā),要看看你的接收方是否已經(jīng)收到你的發(fā)出指令,是否已經(jīng)有響應(yīng)數(shù)據(jù)回發(fā)。
建議分開(kāi)調(diào)試,用一個(gè)通用的串口助手,如格西烽火串口助手之類(lèi)的,分別調(diào)試你的上位機(jī)和下位機(jī)。格西烽火串口助手能輕松的定制如你上面的發(fā)送數(shù)據(jù)。
不知道你是不是用51單片機(jī)往串口發(fā)數(shù)據(jù),是的話(huà)那是因?yàn)?1的sbuf最多存放8個(gè)字節(jié),所以你發(fā)12個(gè)字節(jié)會(huì)分成兩次發(fā)完。還有在接收的時(shí)候最好定義好通信協(xié)議,比如加一個(gè)偵頭,一個(gè)偵尾,通過(guò)這兩個(gè)字節(jié)的內(nèi)容來(lái)接收數(shù)據(jù)。
老兄,提問(wèn)還是給點(diǎn)懸賞分嘛,不要吝嗇哪點(diǎn)分,知識(shí)是無(wú)價(jià)的,你給得越多,回答你的人才會(huì)越多。
你可以這樣操作:
Form1.BeginInvoke(Sub()
'一些操作
End Sub)
這樣主線(xiàn)程就會(huì)放下手里的事情并執(zhí)行Sub里的操作了
'字符表示的十六進(jìn)制數(shù)轉(zhuǎn)化為相應(yīng)的整數(shù),錯(cuò)誤則返回 -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 '出錯(cuò)信息
End If
ConvertHexChr = test
End Function
'字符串表示的十六進(jìn)制數(shù)據(jù)轉(zhuǎn)化為相應(yīng)的字節(jié)串,返回轉(zhuǎn)化后的字節(jié)數(shù)
Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六進(jìn)制(二進(jìn)制)數(shù)據(jù)字節(jié)對(duì)應(yīng)值
Dim hstr As String * 1 '高位字符
Dim lstr As String * 1 '低位字符
Dim HighHexData As Integer '高位數(shù)值
Dim LowHexData As Integer '低位數(shù)值
Dim HexDataLen As Integer '字節(jié)數(shù)
Dim StringLen As Integer '字符串長(zhǎng)度
Dim Account As Integer
Dim n As Integer
'計(jì)數(shù)
'txtSend = "" '設(shè)初值
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 '遇到非法字符中斷轉(zhuǎn)化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen 0 Then '修正最后一次循環(huán)改變的數(shù)值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,則不會(huì)進(jìn)入循環(huán)體
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
下面跟你介紹strHexToByteArray(strText As String, bytByte() As Byte)的功能。
假如text內(nèi)輸入"ff fe aa 14 af"(引號(hào)內(nèi)的包括空格)那么
strHexToByteArray=5
則byteByte()中 byteByte(0)=ff byteByte(1)=fe byteByte(2)=aa
byteByte(3)=14 byteByte(4)=af
注意:假如輸入你想輸入1,則必須寫(xiě)01
我想有了這個(gè)函數(shù)會(huì)給你很大的幫助。