Re: Serial port failure



Leo,

I like your tuple idea. I will implement it. The ack and nak both
have the same format, namely: "Id Ack" or "Id Nak"

rob

On Dec 15, 4:34 pm, "Leo Kislov" <Leo.Kis...@xxxxxxxxx> wrote:
Rob wrote:
Hi all,

I am fairly new to python, but not programming and embedded. I am
having an issue which I believe is related to the hardware, triggered
by the software read I am doing in pySerial. I am sending a short
message to a group of embedded boxes daisy chained via the serial port.
When I send a 'global' message, all the connected units should reply
with their Id and Ack in this format '0 Ack' To be certain that I
didn't miss a packet, and hence a unit, I do the procedure three times,
sending the message and waiting for a timeout before I run through the
next iteration. Frequently I get through the first two iterations
without a problem, but the third hangs up and crashes, requiring me to
remove the Belkin USB to serial adapter, and then reconnect it. Here
is the code:

import sys, os
import serial
import sret
import time

from serial.serialutil import SerialException
####################################################################
#### GetAck Procedure
####################################################################
def GetAck(p):
response = ""

try:
response = p.readline()
except SerialException:
print ">>>>>Timed out<<<<<"
return -1
res = response.split()

#look for ack in the return message
reslen = len(response)
if reslen > 5:
if res[1] == 'Ack':
return res[0]
elif res[1] == 'Nak':
return 0x7F
else:
return -1

Snip <<<<<<
####################################################################
#### GetNumLanes Procedure
####################################################################
def GetNumLanes(Lanes):
print "Looking for connected units"
# give a turn command and wait for responses
msg = ".g t 0 336\n"

for i in range(3):
port = OpenPort()
time.sleep(3)
print port.isOpen()
print "Request #%d" % (i+1)
try:
port.writelines(msg)
except OSError:
print "Serial port failure. Power cycle units"
port.close()
sys.exit(1)

done = False
# Run first connection check
#Loop through getting responses until we get a -1 from GetAck
while done == False:
# lane will either be -1 (timeout), 0x7F (Nak),
# or the lane number that responded with an Ack
lane = GetAck(port)
if lane >= '0':Your GetAck returns either string or number and then you compare it
with a string. If you compare string with a number python currently
returns result you probably don't expect

-1 >= '0'
False
0x7f >= '0'False

This is a wart and it will be fixed in python 3.0 (it will raise
exception) I think you should rewrite GetAck to return a tuple (state,
lane)

def GetAck(p):
response = ""

try:
response = p.readline()
except SerialException:
print ">>>>>Timed out<<<<<"
return 'Timeout', 'NoID'
res = response.split()

#look for ack in the return message
reslen = len(response)
if reslen > 5:
if res[1] == 'Ack':
return 'Ack', res[0]
elif res[1] == 'Nak':
return 'Nak', Does Nak response contain lane id?
else:
return 'Unknown', 'NoID'

And then instead of

lane = GetAck(port)
if lane >= '0':

use

state, lane = GetAck(port)
if state == 'Ack':

-- Leo

.



Relevant Pages

  • Re: Serial port failure
    ... with their Id and Ack in this format '0 Ack' To be certain that I ... from serial.serialutil import SerialException ... response = p.readline ... lane = GetAck ...
    (comp.lang.python)
  • Lets whisper with the glad reactors, but dont spread the increasing works.
    ... What did Ali relax in response to all the ... Who will we dance after Pat whispers the ... Don't even try to undertake a lane! ... locks will regardless access the chapels. ...
    (sci.crypt)
  • Re: Socket weirdness
    ... ACK or RST, ACK and RST, nothing, or something else? ... Does a blocking Send wait for a response before returning to the caller? ... identified with the unorthodox, but possible, scenario for using sockets whereby a peer attempts to shutdown receiving without a ...
    (microsoft.public.dotnet.framework)
  • Re: Passengers on Trains...
    ... clipping his knee defused the situation by apologising. ... that he hasn't experienced tailgaters in anything except lane three ... I agree, but the first response should be a polite, 'excuse me, but I was ...
    (uk.rec.driving)
  • Re: Complicated Car Accident
    ... The only reason you may be held partly responsible is that the ... (But see last response below) ... side of the lane was letting him out, ... stationary vehicles, of which some of them were parked. ...
    (uk.legal)