Conversation
…a failure Add timeout parameter Add a few convenience functions to avoid dealing with SYNTH_A,SYNTH_B constants
|
It would be better to apply some consistency in error handling.
In regards to the latter, the current/previous philosophy was "throw hands in air and give up", which I agree is a bad way to go about it. But hiding exceptions from the caller seems almost as bad since it hides that something went wrong. Personally I vote for cleaning up and rethrowing. In regards to the convenience functions, my initial thought it that they can easily be accomplished via a subclass (e.g. |
|
Hi Patrick, For example, if I call get_frequency and someone has sent some spurious characters to the synthesizer, it's likely that the method will encounter an error. As it is now, an exception will be thrown, but conn will be left open, which means that if I then catch the exception in my code and want to retry the command; I have to first check that conn is closed. Otherwise, when I call get_frequency again, it will choke on the conn.open() line, because the connection is still open. In particular, I use this code a lot from the ipython prompt, so if an error comes up, I have to manually call vs.conn.close(), which gets tiring. I wrote the try/finally clauses such that they will still throw the same exact errors as before (note, in many cases there is no except block, so the exception still gets raised, and when I used the except block, I re-raise the exception). Regarding the convenience functions; I definitely want these in the module, so I don't have to install yet another dependency (both valon_synth and my_valon_synth). Creating a subclass is fine I guess, but seems more cumbersome. This class is used for exactly one piece of hardware: the Valon synthesizer. So it's not like it's going to grow an infinite number of convenience methods. I added the ones that I use most often; again often for the sake of making interactive use more pleasant. Glenn |
|
I guess I can see your point regarding exceptions, but still to be complete I think the try blocks should include any/all of the serial port operations (at least write and read, if not also open) as they can all throw. Again using get_frequency as an example, the first call to Serial.write could throw and the connection would still be left open since it is not in the try block. Also the try/except block in set_frequency, set_rf_level, and set_options could be changed to try/finally for consistency. And if we're adding convenience methods, may as well do it for all the synthesizer dependent methods (*_label[_a, _b, s], *_phase_lock[_a, _b, s], *_vco_range[_a, _b, s], etc) again for consistency. |
|
You are right: I missed a .write. I'll fix that. The intent was to include all .write and .read calls. I don't think .open should be included because if .open fails, the port should still be closed, so it shouldn't be needed. But I suppose it shouldn't hurt (provided closing an already closed port doesn't error; I don't think it does). Exhaustively adding convenience methods is fine, but I'll save that for another day; I included the ones I use all the time. |
I've added a few improvements; in particular many try, except, finally clauses to make sure the serial port connection gets closed if something goes wrong. Also, ability to set the serial port timeout easily. Finally, added a few convenience functions to simplify common tasks. I haven't thoroughly tested it all, but the changes are straightforward.