Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to read a text line from a COM port

How to read a text line from a COM port

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++delphicom
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jrgrobinson
    wrote on last edited by
    #1

    I know this is a simple question that I will regret answering. I am currently working in Borland C++ Builder (VCL) on adding some serial port activity to an older Windows project. No .NET (just yet). Rather than character by character processing messages (NMEA) from a number of serial ports I would like something simple like 'Readline' in .NET2 or an 'fgets'. Reading to a '\n' and timeout. I have done a bit of searching and found a lot of read character by character examples. Does anyone have the magic line at hand of code or recall how to read a text line?

    R 1 Reply Last reply
    0
    • J jrgrobinson

      I know this is a simple question that I will regret answering. I am currently working in Borland C++ Builder (VCL) on adding some serial port activity to an older Windows project. No .NET (just yet). Rather than character by character processing messages (NMEA) from a number of serial ports I would like something simple like 'Readline' in .NET2 or an 'fgets'. Reading to a '\n' and timeout. I have done a bit of searching and found a lot of read character by character examples. Does anyone have the magic line at hand of code or recall how to read a text line?

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      jrgrobinson wrote:

      Rather than character by character processing messages (NMEA) from a number of serial ports I would like something simple like 'Readline'

      With the ordinary serial port driver you simply cannot do this. Each character is transmitted as a groupd of bits consisting of a start bit, the actual character value, parity bits and one or two stop bits. There's no way for the hardware to know how you want to separate messages from each other; it should not since it's the wrong OSI layer for that. It is for an application to handle this kind of situations, or some kind of service or "daemon". I suggest that you implement a worker thread that reads the port, assembles the NMEA messages and puts them into a queue. When a complete new message has arrived and has been put in the queue, the worker thread signals the main thread to inform that there is a new message in the queue. In your worker thread you have to read byte-wise and each NMEA message will start with '$' and end with '\n', if I remember correctly. This way you won't bother the main thread until there's a complete NMEA message in the queue. Have a look at Joe Newcomer's article about serial port programming here[^]. I don't know if the article is applicable for Borland C++ Builder, but it addresses some issues that you probably should think of anyway.


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote

      J 1 Reply Last reply
      0
      • R Roger Stoltz

        jrgrobinson wrote:

        Rather than character by character processing messages (NMEA) from a number of serial ports I would like something simple like 'Readline'

        With the ordinary serial port driver you simply cannot do this. Each character is transmitted as a groupd of bits consisting of a start bit, the actual character value, parity bits and one or two stop bits. There's no way for the hardware to know how you want to separate messages from each other; it should not since it's the wrong OSI layer for that. It is for an application to handle this kind of situations, or some kind of service or "daemon". I suggest that you implement a worker thread that reads the port, assembles the NMEA messages and puts them into a queue. When a complete new message has arrived and has been put in the queue, the worker thread signals the main thread to inform that there is a new message in the queue. In your worker thread you have to read byte-wise and each NMEA message will start with '$' and end with '\n', if I remember correctly. This way you won't bother the main thread until there's a complete NMEA message in the queue. Have a look at Joe Newcomer's article about serial port programming here[^]. I don't know if the article is applicable for Borland C++ Builder, but it addresses some issues that you probably should think of anyway.


        "It's supposed to be hard, otherwise anybody could do it!" - selfquote

        J Offline
        J Offline
        jrgrobinson
        wrote on last edited by
        #3

        Thanks Roger. It does seem like the standard exhaustive read is my more intelligent option. I have been looking at some of the old MSDN (Allen Denver, Serial Communications in Win32, 1995) and there is an EV_RXFLAG which happens in response to an event character you can set in the DCB. That does mean Overlapped receiving though which is too much for this really simple task. I was hoping I could achieve that 'One-line' as .NET2 does with ReadLine. Makes for very simple code. Thanks again for your time in responding.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups