How to read all data across a serial COM

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

    How to read all data across a serial COM

    Im trying to raed data over a com port. The data comes over the serial
    port as such reading CRLF reading CRLF and so on.

    The code below only sees the first reading CRLF and then only added
    that into the DSIDXPort arraylist and never adds readings 2 through
    16.....

    How can I get it to read those?

    [CODE]

    serialPort.Read Timeout = 50
    serialPort.Disc ardInBuffer()

    Dim strData As String = String.Empty

    If Not serialPort.IsOp en Then
    strData = String.Empty
    Else
    strData = serialPort.Read Line
    End If






    If strData.Length 0 Then

    Dim tempArray() As String
    strData = strData.Replace (vbCrLf,
    vbTab).Replace( Chr(26), "").Replace(Chr (12), "").Replace(Chr (13), "")

    'parses out the information gathered from the COM port
    to a temp location
    tempArray = Split(strData, vbTab)


    If serialPort.Read Timeout = 50 Then
    Dim intLoop As Integer
    For intLoop = 0 To UBound(tempArra y)
    If IsNumeric(tempA rray(intLoop)) Then
    DSIMRPort.Add(t empArray(intLoo p))
    End If

    Next
    End If
    end if


    [\CODE]
  • Armin Zingler

    #2
    Re: How to read all data across a serial COM

    "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb
    Im trying to raed data over a com port. The data comes over the
    serial port as such reading CRLF reading CRLF and so on.
    I don't fully understand the meaning of this sentence.
    The code below only sees the first reading CRLF and then only added
    that into the DSIDXPort arraylist and never adds readings 2 through
    16.....
    >
    How can I get it to read those?

    I only see one "serialPort.Rea dLine". When or where do you expect it to read
    more?


    Armin

    Comment

    • James Hahn

      #3
      Re: How to read all data across a serial COM

      I presume you mean that the format of the data stream is variable length
      strings separated by CR and LF.

      As VB is an event based language, you need to make use of the event that
      indicates that some data has been received from the port. There is no event
      available that tells you that a CRLF has been received, but the DataReceived
      event will tell you when each character is received. You can use processing
      in that event to see if the received character was a CR or LF - if it is,
      then there is a data sentence in the buffer that can be retrieved. You can
      then use Readline to retrieve the data from the buffer.

      You should not use readline unless you know there is a line to be read.

      "cmdolcet69 " <colin_dolcetti @hotmail.comwro te in message
      news:820caaf7-b896-4419-af0d-a685081985dc@k3 6g2000pri.googl egroups.com...
      Im trying to raed data over a com port. The data comes over the serial
      port as such reading CRLF reading CRLF and so on.
      >
      The code below only sees the first reading CRLF and then only added
      that into the DSIDXPort arraylist and never adds readings 2 through
      16.....
      >
      How can I get it to read those?
      >
      [CODE]
      >
      serialPort.Read Timeout = 50
      serialPort.Disc ardInBuffer()
      >
      Dim strData As String = String.Empty
      >
      If Not serialPort.IsOp en Then
      strData = String.Empty
      Else
      strData = serialPort.Read Line
      End If
      >
      >
      >
      >
      >
      >
      If strData.Length 0 Then
      >
      Dim tempArray() As String
      strData = strData.Replace (vbCrLf,
      vbTab).Replace( Chr(26), "").Replace(Chr (12), "").Replace(Chr (13), "")
      >
      'parses out the information gathered from the COM port
      to a temp location
      tempArray = Split(strData, vbTab)
      >
      >
      If serialPort.Read Timeout = 50 Then
      Dim intLoop As Integer
      For intLoop = 0 To UBound(tempArra y)
      If IsNumeric(tempA rray(intLoop)) Then
      DSIMRPort.Add(t empArray(intLoo p))
      End If
      >
      Next
      End If
      end if
      >
      >
      [\CODE]

      Comment

      • Dick Grier

        #4
        Re: How to read all data across a serial COM

        I'd have to see the actual code. You call ReadLine each time you want to
        read a line (seems clear). Do you have a loop for reading data? Are you
        calling this in the DataReceived event (if so, the you certainly cannot call
        DiscardInBuffer ).


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

        Working...