vb.net串行端口仅显示最后一个字符
我的问题是,当我第一次使用条形码扫描仪ds457读取条形码时,我会得到例如02405529的全文,但如果我再次读取代码,我只会得到最后一个字符9。如果我阅读了其他代码,例如02405530,我将在最后一个字符0处获取此代码。我需要关闭我的程序,重新阅读全文。我的程序中的DC按钮也将帮助作为清除列表框。有什么想法吗vb.net串行端口仅显示最后一个字符,.net,vb.net,serial-port,.net,Vb.net,Serial Port,我的问题是,当我第一次使用条形码扫描仪ds457读取条形码时,我会得到例如02405529的全文,但如果我再次读取代码,我只会得到最后一个字符9。如果我阅读了其他代码,例如02405530,我将在最后一个字符0处获取此代码。我需要关闭我的程序,重新阅读全文。我的程序中的DC按钮也将帮助作为清除列表框。有什么想法吗 Public Class frmMain Dim myPort As Array 'COM Ports detected on the system will be store
Public Class frmMain
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 100
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Handshake = IO.Ports.Handshake.XOnXOff
SerialPort1.RtsEnable = True
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
btnConnect.Enabled = False 'Disable Connect button
btnDisconnect.Enabled = True 'and Enable Disconnect button
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'Close our Serial Port
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtTransmit.Text & vbCr)
'The text contained in the txtText will be sent to the serial port as ascii
'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
'compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
' If [text] <> Me.rtbReceived.Text Then
Me.rtbReceived.Text = [text]
' Me.rtbReceived.Text &= [text]
'End If
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
rtbReceived.Text = " "
End Sub
Private Sub txtTransmit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTransmit.TextChanged
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub
Private Sub Isvalyti_Click(sender As Object, e As EventArgs) Handles Isvalyti.Click
rtbReceived.Clear()
End Sub
End Class
公共类frmMain
Dim myPort作为系统上检测到的阵列COM端口将存储在此处
添加了委托子SetTextCallback(ByVal[text]作为字符串),以防止在接收数据期间出现线程错误
私有子frmMain_Load(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
Timer1.Enabled=True
计时器1.间隔=100
'加载表单时,自动检测系统中的所有串行端口并填充cmbPort组合框。
myPort=IO.Ports.SerialPort.GetPortNames()'获取所有可用的com端口
CMBaud.Items.Add(9600)”将CMBaud组合框填充到使用的常用波特率
CMB波特率项目添加(19200)
CMB波特率项目添加(38400)
CMBAUD.项目添加(57600)
CMB波特率项目添加(115200)
对于i=0到uBond(myPort)
cmbPort.项目.添加(myPort(i))
下一个
cmbPort.Text=cmbPort.Items.Item(0)”将cmbPort文本设置为检测到的第一个COM端口
cmbaud.Text=cmbaud.Items.Item(0)”将cmbaud Text设置为列表中的第一个波特率
btnDisconnect.Enabled=False“初始断开按钮被禁用
端接头
私有子btnConnect_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnConnect。单击
SerialPort1.PortName=cmbPort.Text“在启动时将SerialPort1设置为选定的COM端口
SerialPort1.BaudRate=CMBaud.Text“将波特率设置为上的选定值”
'其他串行端口属性
SerialPort1.Parity=IO.Ports.Parity.None
SerialPort1.StopBits=IO.Ports.StopBits.One
SerialPort1.Handshake=IO.Ports.Handshake.XOnXOff
SerialPort1.RtsEnable=True
SerialPort1.DataBits=8'打开我们的串行端口
SerialPort1.Open()
btnConnect.Enabled=False“禁用连接按钮”
btnDisconnect.Enabled=True'并启用断开按钮
端接头
私有子btnDisconnect_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnDisconnect。Click
SerialPort1.Close()'关闭我们的串行端口
btnConnect.Enabled=True
btnDisconnect.Enabled=False
端接头
私有子BTN发送\单击(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理BTN发送。单击
SerialPort1.Write(txtTransmit.Text和vbCr)
'txtText中包含的文本将作为ascii码发送到串行端口
'加上回车键(回车键),如果另一端不需要回车键,则可以输入回车键
端接头
私有子SerialPort1_DataReceived(ByVal sender作为对象,ByVal e作为System.IO.Ports.SerialDataReceivedEventArgs)处理SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())在每次从serialPort接收数据时自动调用
端接头
私有子接收文本(ByVal[text]作为字符串)
'将创建线程的ID与调用线程的ID进行比较
如果需要Me.rtbReceived.invoker,那么
Dim x作为新的SetTextCallback(ReceivedText的地址)
调用(x,新对象(){(文本)})
其他的
'如果[text]Me.rtbReceived.text,则
Me.rtbReceived.Text=[Text]
'Me.rtbReceived.Text&=[Text]
"完"
如果结束
端接头
私有子cmbPort_SelectedIndexChanged(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理cmbPort.SelectedIndexChanged
如果SerialPort1.IsOpen=False,则
SerialPort1.PortName=cmbPort.Text“如果用户正在更改端口,则向其弹出一个消息框。”
没有先断开连接的情况下。
MsgBox(“仅在端口关闭时有效”,vbCritical)
如果结束
端接头
私有子CMBAUD_SelectedIndexChanged(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理CMBAUD.SelectedIndexChanged
如果SerialPort1.IsOpen=False,则
SerialPort1.BaudRate=cmbBaud.Text“如果用户正在更改波特率,则弹出一个消息框
没有先断开连接的情况下。
MsgBox(“仅在端口关闭时有效”,vbCritical)
如果结束
端接头
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnDisconnect。单击
rtbReceived.Text=“”
端接头
私有子txtTransmit_TextChanged(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理txtTransmit.TextChanged
端接头
私有子Timer1_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理Timer1.Tick
'SerialPort1.Write(txtTransmit.Text和vbCr)
端接头
私有子Isvalyti_Click(发送者作为对象,e作为事件参数)处理Isvalyti。单击
rtbReceived.Clear()
端接头
末级
串口速度非常慢,ReadExisting()调用通常只检索一个或两个字符。ReceivedText()方法不会追加文本,因此只能看到这几个字符
您需要将条形码扫描仪配置为发送指示字符串结尾的特殊字符。如果它还没有做到这一点,大多数都会做到。将SerialPort.NewLine属性设置为该特殊字符,现在可以改用SerialPort.ReadLine(),并确保获得整个扫描仪响应。谢谢Hans Passant和DMAN的帮助。这两个答案对我帮助很大。我还发现扫描仪和我的代码有一个bug。扫描器无法快速扫描到,所以我添加了threading.Thread.Sleep(100),现在扫描器获取全部代码,而不是一段代码
SerialPort
can b
ReceivedText(SerialPort1.ReadExisting())