Frustration debugging serial code
MRAB
python at mrabarnett.plus.com
Fri May 7 15:23:38 EDT 2010
William R. Wing (Bill Wing) wrote:
>
> 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 terminal window, it fails.
> If the import were failing, wouldn't I get failures on the serial
> operations?
If the import fails then you wouldn't be able to create a serial object.
> In particular, the ser object is exactly what it should be. If I print
> it out, I get
> the same value for ser in either terminal window mode or in the debugger.
>
> The various status print statements give exactly the expected results in
> both
> cases. But when invoked from the terminal window, the X10 controller
> doesn't
> get commands.
>
So you can execute:
stat1 = ser.write('\x02')
but the X10 controller just isn't receiving it? Is stat1 the same in
both cases (ie, working and non-working)?
>>> 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: ')
>>
More information about the Python-list
mailing list