Frustration debugging serial code
William R. Wing
wrw at mac.com
Fri May 7 15:36:20 EDT 2010
On May 7, 2010, at 2:08 PM, MRAB wrote:
> William R. Wing (Bill Wing) wrote:
>> Hello World -
>> I'm new to both Python and this list, but here's hoping someone can spot my problem.
>> System: Mac OS-X, 10.6.3 (Intel dual quad processor)
>> Using Python 2.6.1, and pyserial-2.5_rc2-py2.6
>> The following snippet of code is designed to open a port via a KeySpan USB-to-serial converter and communicate with an X10 power line signaling system. Before you look at the code, let me mention that my frustration is the it executes perfectly if i enter it line by line at the Python prompt, and executes perfectly if I run it under the Wing IDE. It fails silently (no errors) if I run it as a standalone script. Clearly, I'm missing something.
> What do you mean "It fails silently"? It might be unable to find the
> modules if you double-click on the script. You could check for that by
> printing a message if the import statement raises an ImportError.
Maybe I should have been more explicit. The first line in the Python file is:
#!/usr/bin/env Python (alternatively #!/usr/bin/Python - same results either way).
and what I meant was that when I invoke it from a UNIX CLI window, it fails.
If the import were failing, wouldn't I get failures on the serial operations?
In particular, the ser object is exactly what it should be. If I print it out, I get
the same value for ser from either the UNIX CLI or in the debugger, and in
both cases it says "open=True"
The various status print statements give exactly the same (expected) results in
both cases. But when invoked from the terminal window, the X10 controller doesn't
get commands. If I invoke python from the same window, and enter the script
code interactively one line at a time - it works just fine.
>> TIA,
>> Bill Wing
>> import serial, string
>> port = '/dev/tty.KeySerial1'
>> ser = serial.Serial(port, 9600, timeout=1) # Set up handle to serial port
>> stat1 = ser.write('\x02') # Write "Attention" to PowerLink
>> print(' Stat1 is:', stat1) ack = ser.read(2) # Check to see if PowerLink is ready
>> if ack == '\x06\r': # It returns ACK (\x06) \r if it is
>> stat2 = ser.write('\x63\x4E\x5C\x45\x41')# send "On" to module at House Code B, device code 2
>> print(' Stat2 is:', stat2)
>> stat3 = ser.close() # Close serial port
>> print(' Stat3 is:', stat3)
>> elif ack =='\x15': # PowerLink sends NAC (hex 15) if it isn't.
>> print('Received NAK after first open')
>> else: print("Something's wrong at 1.\n")
>> dummy = raw_input(' Hit any key to continue: ')
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100507/e206efb6/attachment-0001.html>
More information about the Python-list
mailing list