Link to home
Start Free TrialLog in
Avatar of manasa355
manasa355

asked on

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

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) .NET version?

2) Download location?

Bob
Avatar of manasa355
manasa355

ASKER

1)Microsoft Development Environment 2003           Version 7.1.3088
Microsft DotNet Environment Framework 1.1        Version 1.1.4322SP1

1) Can you use HyperTerminal, and connect to the bar code scanner?

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

Bob
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
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
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
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
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
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

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
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
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





ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Bob,
Can you please take a look. Please urgent...
Thanks
Manasa