信雪神话
2015-05-04 03:27
采纳率: 0%
浏览 5.1k

Vb将文本框里输入的十进制数据按chr(13)分割,然后转换成&h+16进制字符串形式

例如文本框里输入253chr(13)255chr(13),要求输出结果书“&HFD” "&HFF",看了好多算法,但是还是没有研究出来.

 我自己做的结果如下

Private Sub Text2_KeyPress(KeyAscii As Integer)

Dim tmp() As String
Dim aa As String * 1
Dim rd As String
Dim i As Integer
Dim reload As Integer
Dim response

If pc_draw_flag = False Then
MsgBox "请先选择路径规划功能,再进行路径规划!", vbOKOnly + vbInformation, "提示信息!"
End If
aa = Chr$(KeyAscii)

If aa = Chr(13) And pc_draw_flag = True Then
tmp = Split(Text2.Text, Chr(13)) '把目标数组按分隔符chr(13)分割
For i = 0 To UBound(tmp) 'UBound()取数组内元素的个数

rd = getnum(CStr(tmp(i)))



Next i

route(route_num) = "&H" & Hex(Val(rd))


route_num = route_num + 1
Text8.Text = route_num
If route_num = 20 Then
    response = MsgBox("路径存储已达上限!是否重新规划?", vbYesNo + vbDefaultButton2 + vbInformation, "提示信息!")
    If response = vbYes Then
        route_num = 0
    Else
        MsgBox "路径存储已完成,请点击发送按钮!", vbOKOnly + vbInformation, "提示信息!"
    End If
End If

' MSComm1.Output = test

End If

End Sub
发送的
Private Sub CmdSend_Click()

Dim bt(0 To 4) As Byte
bt(0) = "&H" & "10"
bt(1) = "&H" & "11"
bt(2) = "&H" & "12"
bt(3) = "&H" & "13"
bt(4) = "&H" & "F6"

If MSComm1.PortOpen = True Then

If pc_draw_flag = True And OptSendHex.Value = True Then

' For i = 0 To 19
' MSComm1.Output = "&H" & StrConv(Hex(route(i)), vbUpperCase)
' MSComm1.Output = bt
MSComm1.Output = route(0)
send_num = send_num + 1
Text7.Text = send_num
' Next i

End If


If Text2.Text = "" Or pc_draw_flag = False Then
    MsgBox "发送数据不能为空,请进行路径规划或输入字符!", vbOKOnly + vbInformation, "提示信息!"
Else
    If OptSendHex.Value = True Then
        MSComm1.InputMode = comInputModeBinary
        MSComm1.InputLen = 0
    Else
        MSComm1.InputMode = comInputModeText
        MSComm1.InputLen = 1
    End If

' MSComm1.Output = Trim(Val(Text2.Text))
End If

Else
MsgBox "串口没有打开,请打开串口!", vbOKOnly + vbInformation, "提示信息!"
End If

route_num = 0
If pc_draw_flag = True And MSComm1.PortOpen = True And Text2.Text <> "" Then

For i = 0 To 19
Text2.Text = Text2.Text & route(i) & " "
If i = 19 Then
Text2.Text = Text2.Text & Chr(13) & Chr(10)
End If
Next i

End If

End Sub
诸如此类形式的
bt(4) = "&H" & "F6"可以发送成功,检测也是好的,但是为什么我的生成的字符串
如输入255,显示的字符串&HFF,但是发送过去后不对,是把那个转换成了& H F F四个字符的ascii吗。。这是什么鬼。。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • Tiger_Zhao 2015-05-04 09:22

    你脑子里想的什么鬼?
    bt(4) = "&H" & "F6 已经隐式地把字符串 &HF6 转化为一个字节,所以发送的内容是1个字节。
    而 MSComm1.Output = "&HFF" 直接发的是字符串啊,当然是变成4个字节了。

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题