VB接收和发送大于7E的16进制数

ywk41 2008-01-16 03:17:41
我刚从事软件,现写一个串口测试程序,现在能发送和接收十六制数,但发送80以上的数,接收到为00,我接收和发送程序如下:
请高手指点.
Private Sub Command1_Click()
For byteloop = 1 To Len(Text1.Text) Step 2
strsend = Mid$(Text1.Text, byteloop, 2)
MSComm1.Output = Chr$("&h" + strsend)

Next
End Sub

Private Sub Mscomm1_Oncomm()

'...通讯事件发生

Dim indata As Variant

Dim bte(0) As Byte

Dim intInputLen As Integer

Dim i As Integer

Select Case MSComm1.CommEvent

Case comEvReceive '...有接受事件发生

bytInput = MSComm1.Input
For i = LBound(bytInput) To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i)) & Chr(32)
Else
strData = strData & Hex(bytInput(i)) & Chr(32)
End If
Next
Text2 = strData

End Select
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0

End Sub

...全文
233 点赞 收藏 23
写回复
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
guyehanxinlei 2008-01-18
学习
回复
zdingyun 2008-01-17
又出来了!哈哈!
回复
zdingyun 2008-01-17
12:40回贴不见了??
回复
zdingyun 2008-01-17
To ywk41:
你这样设置没解决发送大于&H80(Byte值>128)数据的正确发送。
Option Explicit
Dim strData As String
Private Sub Command1_Click() '16进制发送,不出现发送80以上的数,接收到为00的错误
Dim sj() As Byte
Dim sj_Txt As String
Dim i As Integer
sj_Txt = Text1
ReDim sj(Len(sj_Txt) / 2 - 1)
For i = 0 To Len(sj_Txt) - 1 Step 2
sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
Next
If MSComm1.PortOpen = True Then
MSComm1.Output = sj
Else
MSComm1.PortOpen = True
MSComm1.Output = sj
End If
End Sub
'执行Command2_Click代码,出现80以上的数,接收到为00错误
Private Sub Command2_Click()
Dim byteloop As Integer
Dim strSend As String
For byteloop = 1 To Len(Text1.Text) Step 2
strSend = Mid$(Text1.Text, byteloop, 2)
MSComm1.Output = Chr$("&h" + strSend)
Next
End Sub

Private Sub Form_Load()
Text1 = "808182838400123456"
MSComm1.RThreshold = 1
MSComm1.NullDiscard = False
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
End Sub

Private Sub Mscomm1_Oncomm()
'通讯事件发生
Dim indata As Variant
Dim bytInput() As Byte
Dim intInputLen As Integer
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
bytInput = MSComm1.Input
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i)) '& Chr(32)
Else
strData = strData & Hex(bytInput(i)) '& Chr(32)
End If
Next
Text2 = strData
End Select
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
End Sub
回复
ywk41 2008-01-17
(1)设置MSComm控件的属性NullDiscard=False;

(2)使用二进制接收,即用MSComm1.InputMode=comInputModeBinary便可以解决问题;

按这个设置的,
回复
ywk41 2008-01-17
那怎样接收00字!还没搞定!
回复
zdingyun 2008-01-17
To ywk41:
你发表于:2008-01-17 16:05:37的贴子中的代码存在错误,不能接收.
1. bte(0)是何变量名,接收代码中用的是bytInput()BYTE动态数组
2. MSComm1属性RThreshold必需>0,方能产生Oncomm事件
3. 必需注意程序中变量的准确定义
代码修改如下:

Option Explicit
Dim sj() As Byte
Dim strData As String
Private Sub Command1_Click() '发送数据
Dim buffer As Variant
Dim strsend As String
Dim i As Integer
ReDim sj(Len(Text1.Text) / 2 - 1)
For i = 0 To Len(Text1.Text) - 1 Step 2
sj(i / 2) = Val("&H" & Mid(Text1.Text, i + 1, 2))
Next
MSComm1.Output = sj
strData = ""
End Sub

Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Text1 = "001234567890FF00"
End Sub

Private Sub Mscomm1_Oncomm()
'通讯事件发生
Dim indata As Variant
Dim bytInput() As Byte
Dim intInputLen As Integer
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
'此处添加处理接收的代码
MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = MSComm1.Input
'jieshou
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
Text2.Text = strData
End Select
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
End Sub
回复
xaobao 2008-01-17
oh yeah~
回复
ywk41 2008-01-17
这个我已改过了,
是发送和接收十六进数.发00H接收不到!
代码如下:
Private Sub Command1_Click() '发送数据
Dim buffer As Variant
Dim strsend As String
Dim sj() As Byte
Dim i As Integer
ReDim sj(Len(Text1.Text) / 2 - 1)
For i = 0 To Len(Text1.Text) - 1 Step 2
sj(i / 2) = Val("&H" & Mid(Text1.Text, i + 1, 2))
Next
MSComm1.Output = sj

strData = ""

End Sub


Private Sub Mscomm1_Oncomm()

'...通讯事件发生

Dim indata As Variant

Dim bte(0) As Byte

Dim intInputLen As Integer

Dim i As Integer


Select Case MSComm1.CommEvent

Case comEvReceive '...有接受事件发生

'此处添加处理接收的代码
Me.MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm1.Input
'jieshou
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i)) '& " " '建议 & " " 不要用
Else
strData = strData & Hex(bytInput(i)) '& " " '建议 & " " 不要用
End If
Next
Text2.Text = strData


End Select
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0

End Sub
回复
zdingyun 2008-01-17
TO ywk41:
你发表于:2008-01-16 15:17:41的原贴是:
我刚从事软件,现写一个串口测试程序,现在能发送和接收十六制数,但发送80以上的数,接收到为00,我接收和发送程序如下:
请高手指点.
你发表于:2008-01-17 13:50:55的贴子:
谢谢zdingyun,我可以发送80以上的数,但我不能接收00H,
我被你搞糊涂了!!!

一般MSComm控件的属性NullDiscard缺省设置就为False,除非你刻意设置为True
但你发表于:2008-01-16 15:17:41的原贴中代码:

Private Sub Command1_Click()
For byteloop = 1 To Len(Text1.Text) Step 2
strsend = Mid$(Text1.Text, byteloop, 2)
MSComm1.Output = Chr$("&h" + strsend)
Next
End Sub

是80以上的数,会接收到为00错误
回复
ywk41 2008-01-17
谢谢zdingyun,我可以发送80以上的数,但我不能接收00H,
回复
ywk41 2008-01-16

可以发,我的接收有问题
回复
ywk41 2008-01-16
但不能传送00?
回复
ywk41 2008-01-16
任道甚远,还有好多要做,
回复
....................
回复
ywk41 2008-01-16
ok了,谢谢zdingyun 和alifriend
回复
zdingyun 2008-01-16
Private Sub cmdSendHex_Click() '16进制发送
Dim sj() As Byte
Dim sj_Txt As String
Dim i As Integer
TxtSend = "800A00113135323634389794"
sj_Txt = TxtSend
ReDim sj(Len(sj_Txt) / 2 - 1)
For i = 0 To Len(sj_Txt) - 1 Step 2
sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
Next
MSComm1.Output = sj
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1 'COM端口
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
TxtSend = ""
End Sub
回复
ywk41 2008-01-16
明白了,谢谢,那怎样改?我是初手!
回复
zdingyun 2008-01-16
Private Sub cmdSendHex_Click() '16进制发送
Dim sj() As Byte
Dim sj_Txt As String
Dim i As Integer
TxtSend = "800A00113135323634389794"
sj_Txt = TxtSend
ReDim sj(Len(sj_Txt) / 2 - 1)
For i = 0 To Len(sj_Txt) - 1 Step 2
sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
Next
If MSComm1.PortOpen = True Then
MSComm1.Output = sj
Else
MSComm1.PortOpen = True
Shape1.FillColor = vbGreen
Label5.Caption = "打开"
MSComm1.Output = sj
End If
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1 'COM端口
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True
TxtSend = ""
End Sub
回复
波导终结者 2008-01-16
MSComm1.Output = Chr$("&h" + strsend)
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告