求VB.NET通过将textboxz中的数据转化为16进制数通过串口发送到单片机的程序

2024-11-28 01:40:25
推荐回答(2个)
回答1:

Private Sub hexSend()
On Error Resume Next
Dim outputLen As Integer
Dim outData As String
Dim SendArr() As Byte
Dim TemporarySave As String
Dim dataCount As Integer
Dim i As Integer

outData = UCase(Replace(txtSend.Text, Space(1), Space(0)))
outData = UCase(outData)
outputLen = Len(outData)

For i = 0 To outputLen-1
TemporarySave = Mid(outData, i + 1, 1)
If (Asc(TemporarySave) >= 48 And Asc(TemporarySave) <= 57) Or (Asc(TemporarySave) >= 65 And Asc(TemporarySave) <= 70) Then
dataCount = dataCount + 1
Else
Exit For
Exit Sub
End If
Next
If dataCount Mod 2 <> 0 Then
dataCount = dataCount - 1
End If
outData = Microsoft.VisualBasic.Left(outData, dataCount)
ReDim SendArr(dataCount / 2 - 1)
For i = 0 To dataCount / 2 - 1
SendArr(i) = Val("&h" + Mid(outData, i * 2+1, 2))
Next
SerialPort1.Write(SendArr, 0, 1)
End Sub

修改几处就好用了。

回答2:

需要帮你写吗