Serial port failure

drake at ultech.com drake at ultech.com
Fri Dec 15 13:47:29 EST 2006


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':
>                     if False == Lanes.has_key(lane):
>                         Lanes[lane] = True
> 	        else:
> 	            done = True
> 	    port.close()
> 	    time.sleep(3)
>
> # Report number of lanes found
>         NumLanes = len(Lanes)
> 	if NumLanes == 1:
>     		print "\n\nFound 1 unit connected"
> 	else:
>     		print "\n\nFound %d units connected" % NumLanes
>
> 	return NumLanes
>
>
> >>>>>> Snip <<<<<<
> ####################################################################
> #### Main Program Code Section
> ####################################################################
>
> #open the serial port
> # capture serial port errors from trying to open the port
>
> port = OpenPort()
>
> # If we got to here, the port exists.  Set the baud rate and timeout
> values
>
> # I need to determine how many lanes are on this chain
> # First send a turn command
>
> #Create a dictionary of lanes so I can check each lane's responses
> Lanes = {}
> #<><><><><><><><><><><><><><><><>
> # Call the lane finder utility
> NumLanes = GetNumLanes(Lanes)
> #<><><><><><><><><><><><><><><><>
>
> #if no lanes responded, exit from the utility
> if 0 == NumLanes:
>     print "I can't find any units connected."
>     print "Check your connections and try again"
>     sys.exit(1)
>
> # list the lanes we have in our dictionary
> for n in Lanes:
>     print "Lane - %s" % n
>
> Now, here is the error message that I get
>
> dad at nb29:~/py$ ./Thex.py
> Looking for connected units
> True
> Request #1
> True
> Request #2
> Serial port failure.  Power cycle units
> dad at nb29:~/py$ ./Thex.py
> The serial port is unavailable.
> Disconnect your USB to Serial adapter, Then
> reconnect it and try again.
> dad at nb29:~/py$
>
> Does anyone have any ideas?
>
> Thanks,
>
> rob < Amateur.N7TZG at gmail.com >

In the second iteration of your loop, you appear to be opening a port
that is already open:

 	for i in range(3):
 	    port = OpenPort()

thus the error message: "the serial port is unavailable".

--Drake Smith




More information about the Python-list mailing list