Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I'm planning to use an RS422 to USB converter to fetch bytes arriving over an RS422 port from an instrument. Each data set or sample contain 37 bytes including ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    7

    Reading Bytes from RS485 stream

    Hi,

    I'm planning to use an RS422 to USB converter to fetch bytes arriving over an RS422 port from an instrument. Each data set or sample contain 37 bytes including a one byte checksum at the end. The data sample begins with a few sycronization bytes denoting the beginning of the sample.

    I do not know when I will start recieving the datastream so I would like to listen to the port and first detect the synchronization bytes and then store all of the 37 bytes of the sample. Then I would like to validate the sample by checking it against the checksum. Then I would like to strip the sample of the bytes of the unessesary bytes and keep those that I want to process.

    I'm wondering if there is an efficient and simple way to do this in C?

    I've worked with RS232 datastreams, but that was at a low datarate. This RS422 stream will be streaming at 115200 baud. I do not need to catch every sample that comes over the stream, but I would like to make sure that I capture a complete sample each loop through my program so that the checksum checks out.

    Initially, it is fine to not get all the 37 byte samples coming over the data stream, but as an improvement, I would like to figure out a way to not miss a sample. This is so that I can apply some filtering to the data coming over.

    Thank you,
    windell

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm wondering if there is an efficient and simple way to do this in C?
    There's no reason why not. What you've described is fairly simple.

    Unless.

    Unless I'm missing a complication. Is your program also doing I/O to other devices as they permit? Is your program also trying to do other computations? Are there other programs contending for the CPU so yours could get starved?

    If not, just code it as you've described it. Come back here with specific questions as you go.
    Initially, it is fine to not get all the 37 byte samples coming over the data stream, but as an improvement, I would like to figure out a way to not miss a sample.
    It would make your program simpler just to catch all the data and not miss any. Again, unless I'm not seeing some complication here.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    7
    Thanks for the reply, wje_lf.

    There is a chance that my code will go as part of a larger loop so I might miss some of the data streaming.

    Also, even if I'm only running my program as part of a forever while loop I'm not certain if my loop will come around fast enough as it has to process the sample it just recieved...and the next sample comes immediately after.

    The stream is arriving at a steady rate so it doesn't wait on the program.

    I was actually looking for some code example. I know how to read ASCII characters from a serial port, but I don't know how to read bytes. I'm sure its pretty simple, but I haven't had any experience with yet.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you already know how to read ASCII characters from a serial port, you know how to read bytes. It's the same thing, unless your reading of ASCII characters involved setting the number of data bits per byte to 7. Set it to 8 in the usual way, no parity (probably), and you're set. Just refactor your old code.
    I'm not certain if my loop will come around fast enough
    Be prepared, then, for buffer overflow errors. I've had no experience with those, but you'll learn some new stuff along the way. :)
    I was actually looking for some code example.
    Your own experience with reading ASCII characters from a serial port should be sufficient. But if you want other sample code, download this. I wrote this application to communicate with a modem, but you can ignore the parts you don't need. It's insanely well documented if I say so myself. :)

    I will leave that source code there for at least two weeks.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    7
    This might be a dumb question, but how to I know when the byte starts with all the zeros and ones coming over the line? I do not know of any delimiters that are with the stream seperating the bytes.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I don't remember the details from my college days 40 years ago, but there are framing bits (start bits and stop bits) which help make this work. It's transparent to the programmer.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Jul 2008
    Posts
    7
    thank you very much wje_lf. I just remembered that. Thanks for your patience!

Posting Permissions

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