不是很明白你的題意
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設,市北企業(yè)網(wǎng)站建設,市北品牌網(wǎng)站建設,網(wǎng)站定制,市北網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,市北網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
strHex = strHex + [String].Format("{0:X2} "
這里的意思是把每個字節(jié)數(shù)據(jù)轉(zhuǎn)換成了十六進制,每個字節(jié)占兩個字符
如果你串口收到的4個字節(jié)數(shù)據(jù):43,27,56,200
那么你的結果是:2B1B38C8
即receivebytes.Text="2B1B38C8"
不足兩位的補0
如果你串口收到的4個字節(jié)數(shù)據(jù):3,27,56,200
那么你的結果是:031B38C8
即receivebytes.Text="031B38C8"
搜一下
有串口通信的例程
借鑒一下,再根據(jù)你的情況 編程
vb.net的話
For
Each
sp
As
String
In
My.Computer.Ports.SerialPortNames
cbxport.Items.Add(sp)
Next
vb6.0的話要調(diào)用API查看串口相關信息存在的注冊表。
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
通過讀注冊表的方法獲得串口數(shù)量,當然也可以獲得串口號了。
Option
Explicit
Private
Declare
Function
RegOpenKey
Lib
"advapi32.dll
"
Alias
"RegOpenKeyA
"
(ByVal
hKey
As
Long,
ByVal
lpSubKey
As
String,
phkResult
As
Long)
As
Long
Private
Declare
Function
RegQueryInfoKey
Lib
"advapi32.dll
"
Alias
"RegQueryInfoKeyA
"
(ByVal
hKey
As
Long,
ByVal
lpClass
As
String,
lpcbClass
As
Long,
ByVal
lpReserved
As
Long,
lpcSubKeys
As
Long,
lpcbMaxSubKeyLen
As
Long,
lpcbMaxClassLen
As
Long,
lpcValues
As
Long,
lpcbMaxValueNameLen
As
Long,
lpcbMaxValueLen
As
Long,
lpcbSecurityDescriptor
As
Long,
lpftLastWriteTime
As
Long)
As
Long
Private
Const
HKEY_LOCAL_MACHINE
=
H80000002
'
獲得當前系統(tǒng)的
COM
口的數(shù)量
Function
GetCOMCount()
As
Integer
Dim
ret
As
Long,
cntCOM
As
Long
RegOpenKey
HKEY_LOCAL_MACHINE,
"HARDWARE\DEVICEMAP\SERIALCOMM
",
ret
RegQueryInfoKey
ret,
"
",
0,
0,
0,
0,
0,
cntCOM,
0,
0,
0,
GetCOMCount
=
cntCOM
End
Function
Private
Sub
Command1_Click()
MsgBox
"您的機器有
"
GetCOMCount
"
個串口。
",
vbOKOnly,
"串口數(shù)量
"
End
Sub
首先:
textbox里沒有顯示,是因為SerialPort1和TextBox2不是同一線程創(chuàng)建的,需要跨線程操作。需要用到委托,這樣才能顯示出來。
其次:
我覺得用串口的接收數(shù)據(jù)事件更好一些。
下面代碼供參考:
'----------------------
'串口接收數(shù)據(jù)事件,其實比用定時器更好,
'觸發(fā)事件的條件可以自己在form_load中設置ReceivedBytesThreshold屬性數(shù)值,默認為ReceivedBytesThreshold=1
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim strRecvData As String = ""
strRecvData = SerialPort1.ReadExisting
Call disPlayComData(strRecvData)
End Sub
Delegate Sub callback(ByVal strT As String) '定義委托
Sub showString(ByVal comdata As String) '顯示結果
Me.TextBox1.Text = "結果:" comdata
End Sub
Sub disPlayComData(ByVal strTmp As String) '判定是否為跨線程
If Me.TextBox1.InvokeRequired Then
Dim d As New callback(AddressOf showString)
Me.Invoke(d, New Object() {strTmp})
Else
Me.TextBox1.Text = strTmp
End If
End Sub