CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    3

    Question Serial Device comms application architecture

    Hi guys,

    I have been tasked with writing a small application that interfaces with a serial device. I have managed to create my communication wrapper which basically handles all the comms between the application and the device. So, it has a 'Connect' method, a 'Send' and a 'Recieve'. It runs in a thread which basically does a 'While(_connected) ReadLine', and passes any incoming messages to a message handler within the object. That seems to work well - except for when I close the app! The ReadLine() is blocking, so the thread stays alive, even though the application is closed.

    First question: How do I stop that from happening?

    Then, the design. I have been doing mainly database applications. Can I treat this the same way - instead of a database though, I have a serial device as my 'storage'? So, I have a UI layer, speaking to a setrvice layer, speaking to a business (rules) layer, which speaks to my communication layer. does this sound like a good design for my needs?

    Lets's say my UI requires a TextBox to be populated with PARAM1 from the device. A method in the UI would call the (Static?) service layer, like, maybe:

    string value = ServiceLayer.GetParameterValue(string ParameterName);

    Service Layer accepts that. Creates a Business Layer object, and then passes the call to the BL. The BL then creates (or uses a static?) Communication object. The BL then formats the request into something the device may understand, such as:

    '$QUERY PARAM1'

    It then calls a method on the communication object called Send(string Message) where Message is '$QUERY PARAM1'.

    As I always expect a reply, the Send method in the comms layer might do a ReadLine() and wait for the reply, passing the value back?

    The BL then gets the message back, and does some checking... If all is OK, it passes it back to the Service layer, who maybe parses the message and extracts the value required, and then passes the value back to the UI.

    Does this sound like a good design? Should I be making use of Static classes? Should I connected and disconnect the COM port on each request, or just stay connected?

    Hopefully someone can guide me.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Serial Device comms application architecture

    At the very least I would add a timeout into the area that would read from the port. Under ideal conditions we may expect to get something back everytime we send something out the port but what if the device becomes disconnected or the program crashes, looses power or some other such thing.

    In most applications the response is pretty quick so just a short timeout is usually fine.
    Typically i will use 30 seconds as a default and make it adjustable by the user. In your senario with this method in place I would expect the thread to terminate within 30 seconds after the last byte is received.

    I also normally use a flag to tell my threads the program is shutting down so they can clean up and exit asap.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Apr 2004
    Posts
    3

    Re: Serial Device comms application architecture

    Thanks for the reply.

    I have a timout setup...
    But not 100% sure what to do with it. I can try update a variable on the FormClosing event (Can I?) maybe? And that would be the variable I am checking in my while(_checkme) loop? Would that be a valid option?

Tags for this Thread

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

Featured