Occasionally I have the need to write some communications software, typically something that connects to a serial or TCP port, writes requests and reads responses, writing the results into a database.
Usually the basics are simple enough to achieve, but the challenge is in making a robust solution that will cope with lost connections and any other variety of reasons why things might go wrong (including bugs in the script itself).
What is the "best" way to approach something like this? Are some programming languages better than others for this? What's the best way to ensure a script restarts if it fails for some reason?
I'm looking for something Debian/Ubuntu based if relevant.
[Aside: One reason for asking is that I have been playing with an app I downloaded that talks to CurrentCost power meters, with a PHP-based web interface and a Python-based daemon, and I'm not sure why Python was chosen for it; also I note that the daemon regularly crashes on my system leaving me without any data.]
I've started to use Python almost exclusively for all development over the last year or so on both Linux and Windows environments, including TCP daemon style apps.
I did try going back to C, but I've been spoilt by C# over the last 7 years and don't really want to live without my garbage collector.
I recently needed to write reasonably complex TCP server and after trying Php - which is realistically about as close to C that I want to get - went back to Python, it just feels like a more natural language. Python comes with a decent Socket Server and can be daemonized relatively easily.
As for how to catch those errors and bugs, I'm sure I'm teaching you how to suck eggs, but.... try...catch (or its equivalent) is your friend :-)
N.
Mark Rogers mark@quarella.co.uk
Occasionally I have the need to write some communications software, typically something that connects to a serial or TCP port, writes requests and reads responses, writing the results into a database. [...] What is the "best" way to approach something like this? Are some programming languages better than others for this? What's the best way to ensure a script restarts if it fails for some reason?
I think the best approach is to build on well-tested available libraries. I used Perl, but I expect Python and other languages have good libraries too. Perl did have the benefit of both serial and TCP connections being under IO::Socket and behaving in fairly similar ways.
The best way to ensure a script restarts if it fails is to run it from something like daemon or maybe even init (with inittab), but beware that the device or connection may be left in an unclean state after a crash and set restart parameters so that the fastest possible series of crashes and restarts won't harm the device.
Good luck!
On 08/01/13 17:32, MJ Ray wrote:
I think the best approach is to build on well-tested available libraries. I used Perl, but I expect Python and other languages have good libraries too. Perl did have the benefit of both serial and TCP connections being under IO::Socket and behaving in fairly similar ways.
I agree with the idea of using standard and well tested libraries.
As things stand I'm not competent in Perl or Python but Python is higher up my list of things to learn so I'm leaning that way.
The best way to ensure a script restarts if it fails is to run it from something like daemon or maybe even init (with inittab), but beware that the device or connection may be left in an unclean state after a crash and set restart parameters so that the fastest possible series of crashes and restarts won't harm the device.
I Googled for "writing communications daemon in python" and there's plenty of examples out there, but going back to the point about sticking with well tested libraries it's hard to sort the wheat from the chaff. Can anyone recommend any good resources (dead tree considered) to get me started? As a python novice (but, I like to think, a competent programmer) I don't know the right places to look for reliable python resources that don't assume years of python experience.
Mark Rogers mark@quarella.co.uk
[...] Can anyone recommend any good resources (dead tree considered) to get me started? As a python novice (but, I like to think, a competent programmer) I don't know the right places to look for reliable python resources that don't assume years of python experience.
http://docs.python.org/ and probably something in Interprocess Communication and Networking chapter of the library reference or the Cheeseshop http://pypi.python.org/ will reveal good libraries for Python, or at least what to search the web for to find discussion of how to write it.
I'd accuse it of being a bit dry, like the language manuals we used to use at UEA, rather than assuming years of experience.
Hope that helps,
I've started to use Python almost exclusively for all development over the last year or so on both Linux and Windows environments, including TCP daemon style apps.
I did try going back to C, but I've been spoilt by C# over the last 7 years and don't really want to live without my garbage collector.
I recently needed to write reasonably complex TCP server and after trying PHP - which is realistically about as close to C that I want to get - I went back to Python, it just feels like a more natural language. Python comes with a decent Socket Server and can be daemonized relatively easily.
As for how to catch those errors and bugs, I'm sure I'm teaching you how to suck eggs, but.... try...catch (or its equivalent) is your friend :-)
Nick.
On Mon, Jan 7, 2013 at 4:07 PM, Mark Rogers mark@quarella.co.uk wrote:
Occasionally I have the need to write some communications software, typically something that connects to a serial or TCP port, writes requests and reads responses, writing the results into a database.
Usually the basics are simple enough to achieve, but the challenge is in making a robust solution that will cope with lost connections and any other variety of reasons why things might go wrong (including bugs in the script itself).
What is the "best" way to approach something like this? Are some programming languages better than others for this? What's the best way to ensure a script restarts if it fails for some reason?
I'm looking for something Debian/Ubuntu based if relevant.
[Aside: One reason for asking is that I have been playing with an app I downloaded that talks to CurrentCost power meters, with a PHP-based web interface and a Python-based daemon, and I'm not sure why Python was chosen for it; also I note that the daemon regularly crashes on my system leaving me without any data.]
-- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
______________________________**_________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/**mailman/listinfo/mainhttp://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On 9 January 2013 10:37, Nick Heppleston nick@modhul.com wrote:
I've started to use Python almost exclusively for all development over the last year or so on both Linux and Windows environments, including TCP daemon style apps.
That's good to hear - it sounds like you've been where I am but have already done the research and I can just blindly copy your path :-)
I recently needed to write reasonably complex TCP server and after trying PHP - which is realistically about as close to C that I want to get - I went back to Python, it just feels like a more natural language. Python comes with a decent Socket Server and can be daemonized relatively easily.
I have done a lot with PHP over the last few years and in many cases it's an ideal tool for the job, but I've developed a tendency to shoehorn other jobs into PHP tasks because I have the libraries already ready to go. PHP can be used for scripts etc but Python is, I believe anyway, much better suited.
As for how to catch those errors and bugs, I'm sure I'm teaching you how to suck eggs, but.... try...catch (or its equivalent) is your friend :-)
For some reason I've never really used exception handling in anger, so to that extent any egg sucking lessons are welcome. But I do at least know what you mean by it! (PS: I therefore take back my previous comment about being a competant programmer. What's wrong with GOTO anyway?)