none
想请问各位为什么我的textbox read不到我program所scan的barcode我是否哪里有出错请各位大大指点 RRS feed

  • 问题

  • 我正在篇写一个2d barcode scanner 的program 我遇到了当我barcode scanner scan barcode 时我的text box无法read到我scanner所scan 的data。可以请各位高手帮我看看吗?以下是我所篇写的barcode scanner 的program

    Public Class Form1
        Private DispString As String
        Dim serialPort As New SerialPort
        Dim returnStr As String = ""

      Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

            If TextBox1.Text.Length >= 20 Then
                serialPort.Close()
            Else
                DispString = serialPort.ReadExisting()
            End If
        End Sub

      Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            serialPort.PortName = "COM13"
            serialPort.BaudRate = "9600"
            serialPort.Parity = Parity.None
            serialPort.StopBits = StopBits.One
            serialPort.Handshake = Handshake.None
            serialPort.Open()
            serialPort.ReadTimeout = 200

            If serialPort.IsOpen Then
                DispString = ""
                txtCardKeyDeactivate.Text = ""
            End If

        End Sub

        Private Sub SendSerialData(ByVal data As String)
            Using COM13 As IO.Ports.SerialPort =
                    My.Computer.Ports.OpenSerialPort("COM13")
                COM13.WriteLine(data)
            End Using
        End Sub

        Private Sub ReceiveSerialData(ByVal data As String)

            Dim returnStr As String = ""
            Dim COM13 As IO.Ports.SerialPort = Nothing
            Try
                COM13 = My.Computer.Ports.OpenSerialPort("COM13")
                COM13.ReadTimeout = 10000
                Do
                    Dim Incoming As String = COM13.ReadLine()
                    If Incoming Is Nothing Then
                        Exit Do
                    Else
                        returnStr &= Incoming & vbCrLf
                    End If
                Loop
            Catch ex As TimeoutException
                returnStr = "Error: Serial Port read timed out."
            Finally
                If COM13 IsNot Nothing Then COM13.Close()
            End Try
        End Sub

    2019年11月13日 8:51

全部回复

  • I am writing a barcode scanner program i face the issue is when user the 2d barcode scanner Scan the barcode. my textbox can not read the data. can someone help me take a look. Thanks.

    below is my program

    Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
            If TextBox1.Text.Length >= 20 Then
                serialPort.Close()
            Else
                DispString = serialPort.ReadExisting()
            End If
        End Sub

    Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            serialPort.PortName = "COM13"
            serialPort.BaudRate = "9600"
            serialPort.Parity = Parity.None
            serialPort.StopBits = StopBits.One
            serialPort.Handshake = Handshake.None
            serialPort.Open()
            serialPort.ReadTimeout = 200

            If serialPort.IsOpen Then
                DispString = ""
                txtCardKeyDeactivate.Text = ""
            End If

        End Sub

    Private Sub SendSerialData(ByVal data As String)
            Using COM13 As IO.Ports.SerialPort =
                    My.Computer.Ports.OpenSerialPort("COM13")
                COM13.WriteLine(data)
            End Using

        End Sub

        Private Sub ReceiveSerialData(ByVal data As String)

            Dim returnStr As String = ""
            Dim COM13 As IO.Ports.SerialPort = Nothing
            Try
                COM13 = My.Computer.Ports.OpenSerialPort("COM13")
                COM13.ReadTimeout = 10000
                Do
                    Dim Incoming As String = COM13.ReadLine()
                    If Incoming Is Nothing Then
                        Exit Do
                    Else
                        returnStr &= Incoming & vbCrLf
                    End If
                Loop
            Catch ex As TimeoutException
                returnStr = "Error: Serial Port read timed out."
            Finally
                If COM13 IsNot Nothing Then COM13.Close()
            End Try
        End Sub
    End Class


    2019年11月13日 9:41
  • Hi christing,

    我看了你的代码,但是没有找到有关将“data”赋值给textBox的代码。你能详细说明一下吗?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月14日 1:48
    版主
  • @kyle 我有篇写这个code在我的textbox1 但不work所以没有post

     Private Sub DisplayText(ByVal sender As Object, ByVal e As EventArgs)
            TextBox1.AppendText(DispString)
            If serialPort.IsOpen Then serialPort.Close()
        End Sub

    我有更改过我的data receive 的code 

       Private Sub scanner_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles scanner.DataReceived
            If TextBox1.Text.Length >= 20 Then
                serialPort.Close()
            Else
                DispString = serialPort.ReadExisting()
            End If
        End Sub



    2019年11月14日 2:46
  • @kyle 我有篇写这个code在我的textbox1 但不work所以没有post

     Private Sub DisplayText(ByVal sender As Object, ByVal e As EventArgs)
            TextBox1.AppendText(DispString)
            If serialPort.IsOpen Then serialPort.Close()
        End Sub

    我有更改过我的data receive 的code 

       Private Sub scanner_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles scanner.DataReceived
            If TextBox1.Text.Length >= 20 Then
                serialPort.Close()
            Else
                DispString = serialPort.ReadExisting()
            End If
        End Sub



    你有通过添加断点的方式来查看“DispString”中的值是否为空吗?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月14日 5:07
    版主
  • @Kyle

    关于DispString 我有在scanner_DataReceived中setting当我的scanner read barcode 时可以将read到的data paste 在text box1 里。

    DispString = serialPort.ReadExisting()

    以我的理解我的textbox 1 是tools 我没有在我的text box1 setting hard code 所以它是empty data 的。

    我将我的serial port 的tools命名为scanner。以下是我设置的private 与dim

    Private DispString As String
    Dim serialPort As New SerialPort
    Dim returnStr As String = ""


    2019年11月14日 5:36
  • @kyle 我有另外在写过 handle scan in的code但是我还是失败在insert data 进textbox

     Private Sub HandleScannerIn(ByVal Code As String)

            Code = Code.Replace(vbCr, "")

            Dim a As Array
            Dim barcode As String = a(0)

            TextBox1.Text = barcode
            TextBox1.Text = barcode.Length

            strbarcode = barcode
            strbarcode = Replace(strbarcode, vbCrLf, "")
            strbarcode = Replace(strbarcode, vbCr, "")
        End Sub
    End Class

    2019年11月14日 7:57
  • @kyle 我有另外在写过 handle scan in的code但是我还是失败在insert data 进textbox

     Private Sub HandleScannerIn(ByVal Code As String)

            Code = Code.Replace(vbCr, "")

            Dim a As Array
            Dim barcode As String = a(0)

            TextBox1.Text = barcode
            TextBox1.Text = barcode.Length

            strbarcode = barcode
            strbarcode = Replace(strbarcode, vbCrLf, "")
            strbarcode = Replace(strbarcode, vbCr, "")
        End Sub
    End Class

    在你的代码中,barcode好像始终为空值。此外,形参Code的作用是什么?a是数组吗?也没有实例化。

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月14日 8:16
    版主
  • @kyle 我所设置的textbox 是空值。 barcode 是当我scanner scan barcode时将读到的data paste 在 textbox 里面。

    请问形参Code是什么意思。

    我想写的program是当scanner scan barcode 时我所scan到的barcode 可以paste在指定的textbox 里。


    2019年11月14日 8:23
  • @kyle 我所设置的textbox 是空值。 barcode 是当我scanner scan barcode时将读到的data paste 在 textbox 里面。

    请问形参Code是什么意思。


    你在这个方法之外又重新定义了a和barcode吗?你现在提供的代码中的a和barcode都是局部变量,如果你在其他地方也定义了一个a或barcode,并不会影响到HandleScannerIn代码中的a和barcode。形参指的就是方法HandleScanner参数里的Case,你定义了它,但没有具体使用到它。

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月14日 8:31
    版主
  • 我将a 设定为array. barcode 设定为 我scanner所读到的barcode 会设定这个是因为我要我的barcode scanner 以 array方法读取我所读到的资料。可以请教kyle 指点我能怎么改进吗?我开始是只使用usb scanner来读取资料,但发现,使用u art serial port 的scanner在我的program上完全无法读取barcode 的资料进text box所以才想要用source code篇写




    2019年11月14日 8:38
  • 如果你想要同步所有的a或barcode的话,你可以想下面这样声明对象。

        Dim b As String
    
        Sub Method1()
            b = "123"
        End Sub
        Sub Method2()
            b = "456"
        End Sub

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月14日 8:56
    版主
  • @kyle 我想问我可以怎么篇写我的code 让我的barcode scanner 可以read到barcode 后paste 在我所指定的text box呢
    2019年11月14日 9:14
  • 获取接收到的数据,就是使用你代码中的DataReceived事件。你可以参考文档SerialPort.DataReceived 事件

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年11月14日 9:51
    版主
  • @kyle 我想请教当我的serial port receive barcode data.我可以怎么写我textbox 所read到的barcode scanner 的data.请指点谢谢。
    2019年11月15日 0:19