pyserial and com port interrupts

2005-01-12 Thread engsol
Has anyone done a script that will rspond to the serial com port(s)
receive buffer interrupt, as opposed to polling and timeouts? 
Win2000 is the main interest right now.
Thanks
Norm B
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyserial and com port interrupts

2005-01-12 Thread engsol
On Wed, 12 Jan 2005 17:45:48 -0500, Peter Hansen <[EMAIL PROTECTED]> wrote:

>engsol wrote:
>> Has anyone done a script that will rspond to the serial com port(s)
>> receive buffer interrupt, as opposed to polling and timeouts? 
>> Win2000 is the main interest right now.
>
>What problem do you hope to avoid by not using "polling
>and timeouts"?  (Note that if you specify a sizable
>read timeout, you're closer to being interrupt-driven
>than you are to what is traditionally called "polling".)
>
>-Peter

Peter,
Thanks for the reply. I'm working on a s/w test program using python
code. Com1 and com2 play a part. The problem is that the python code
has a lot of work to do...and the results from the hardware under test can
come from either com1 or com2...at any time. It may be a few milliseconds, 
or several seconds, sometimes minutes, before a response is expected. 
I'm not sure what timeout value I'd use. Using threads, and re-loading the
timeout values on the fly may be a solution, but I'm not experienced with
threadsand was hoping to avoid them.
Norm B
-- 
http://mail.python.org/mailman/listinfo/python-list


Com port interrupts again

2005-01-14 Thread engsol
I didn't fully think through my application before posting my
question. Async com port routines to handle com port interrups
only work well if one has access to the low level operating
system. In that case the receive buffer interrupt would cause
a jump to an interrupt service routine.. I don't believe that
Python provides that capabilty directly. The solution then would
be to write a C extention?

The suggestions offered by respondents to my original post
were almost all of a "Use threads, and poll as needed" flavor.
You're right...I need to learn threads as applied to com ports.
Norm B
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Com port interrupts again

2005-01-14 Thread engsol
Thanks much..:)

On 14 Jan 2005 12:25:43 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

>A search on google gave me this library, I haven't tested it though:
>http://groups-beta.google.com/group/comp.lang.python.announce/browse_frm/thread/6d3263250ed65816/291074d7bd94be63?q=com+port+python&_done=%2Fgroups%3Fhl%3Den%26lr%3D%26safe%3Doff%26q%3Dcom+port+python%26qt_s%3DSearch+Groups%26&_doneTitle=Back+to+Search&&d#291074d7bd94be63

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Com port interrupts again

2005-01-15 Thread engsol
On Sat, 15 Jan 2005 19:38:19 + (UTC), Chris Liechti <[EMAIL PROTECTED]> 
wrote:

>engsol <[EMAIL PROTECTED]> wrote in 
>news:[EMAIL PROTECTED]:
>
>> I didn't fully think through my application before posting my
>> question. Async com port routines to handle com port interrups
>> only work well if one has access to the low level operating
>> system. In that case the receive buffer interrupt would cause
>> a jump to an interrupt service routine.. I don't believe that
>
>i would not go that route... the operating system provides sync and async 
>methods to access the serial port. it would make sense to use these before 
>hacking the operating system. (also see below)
>
>> Python provides that capabilty directly. The solution then would
>> be to write a C extention?
>
>ctypes can do many things without a C compiler. it's a very nice an 
>valuable extension, but i won't like to encurage to use it for this 
>particular problem.
>
>> The suggestions offered by respondents to my original post
>> were almost all of a "Use threads, and poll as needed" flavor.
>> You're right...I need to learn threads as applied to com ports.
>
>if you realy want to do async programming, have a look at twisted 
>(http://twistedmatrix.com). it does not only provide async access to the 
>serial port (trough pyserial + some code in twisted) it also delivers some 
>nice utility functions, classes etc, like the reactor, defereds, thread 
>pools (if you can't resist ;-) and many protocol handlers.
>
>chris

Chris, thanks for the pointer to twisted. I'm a bit snow bound, so it's a
good time to actually read some docs...:)
Norm B

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Com port interrupts again

2005-01-15 Thread engsol
On Fri, 14 Jan 2005 23:25:21 -0500, Peter Hansen <[EMAIL PROTECTED]> wrote:

>engsol wrote:
>> I didn't fully think through my application before posting my
>> question. Async com port routines to handle com port interrups
>> only work well if one has access to the low level operating
>> system. In that case the receive buffer interrupt would cause
>> a jump to an interrupt service routine.. I don't believe that
>> Python provides that capabilty directly. The solution then would
>> be to write a C extention?
>
>Maybe, but I doubt that you can or would really want to do this
>with modern operating systems anyway.  With DOS, and similar
>primitive things, glomming onto an interrupt or hooking yourself
>into the interrupt table was pretty easy.  I don't think either
>Windows or Linux is structured such that you just "write a
>C extension" to intercept interrupts.  Instead, you must write
>relatively complicated drivers which have to be loaded at
>system startup (more or less) and be tied into the kernel at
>a relatively low level.  Think "rocket science", at least in
>comparison to writing a simple C extension. :-)
>
>> The suggestions offered by respondents to my original post
>> were almost all of a "Use threads, and poll as needed" flavor.
>> You're right...I need to learn threads as applied to com ports.
>
>At least on Windows, I'm fairly sure you can configure the
>read timeouts so that you get behaviour on reads that for
>all intents and purposes is about as good as an interrupt,
>without the difficulties inherent in that approach, but
>provided you are willing to dedicate a thread to the task.
>
>On Linux, it's possible the read timeouts capabilities are
>a little less flexible (but I've only just barely glanced
>at this area), but as I recall you were on Windows anyway.
>
>BTW, another post pointed you to USPP.  As far as I know,
>it hasn't been updated recently and, while I can't say how
>it compares to PySerial, I believe it's fair to say at
>this point in time that PySerial is the _de facto_ standard
>way to do serial port stuff in Python.  If it doesn't do
>what you need, it's probably a good idea to at least point
>that out in its mailing list so that it can be improved.
>
>-Peter

Peter, thanks for the input...good advice. I took a look at USPP,
and as you observed, it seems to be a bit dated.

Actually, I've violated one of my basic rules: do it the simple way
first, then determine what needs to be expanded, improved, speeded up,
etc.
Norm B

-- 
http://mail.python.org/mailman/listinfo/python-list


How would you program this?

2005-03-02 Thread engsol
There is a number puzzle which appears in the daily paper.
Because I'm between Python projects, I thought it might be
fun to write a program to solve it20 minute job, max.

On closer inspection, it became apparent that it's not a
simple thing to program. How would you approach it?

The puzzle: a 4 x 4 grid. The rows are summed (and given), the
cols are summed (and given), and the two diagonals are summed,
and given. In addition, 4 clues are given, but none of the 4 are in
the same row or col.

Example from today's paper:...solution time is 8 minutes, 1 second,
so they say.

The set of allowable numbers  is 1 thru 9

Rows:
3 + B + C + D = 22
E + F + 8 + H = 26
I + J + K + 8 = 31
M + 7 + O + P = 25

Col sums:
24, 18, 31, 31

Diag sums:
3 + F + K + P = 24
M + J + 8 + D = 24



The first impulse is to just brute force it with nested for loops,
but the calculator shows the possible combinations are 
9^12 = 5,159,780,352, which would take much too long.

Another approach would be to inspect each square and determine
what the range of reasonable numbers would be. For example,

if A + 9 + C + D = 14, then A, C, D can only have a value of 1 or 2 or 3,
which would greatly reduce the for loop range of A, C and D. 
While useful, it's still a manual task.

I can't help but think there's a better way. If you have a real Python
project, this isn't worth your time, but if a student, it might be a good
exercise to think how you'd do it.
Norm B
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: How would you program this?

2005-03-02 Thread engsol
When I posted the puzzle, I typo'd the first row.

It should read *5* + B + C + D = 22

instead of 3. 

Thanks to all with suggestions so far.
Norm B
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How would you program this?

2005-03-03 Thread engsol
On Thu, 3 Mar 2005 14:57:13 -, "Duncan Smith" <[EMAIL PROTECTED]> wrote:

>
>"engsol" <[EMAIL PROTECTED]> wrote in message
>news:[EMAIL PROTECTED]
>> There is a number puzzle which appears in the daily paper.
>> Because I'm between Python projects, I thought it might be
>> fun to write a program to solve it20 minute job, max.
>>
>> On closer inspection, it became apparent that it's not a
>> simple thing to program. How would you approach it?
>>
>> The puzzle: a 4 x 4 grid. The rows are summed (and given), the
>> cols are summed (and given), and the two diagonals are summed,
>> and given. In addition, 4 clues are given, but none of the 4 are in
>> the same row or col.
>>
>> Example from today's paper:...solution time is 8 minutes, 1 second,
>> so they say.
>>
>> The set of allowable numbers  is 1 thru 9
>>
>> Rows:
>> 3 + B + C + D = 22
>> E + F + 8 + H = 26
>> I + J + K + 8 = 31
>> M + 7 + O + P = 25
>>
>> Col sums:
>> 24, 18, 31, 31
>>
>> Diag sums:
>> 3 + F + K + P = 24
>> M + J + 8 + D = 24
>>
>>
>>
>> The first impulse is to just brute force it with nested for loops,
>> but the calculator shows the possible combinations are
>> 9^12 = 5,159,780,352, which would take much too long.
>>
>> Another approach would be to inspect each square and determine
>> what the range of reasonable numbers would be. For example,
>>
>> if A + 9 + C + D = 14, then A, C, D can only have a value of 1 or 2 or 3,
>> which would greatly reduce the for loop range of A, C and D.
>> While useful, it's still a manual task.
>>
>> I can't help but think there's a better way. If you have a real Python
>> project, this isn't worth your time, but if a student, it might be a good
>> exercise to think how you'd do it.
>> Norm B
>
>This sort of thing actually is a real Python project for me.  Unfortunately
>you don't generally (in practice) end up with constraints on diagonals in
>contingency tables, so my code can't solve this particular problem.  You
>might be interested in checking out the shuttle algorithm (Fienberg and
>Dobra), and seeing if you can tweak it to handle more general constraints.
>
>Duncan
>

The diagonal constraint is interestingit seems to affect the number of
solutions. One surprise, (not being a math major), was that when I let the
brute force run (forever, it seemed), but without the diagonal qualification(s),
there was maybe 100 solutions. The reson it was a surprise it that years
ago a programmer used the row-sum, col-sum method to detect and correct
data errors. He swore it was robust, and 100% reliable. Seems that that
isn't the case.
Norm B
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How would you program this?

2005-03-03 Thread engsol
On Thu, 3 Mar 2005 15:32:36 -0500, Bill Mill <[EMAIL PROTECTED]> wrote:

>On Thu, 03 Mar 2005 10:52:23 -0800, engsol <[EMAIL PROTECTED]> wrote:
>> 
>> The diagonal constraint is interestingit seems to affect the number of
>> solutions. One surprise, (not being a math major), was that when I let the
>> brute force run (forever, it seemed), but without the diagonal 
>> qualification(s),
>> there was maybe 100 solutions. The reson it was a surprise it that years
>> ago a programmer used the row-sum, col-sum method to detect and correct
>> data errors. He swore it was robust, and 100% reliable. Seems that that
>> isn't the case.
>
>I think that it probably is a decent gut-check for data errors, for
>the simple reason that changing one number requires, at a minimum,
>three other changes in order to maintain both the row and column sums.
>If you have at least a decent data fidelity rate, that is unlikely to
>happen, and even if it does, very very unlikely to happen in such a
>way that the row and column sums are maintained; especially as the
>number of rows and columns grows.
>
>Better to just do a crc or a hash of the data though.
>
>Peace
>Bill Mill
>bill.mill at gmail.com

You make a good point Bill, and are entirely correct. 
I never liked that programmer, and was too hasty in 
condeming his work...shame on meI was unfair.

BTW, I ran the code you suggested, (I like your approach), after
correcting the A cell value (should have 5 vice 3 as I posted). There seems
to be 16 solutions, one of which agrees with the puzzle author.

I don't fully understand this line of your code:
return [(a,b,c) for a in ns for b in ns for c in ns if a + b + c == limit]
If  the "if" part is true, does it  'trigger' the return?
Norm B

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How would you program this?

2005-03-03 Thread engsol
On Thu, 03 Mar 2005 19:32:19 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote:

>engsol wrote:
>> I don't fully understand this line of your code:
>> return [(a,b,c) for a in ns for b in ns for c in ns if a + b + c == limit]
>> If  the "if" part is true, does it  'trigger' the return?
>
>This code is basically equivalent to:
>
>lc = []
>for a in ns:
> for b in ns:
> for c in ns:
> if a + b + c == limit:
> lc.append((a, b, c))
>return lc
>
>Does that help?
>
>STeVe

Now it's clearthank you.
Norm B

-- 
http://mail.python.org/mailman/listinfo/python-list