Results 1 to 3 of 3

Thread: Help getting my head wrapped around ReadLine and ReadByte

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Location
    Ottawa
    Posts
    19

    Help getting my head wrapped around ReadLine and ReadByte

    Hi

    I need some information on SerialPort.ReadLine and SerilaPort.ReadByte.

    I think ReadLine will read the port data until it gets to CR and ReadByte will read only one Byte at a time.

    Also what function does SerialPort1.ReadTimeout = xx serve, I think it’s used to stop trying to get data if a byte has not been received in that time frame but correct me if I’m wrong.


    I have serial data coming at me all the time, It may have CR in it or not but I have to capture it anyway, some times it’s just a prompt eg. :>, some times there are many lines of data.

    I have setup a timer to send to the serial port and wait until the next timeout to process the data I get.

    So I need some thing that will just keep getting data until the timer times out, any one got some thing simple ?

    Thanks

    Gord

    Help getting my head wrapped around ReadLine and ReadByte.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Help getting my head wrapped around ReadLine and ReadByte

    The first thing to decide is whether you're getting text or binary data. If it's text then you use a method for reading text and if it's binary data then you use a method for reading binary data. If you are receiving text and you're not getting it in discrete lines then ReadLine would be inappropriate. If you just want to read all the text available then you call ReadExisting.

    This is what the documentation says about ReadTimeout:
    Gets or sets the number of milliseconds before a time-out occurs when a read operation does not finish.
    Any method of the SerialPort whose name begins with Read will throw a TimeoutException if data is not received within the period specified by that property. Again, from the documentation:
    The time-out can be set to any value greater than zero, or set to InfiniteTimeout, in which case no time-out occurs. InfiniteTimeout is the default.

  3. #3
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Help getting my head wrapped around ReadLine and ReadByte

    You don't really describe what the data stream looks like, so I'm just going to offer some thoughts.

    1. Don't use ReadByte. Unless you expect only a single byte to be sent, ReadByte is very slow.
    2. Don't use ReadLine unless you are sure that the attached device is sending lines of data that are terminated by a carriage return and linefeed character pair. ReadLine may only be used with ASCII/ANSI text (non-binary) data.
    3. If your device is sending ASCII/ANSI text data, it is better to use ReadExisting. You can append new data to a buffer and parse it from that buffer.
    4. If your device is sending binary data, then call Read and assign that data to an array of type Byte.

    The DataReceived event can be used, though it may or may not be "the way to go." This is a depends... On what you really need to do.

    As to one part of your question, you might use a Timer for this operation. For example, you might count the number of times that a Timer Tick event has been generated and multiply that number by the Timer Interval. If the result of this calculation exceeds some value, then you can say "TIMEOUT" stop the timer and reset the counter variable. If their hasn't been a timeout, append new receive data to some appropriate buffer. After the timeout, you could then examine that buffer for whatever might have been received during each of the intermediate times.

    I am not sure that this is either best or even appropriate to your task, since I am not really sure what you are doing.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width