?
Solved

Serialport Communication moRS232 just reading and placing that value in textbox - It is opening Port and failing  in Read method

Posted on 2006-05-25
16
Medium Priority
?
1,007 Views
Last Modified: 2012-08-14
Hi,
I need to read the value from barcode scanner and put that in my form textbox textSerialNumber.
I have downloaded moRS232 from net and added that class in my project. I am able to open a port, but is faling when it try to read.
here is the code I used In form load I opend the port and Button is  clicked I read the value and putting in SerialNumber Text box.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim moRS232 = New Rs232
        Try
            With moRS232
                .Port = 5 '// Uses COM1
                .BaudRate = 2400 '// 2400 baud rate
                .DataBit = 8 '// 8 data bits
                .StopBit = Rs232.DataStopBit.StopBit_1
                .Parity = Rs232.DataParity.Parity_None
                .Timeout = 1500 '// 500 ms of timeout admitted to get all required bytes
            End With
            '// Initializes and Open
            moRS232.Open()
            '// Set state of RTS / DTS
            moRS232.Dtr = True
            moRS232.Rts = True
        Catch Ex As Exception
            MessageBox.Show(Ex.Message, "Connection Error", MessageBoxButtons.OK)
            'Finally
           
        End Try
        MsgBox("Port Opened")
 
    End Sub

 Private Sub bwrite_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bwrite.Click
        Try
            'moRS232.Read(10)
            moRS232.Read(512)
            TxtSerialNumber.Text = moRS232.InputStreamString.ToString()
            TxtSerialNumber.ForeColor = Color.Black
            TxtSerialNumber.BackColor = Color.White

        Catch Ex As Exception
            TxtSerialNumber.BackColor = Color.Red
            TxtSerialNumber.ForeColor = Color.White
        End Try

    End Sub

Please help where I am doing wrong.

Thanks
Manasa
0
Comment
Question by:manasa355
  • 10
  • 6
16 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761641
1) .NET version?

2) Download location?

Bob
0
 

Author Comment

by:manasa355
ID: 16762444
1)Microsoft Development Environment 2003           Version 7.1.3088
Microsft DotNet Environment Framework 1.1        Version 1.1.4322SP1

0
 

Author Comment

by:manasa355
ID: 16762445
0
Independent Software Vendors: 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 96

Expert Comment

by:Bob Learned
ID: 16763797
1) Can you use HyperTerminal, and connect to the bar code scanner?

2) What is the scanner model, version, manufacturer, etc.?

Bob
0
 

Author Comment

by:manasa355
ID: 16763988
Yes Bob,
I used Hyperterminal and I can see the Barcode. connecting Comport 5 - Barcode looks like AA441500-157


2)DENSO GT10Q-SB
MANUFACTURED:APr.,2005ET

2D Code Handy Scanner

Can you please take a look let me know as soon as possible, if you know anything

Thanks for all your help
0
 

Author Comment

by:manasa355
ID: 16764608
Bob,
Is there any other program, I can do this requirement. All I need is read the data feom serail port(Bar code scanner) and place it in the txtbox serial Number.

Please help....
Thanks
Manasa
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16765554
Free, or do you want to pay?  

Good choice if you want to pay:
http://www.sax.net/

I use VB.NET 2005, which has the System.IO.SerialPort class, so I don't know of any good free resources for serial port stuff that I could recommend.

Bob
0
 

Author Comment

by:manasa355
ID: 16768489
Bob,
I have VB.net 2005 installed on my computer. Can you please guide me through how I can do this.
Please urgent....
Thanks
Manasa
0
 

Author Comment

by:manasa355
ID: 16768812
Bob,
I tried like this VB.NET 2005  ......noting happens. sRead valueis nothing when I debug.


  Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SerialPortConn As System.IO.Ports.SerialPort = New System.IO.Ports.SerialPort("COM5", 38400, IO.Ports.Parity.None, 8, 1)
            SerialPortConn.Open()

            'SerialPortConn.ReadChar()
            Dim sRead As String = SerialPortConn.ReadChar()
        Catch ex As Exception

        End Try
 

I appreciate if you can help me what Read parameter I should work of. I took as ReadChar() because My barcode sample looks like this AA443200-803*

Thanks
Manasa
    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16772260
Imports System.IO.Ports

...

Private Withevents SerialPortConn As SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SerialPortConn = New System.IO.Ports.SerialPort("COM5", 38400, IO.Ports.Parity.None, 8, 1)

    SerialPortConn.Handshake = Handshake.None
    SerialPortConn.DtrEnable = True
    SerialPortConn.RtsEnabled = True
    SerialPortConn.Open()

End Sub

Private Sub OnDataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPortConn.DataReceived
   Dim data As String = SerialPortConn.ReadExisting()
End Sub

Bob

0
 

Author Comment

by:manasa355
ID: 16772442
Bob,
I tried with your code. I do not get any data from bar code scanner. When i debug the value for data is nothing.
and I tried with palcing acommand button even then it is not working.I got from hyperterminal

Please let meknow
weather it worked from your end
Thanks
Manasa
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16777772
1) Just because code works for me doesn't guarantee that it will work for you.  There are too many variables, like flow control, COM settings, DTR signalling, etc. that are different.

2) I am not using a bar scanner, but reading from a scale, which is a different proposition

3) It is very important that you get the communications settings correct in order to get this to work.

4) I noticed from your initial code above that you had a baud rate of 2400 and COM1, but the 2005 code has 38400 and COM5, so which is it?

Bob
0
 

Author Comment

by:manasa355
ID: 16790594
Bob,
I used Rs232 and it worked fine. I think I solved the problem of connecting to port. Now What's happenning is when I display Serial Number in Textbox I am displaying like this
#1301~~AA44320008030~~~~~

Can we replace those Ascii char with empty. I want to display like this #1301 AA44320008030

Can you please help me with this

Thanks
aMnasa





0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16790684
If the characaters are always in the same position, then you can do something like this:

output = output.Substring(7, 13)

Or, you could do a replace:

output = output.Replace("~", "")


Bob
0
 

Author Comment

by:manasa355
ID: 16807337
Bob,
Serial Number is displaying as like this 43AA4432004530,87010-02700,0003,200605301525 looks like it displaying how many characters in the string at the front. instead it should display like this AA4432004530,87010-02700,0003,200605301525. And serialnumber is dsiplaying when I debug it is looping for ever and When I hit F11 then only the serial Number is displaying in the Text box. Otherwise nothing is in the TextBox.

Can you please take a look at the code for looping Please… I really appreciate IF you can see the below code.
And I am thinking that since I am not closing the port, it is looping for ever and displaying that 43 characters in the starting serialNumber. Please help me loop correctly and close the port. So I have correct SerialNumber in the Textbox.

Private Sub GetDataFromScanner(ByVal Ctl$)
        Dim InString As String  ' Buffer to hold input string
        Dim buffer$
        Dim StartTime As Date
        Dim strSerialNumber As String
        strSerialNumber = TxtSerialNumber.Text

        On Error GoTo ErrorSub
        ScannerInUse = False ': DoEvents() : DoEvents() : DoEvents()
        TxtScannerInfo.Text = ""
        If TxtSerialNumber.Text <> "" Then TxtScannerInfo.Text = TxtSerialNumber.Text & "data Already Exists." : Exit Sub

        StartTime = Now
        Dim moRS232 = New Rs232
        'If moRS232.Open = True Then moRS232.Open = False
        With moRS232
            .Port = 2 '// Uses COM1
            .BaudRate = 9600 '// 2400 baud rate
            .DataBit = 8 '// 8 data bits
            .StopBit = Rs232.DataStopBit.StopBit_1
            .Parity = Rs232.DataParity.Parity_None
            .Timeout = 500         End With
        '// Initializes and Open
        moRS232.Open()
        '// Set state of RTS / DTS
        moRS232.Dtr = True
        moRS232.Rts = True

        ScannerInUse = True

        Do      'Wait for data to come back to the serial port.
            'DoEvents()
            If Not ScannerInUse Then GoTo ExitSub
            TxtScannerInfo.Text = "...Waiting for Scanner"
            buffer$ = buffer$ & moRS232.Read(Int32.Parse(50))
            buffer$ = buffer$ & moRS232.InputStreamString()
        Loop Until InStr(buffer$, vbCrLf) Or Truncate(buffer$, 1) = Chr(13)

        TxtSerialNumber.Text = Truncate(buffer$, (Len(buffer$) - 1))

ExitSub:
        TxtScannerInfo.Text = ""

ErrorSub:
        'If moRS232.Open = True Then moRS232.Open = False

    End Sub

    Private Sub TxtSerialNumber_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtSerialNumber.GotFocus
        GetDataFromScanner(TxtSerialNumber.Text)
    End Sub

Thanks a lot for all your help.
Manasa
0
 

Author Comment

by:manasa355
ID: 16810654
Bob,
Can you please take a look. Please urgent...
Thanks
Manasa
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

755 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