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

2024年11月28日 01:40
有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):

需要帮你写吗