From: Craig Lister on 24 May 2010 21:40
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
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,
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:
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.