Serial Port / RS232

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony K

    #1

    Serial Port / RS232

    I have a slight problem. When attempting to receive data via my serial port
    I sometimes do not get the complete data. For example, I have a scanner
    that reads barcodes. When reading barcode after barcode, there might be 2
    or 3 barcodes in a row where I do not get the complete barcode. It will be
    split up over 2 lines.


    Here is my code and an example of the data received is toward the bottom.

    Dim WithEvents com1 As New SerialPort

    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
    Handles Me.Load
    Try
    With com1
    .PortName = "Com4"
    .BaudRate = 9600
    .Parity = Parity.None
    .DataBits = 8
    .StopBits = StopBits.One
    End With
    com1.Open()
    Catch ex As Exception
    MsgBox(ex.ToStr ing)
    End Try
    End Sub

    Private Sub com1_DataReceiv ed(ByVal sender As Object, ByVal e As
    System.IO.Ports .SerialDataRece ivedEventArgs) Handles com1.DataReceiv ed
    Dim returnStr As String
    returnStr = com1.ReadExisti ng 'ReadLine does not work. Any
    reason why??
    ReceiveSerialDa ta(returnStr)
    com1.DiscardInB uffer()
    End Sub

    Sub ReceiveSerialDa ta(ByVal msg As String)
    ' Receive strings from a serial port.
    CheckForIllegal CrossThreadCall s = False
    TextBox1.Text &= msg & vbCrLf
    End Sub

    I cannot find anything about an EOM (End Of Message) character setting for
    the Serial Port. My scanner is set up to send <CR><LFat the end of each
    scan.

    Here is an example of the data that I receive. I scanned 2 barcodes over
    and over. You can see the barcodes are split up but I'm not sure what is
    causing this. Any suggestions would be greatly appreciated.

    036600826313

    04740
    0097742

    036600826313

    047400097742

    036600826313

    0474
    00097742

    036
    600826313

    047
    400097742

    036600826313

    04740009
    7742


    Thank you all,
    Tony

  • Tony K

    #2
    Re: Serial Port / RS232

    Sorry! Nevermind. I jumped the gun here. My scanner was set up with a
    Preamble to send a <CRand my Postamble was DISABLED. Now that I've fixed
    those, I also removed the vbCrLf in my ReceiveSerialDa ta. Everything works
    great.



    "Tony K" <king-tony2@NOSPAMcom cast.netwrote in message
    news:1F350BB0-6EF7-4BE7-B91E-99E26A0A844B@mi crosoft.com...
    >I have a slight problem. When attempting to receive data via my serial
    >port I sometimes do not get the complete data. For example, I have a
    >scanner that reads barcodes. When reading barcode after barcode, there
    >might be 2 or 3 barcodes in a row where I do not get the complete barcode.
    >It will be split up over 2 lines.
    >
    >
    Here is my code and an example of the data received is toward the bottom.
    >
    Dim WithEvents com1 As New SerialPort
    >
    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Try
    With com1
    .PortName = "Com4"
    .BaudRate = 9600
    .Parity = Parity.None
    .DataBits = 8
    .StopBits = StopBits.One
    End With
    com1.Open()
    Catch ex As Exception
    MsgBox(ex.ToStr ing)
    End Try
    End Sub
    >
    Private Sub com1_DataReceiv ed(ByVal sender As Object, ByVal e As
    System.IO.Ports .SerialDataRece ivedEventArgs) Handles com1.DataReceiv ed
    Dim returnStr As String
    returnStr = com1.ReadExisti ng 'ReadLine does not work. Any
    reason why??
    ReceiveSerialDa ta(returnStr)
    com1.DiscardInB uffer()
    End Sub
    >
    Sub ReceiveSerialDa ta(ByVal msg As String)
    ' Receive strings from a serial port.
    CheckForIllegal CrossThreadCall s = False
    TextBox1.Text &= msg & vbCrLf
    End Sub
    >
    I cannot find anything about an EOM (End Of Message) character setting for
    the Serial Port. My scanner is set up to send <CR><LFat the end of each
    scan.
    >
    Here is an example of the data that I receive. I scanned 2 barcodes over
    and over. You can see the barcodes are split up but I'm not sure what is
    causing this. Any suggestions would be greatly appreciated.
    >
    036600826313
    >
    04740
    0097742
    >
    036600826313
    >
    047400097742
    >
    036600826313
    >
    0474
    00097742
    >
    036
    600826313
    >
    047
    400097742
    >
    036600826313
    >
    04740009
    7742
    >
    >
    Thank you all,
    Tony

    Comment

    • Dick Grier

      #3
      Re: Serial Port / RS232

      OK.

      I had written a reply that included (with some example code -- since your
      code is working, Ill include the other thoughts):

      ReadLine returns data (blocking) only AFTER a newline (default is carriage
      return plus line feed). I never use ReadLine. I always use ReadExisting
      (or Read), and look for whatever specific terminating character I need.

      Don't discard anything -- else something received while you are processing a
      part of a read will go somewhere less than useful. As soon as you read
      data, you have cleared that data, subsequent data may be buffered WHILE this
      process is executing.

      Dick

      --
      Richard Grier, MVP
      Hard & Software
      Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
      Edition,
      ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
      2006.
      See www.hardandsoftware.net for details and contact information.


      Comment

      • Tony K

        #4
        Re: Serial Port / RS232

        Dick,
        If you don't mind I'd like to see some of your example code. Maybe I can
        modify my code to complete this in a better way. The examples I've found on
        MSDN just haven't been as clear as I'd like.

        Thanks,
        Tony



        "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e in message
        news:eBZesi1XHH A.4008@TK2MSFTN GP05.phx.gbl...
        OK.
        >
        I had written a reply that included (with some example code -- since your
        code is working, Ill include the other thoughts):
        >
        ReadLine returns data (blocking) only AFTER a newline (default is carriage
        return plus line feed). I never use ReadLine. I always use ReadExisting
        (or Read), and look for whatever specific terminating character I need.
        >
        Don't discard anything -- else something received while you are processing
        a part of a read will go somewhere less than useful. As soon as you read
        data, you have cleared that data, subsequent data may be buffered WHILE
        this process is executing.
        >
        Dick
        >
        --
        Richard Grier, MVP
        Hard & Software
        Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
        Edition,
        ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
        2006.
        See www.hardandsoftware.net for details and contact information.
        >

        Comment

        • Randy Gantner

          #5
          Re: Serial Port / RS232

          Other events can happen while serial data is being received. Your best bet is to append new data to a string until the desired terminator is received. Handle the message and remove it from the the string. In some instances more data can arrive while (or before)handling the previous terminated message. You must preserve this data or the subsequent message will be incomplete.

          Comment

          • Randy Gantner

            #6
            Re: Serial Port / RS232

            Other events can happen while serial data is being received. Your best bet is to append new data to a string until the desired terminator is received. Handle the message and remove it from the the string. In some instances more data can arrive while (or before)handling the previous terminated message. You must preserve this data or the subsequent message will be incomplete.

            Comment

            Working...