【发布时间】:2017-09-20 09:47:57
【问题描述】:
我想从连接在我的一个 COM 端口上的 UHF RFID 阅读器读取数据,数据应该是十六进制的,并且必须是一个完整的十六进制,应该粘贴在我制作的 windows 窗体应用程序的文本框中在 VB.NET 中。
请帮助我,我是 VB.NET 编程的新手。我需要一个 vb.net 代码来完成这项任务:
我的代码:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each s In System.IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.Add(s)
Next s
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("Please select a port")
Exit Sub
Else
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = ComboBox1.SelectedItem.ToString
SerialPort1.Open()
End If
End Sub
Private Shared buffer As String = ""
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
Dim rcv As String = _SerialPort1.ReadExisting()
buffer = String.Concat(buffer, rcv)
Dim hexVal As Integer
hexVal = Convert.ToInt32(rcv, 16) '16 specifies the base
txtReceived.Text = hexVal
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SerialPort1.Close()
End Sub
结束类
【问题讨论】:
-
很好的作业。不幸的是,我们不是编码服务。向我们展示您迄今为止的尝试。
-
我说得对吗,您想将缓冲区转换为 ASCII 十六进制代码吗?
-
我想接收十六进制而不是 ascii 但我的代码无法正常工作可能是我的代码有错误
-
然后,您将不得不遍历字符串(提示:
For Each c As Char in buffer,然后将每个字符转换为 ascii 代码并将 ascii 代码转换为十六进制。我认为这很简单。 -
基本上,我想读取 RFID 卡的 HEX 并将此 HEX 粘贴到文本框中。仅此而已......虽然它看起来很简单,但我无法在 VB.NET 中做到这一点