Reading Serial Port with VB.NET 2005

Dear Experts,
I am trying to read data from serial port connected to barcode scanner and dispaly it to a text box.
Please help me with a simple code ?
Thanks in advance for your help.
djoni4343Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
rachitkohliConnect With a Mentor Commented:
I was about to write the delay for you, good you figured it out.
Have
Thread.Sleep (2)
you can change the interval, as should not affect the data received.
0
 
djoni4343Author Commented:
AUmidh,
Thanks but i am going to use the SerialPort Component instead.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AUmidhCommented:
Are you using .Net Serial Port Component ?
0
 
rachitkohliCommented:
'Drag a serial port control onto your form & write this function in your code window.
Public Function OpenSerialPort() As Boolean
        'Function to Prepare the serial port connection
        Try
            With SerialPort1
                If .IsOpen Then
                    .Close()
                End If

               
                .Parity = Parity.None
                .PortName = "COM1"
                .DataBits = 8
                .StopBits = StopBits.One

                .Encoding = System.Text.UnicodeEncoding.UTF8    'Setting the Encoding Type

                .Open()

                Return .IsOpen
            End With
        Catch ex As Exception
            MsgBox("Cannot Open Port ", MsgBoxStyle.Critical)
        End Try

    End Function

'To read data, you need to write code on its receive data event. Something like this..
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'This event will Receive the data from the selected COM port..
      Textbox1.text = SerialPort1.ReadExisting

End Sub
0
 
djoni4343Author Commented:
Yes, I am using .Net SerialPort Component.
0
 
djoni4343Author Commented:
rachitkohli,

I got error message : "Cross-thread operation not valid: Control 'txtBarcode' accessed from a thread other than the thread it was created on."
0
 
AUmidhCommented:
make txtBarcode.CheckforIllegalCrossThreadCalls=false
0
 
rachitkohliCommented:
in form load write
        Control.CheckForIllegalCrossThreadCalls = False     'Making Thread Safe
0
 
djoni4343Author Commented:
Aumidh,

I will try your second link, thanks.
0
 
djoni4343Author Commented:
rachitkohli,

I will try your suggestions, thanks.
I will be back in an hour, sorry.
0
 
djoni4343Author Commented:
rachitkohli,

It works but my barcode number displayed in parts (2 parts)  ?
0
 
djoni4343Author Commented:
Aumidh,

Your suggestion (second link) actually same with Rachitkohli sample but both gave same result ie: the barcode number displayed in two parts (8 digits then 5 digits)
0
 
rachitkohliCommented:
replace your data received event with the following code .


Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim bRead, nRead As Integer
        Dim returnStr As String = ""
        Dim ascStr As String = ""

        bRead = SerialPort1.BytesToRead 'Number of Bytes to read
        Dim cData(bRead - 1) As Byte

        SerialPort1.Encoding = Encoding.GetEncoding(65001)

        nRead = SerialPort1.Read(cData, 0, bRead)  'Reading the Data
        For Each b As Byte In cData
            ascStr += Chr(b)        'Ascii String
            returnStr += Hex(b)     'Hex String
        Next

      TextBox1.Text = returnStr
End Sub
0
 
djoni4343Author Commented:
rachitkohli,

Got error message "Name 'Encoding' is not declared" at "Encoding.GetEncoding(65001)" text
0
 
rachitkohliCommented:
try

System.Text.Encoding.GetEncoding(65001)
0
 
djoni4343Author Commented:
rachitkohli,

No Error but I received "39313730373036383036383533D" instead of "9170706806853" (my barcode number)
0
 
rachitkohliCommented:
I think you need to check out with your encoding info, this means the data is transmitted & received correctly, should check the encoding now..
0
 
rachitkohliCommented:
0
 
djoni4343Author Commented:
rachitkohli,

I think it's not the encoding
0
 
djoni4343Author Commented:
rachitkohli,

I think the SerialPort1_DataReceived event is triggered 2 times.
0
 
djoni4343Author Commented:
rachitkohli,

If I insert MsgBox("anything") at first line the event is triggered 1 time and my barcode displayed correctly, so I think I need to put 'the delay' code. Can you give that code ? Thanks.
0
 
djoni4343Author Commented:
rachitkohli,

The delay code is :
        System.Threading.Thread.Sleep(1000)          ' 1 second

It seems that my problem solved now, but thanks anyway.
0
 
rachitkohliCommented:
Good you find the solution,
FYI
Thread.Sleep have been worked if you have Imported System.Threading Namespace.

0
 
rachitkohliCommented:
Wasnt my answers helpful in any ways as am not getting any points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.