Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading Serial Port with VB.NET 2005

Posted on 2008-06-17
28
Medium Priority
?
2,818 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:djoni4343
  • 13
  • 9
  • 4
28 Comments
 
LVL 7

Expert Comment

by:AUmidh
ID: 21810034
0
 

Author Comment

by:djoni4343
ID: 21810089
AUmidh,
Thanks but i am going to use the SerialPort Component instead.
0
 
LVL 7

Expert Comment

by:AUmidh
ID: 21810097
Are you using .Net Serial Port Component ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:rachitkohli
ID: 21810112
'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
 

Author Comment

by:djoni4343
ID: 21810113
Yes, I am using .Net SerialPort Component.
0
 

Author Comment

by:djoni4343
ID: 21810141
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
 
LVL 7

Expert Comment

by:AUmidh
ID: 21810150
make txtBarcode.CheckforIllegalCrossThreadCalls=false
0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21810155
in form load write
        Control.CheckForIllegalCrossThreadCalls = False     'Making Thread Safe
0
 

Author Comment

by:djoni4343
ID: 21810157
Aumidh,

I will try your second link, thanks.
0
 

Author Comment

by:djoni4343
ID: 21810175
rachitkohli,

I will try your suggestions, thanks.
I will be back in an hour, sorry.
0
 

Author Comment

by:djoni4343
ID: 21810487
rachitkohli,

It works but my barcode number displayed in parts (2 parts)  ?
0
 

Author Comment

by:djoni4343
ID: 21810772
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
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21811187
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
 

Author Comment

by:djoni4343
ID: 21829004
rachitkohli,

Got error message "Name 'Encoding' is not declared" at "Encoding.GetEncoding(65001)" text
0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21829045
try

System.Text.Encoding.GetEncoding(65001)
0
 

Author Comment

by:djoni4343
ID: 21829135
rachitkohli,

No Error but I received "39313730373036383036383533D" instead of "9170706806853" (my barcode number)
0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21829262
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
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21829269
0
 

Author Comment

by:djoni4343
ID: 21829519
rachitkohli,

I think it's not the encoding
0
 

Author Comment

by:djoni4343
ID: 21829539
rachitkohli,

I think the SerialPort1_DataReceived event is triggered 2 times.
0
 

Author Comment

by:djoni4343
ID: 21829575
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
 
LVL 14

Accepted Solution

by:
rachitkohli earned 2000 total points
ID: 21829584
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
 

Author Comment

by:djoni4343
ID: 21829595
rachitkohli,

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

It seems that my problem solved now, but thanks anyway.
0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21829720
Good you find the solution,
FYI
Thread.Sleep have been worked if you have Imported System.Threading Namespace.

0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21881918
Wasnt my answers helpful in any ways as am not getting any points.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question