473,699 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serial port failure

Rob
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.serialut il 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(Lan es):
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(l ane):
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(Lan es)
#<><><><><><><> <><><><><><><>< ><>

#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@nb29:~/py$ ./Thex.py
Looking for connected units
True
Request #1
True
Request #2
Serial port failure. Power cycle units
dad@nb29:~/py$ ./Thex.py
The serial port is unavailable.
Disconnect your USB to Serial adapter, Then
reconnect it and try again.
dad@nb29:~/py$

Does anyone have any ideas?

Thanks,

rob < Am***********@g mail.com >

Dec 15 '06 #1
13 6204
Rob <Am***********@ gmail.comwrote:
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'
What is to stop all the embedded boxes talking at once?

I suspect that the embedded boxes all taking at once is confusing the
serial port driver. Maybe it is creating a break condition that it
doesn't deal with properly? Or perhaps I've misunderstood the
topology!

What sort of flow control are you using? Could it have got out of
step with XON-XOFF?

Assuming the driver is locking up then it looks like a serial port
driver bug. In my day job I do a lot of stuff with serial ports and
I've found that drivers vary wildly in quality!

My advice is to try a different serial port hardware. I've found ones
based on the PL2303 chip to be very reliable both under windows and
linux.

Eg this one :-

http://www.scan.co.uk/Products/Produ...roductID=98192

Or one of these (which were based on PL2303 last time I bought one)

http://www.comtrol.com/products/cata...=usbserialhubs

I don't think anything you can do from python/user-space should be
able to lock up the kernel mode serial driver. If it does lock up it
is a driver bug.

Here you'll find a little program I wrote which, with the help of a
loopback connector, you can check your serial port out

http://www.craig-wood.com/nick/pub/cambert.exe

Run the program from a cmd prompt and it will tell you how to use it.

I've broken a lot of serial port drivers with that program ;-)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 15 '06 #2
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.serialut il 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(Lan es):
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(l ane):
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(Lan es)
#<><><><><><><> <><><><><><><>< ><>

#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@nb29:~/py$ ./Thex.py
Looking for connected units
True
Request #1
True
Request #2
Serial port failure. Power cycle units
dad@nb29:~/py$ ./Thex.py
The serial port is unavailable.
Disconnect your USB to Serial adapter, Then
reconnect it and try again.
dad@nb29:~/py$

Does anyone have any ideas?

Thanks,

rob < Am***********@g mail.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

Dec 15 '06 #3

dr***@ultech.co m wrote:
.......snip>
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
Skip that -- I didn't notice that your port.close() was indented.

Dec 15 '06 #4
hg
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.serialut il 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(Lan es):
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(l ane):
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(Lan es)
#<><><><><><><> <><><><><><><>< ><>

#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@nb29:~/py$ ./Thex.py
Looking for connected units
True
Request #1
True
Request #2
Serial port failure. Power cycle units
dad@nb29:~/py$ ./Thex.py
The serial port is unavailable.
Disconnect your USB to Serial adapter, Then
reconnect it and try again.
dad@nb29:~/py$

Does anyone have any ideas?

Thanks,

rob < Am***********@g mail.com >

Where is OpenPort ?

hg

Dec 15 '06 #5
Rob
Here is OpenPort

############### ############### ############### ############### ########
#### OpenPort procedure
############### ############### ############### ############### ########
def OpenPort(name):
BRate = 19200
Tout = 3

try:
# Initialize the port
p = serial.Serial(n ame)
# handle failures gracefully
except SerialException :
print "The serial port is unavailable."
print "Disconnect your USB to Serial adapter, Then"
print "reconnect it and try again."
sys.exit(1)

p.setBaudrate(1 9200)
p.setTimeout(3) #set timeout to 1.5 seconds

# finish opening the port and assign a file handle
p.open()
return p

On Dec 15, 1:07 pm, hg <h...@nospam.or gwrote:
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.serialut il 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(Lan es):
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(l ane):
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(Lan es)
#<><><><><><><> <><><><><><><>< ><>
#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@nb29:~/py$ ./Thex.py
Looking for connected units
True
Request #1
True
Request #2
Serial port failure. Power cycle units
dad@nb29:~/py$ ./Thex.py
The serial port is unavailable.
Disconnect your USB to Serial adapter, Then
reconnect it and try again.
dad@nb29:~/py$
Does anyone have any ideas?
Thanks,
rob < Amateur.N7...@g mail.com >Where is OpenPort ?

hg
Dec 15 '06 #6
hg
Rob wrote:
Here is OpenPort

############### ############### ############### ############### ########
#### OpenPort procedure
############### ############### ############### ############### ########
def OpenPort(name):
BRate = 19200
Tout = 3

try:
# Initialize the port
p = serial.Serial(n ame)
# handle failures gracefully
except SerialException :
print "The serial port is unavailable."
print "Disconnect your USB to Serial adapter, Then"
print "reconnect it and try again."
sys.exit(1)

p.setBaudrate(1 9200)
p.setTimeout(3) #set timeout to 1.5 seconds

# finish opening the port and assign a file handle
p.open()
return p

On Dec 15, 1:07 pm, hg <h...@nospam.or gwrote:
>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.serialut il 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(Lan es):
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(l ane):
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(Lan es)
#<><><><><><><> <><><><><><><>< ><>
#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@nb29:~/py$ ./Thex.py
Looking for connected units
True
Request #1
True
Request #2
Serial port failure. Power cycle units
dad@nb29:~/py$ ./Thex.py
The serial port is unavailable.
Disconnect your USB to Serial adapter, Then
reconnect it and try again.
dad@nb29:~/py$
Does anyone have any ideas?
Thanks,
rob < Amateur.N7...@g mail.com >Where is OpenPort ?

hg
I don't get it: you never pass any parameter to OpenPort

The second thing I wonder about is whether you need to reinit serial every
time .

hg

Dec 15 '06 #7
Rob
Craig,

In the embedded firmware, the each box re-transmits after it finishes
reading the packet. This is a very rudimentary system, and uses no
flow control. The topology is that each embedded box has a master and
a slave port. The master is used to receive data from the upstream
box, and send acks or naks back to the upstream box. The slave is
connected to the master of the next downstream box.

Does that clear up the picture a little bit?

Rob

On Dec 15, 11:30 am, Nick Craig-Wood <n...@craig-wood.comwrote:
Rob <Amateur.N7...@ gmail.comwrote:
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'What is to stop all the embedded boxes talking at once?

I suspect that the embedded boxes all taking at once is confusing the
serial port driver. Maybe it is creating a break condition that it
doesn't deal with properly? Or perhaps I've misunderstood the
topology!

What sort of flow control are you using? Could it have got out of
step with XON-XOFF?

Assuming the driver is locking up then it looks like a serial port
driver bug. In my day job I do a lot of stuff with serial ports and
I've found that drivers vary wildly in quality!

My advice is to try a different serial port hardware. I've found ones
based on the PL2303 chip to be very reliable both under windows and
linux.

Eg this one :-

http://www.scan.co.uk/Products/Produ...roductID=98192

Or one of these (which were based on PL2303 last time I bought one)

http://www.comtrol.com/products/cata...=usbserialhubs

I don't think anything you can do from python/user-space should be
able to lock up the kernel mode serial driver. If it does lock up it
is a driver bug.

Here you'll find a little program I wrote which, with the help of a
loopback connector, you can check your serial port out

http://www.craig-wood.com/nick/pub/cambert.exe

Run the program from a cmd prompt and it will tell you how to use it.

I've broken a lot of serial port drivers with that program ;-)

--
Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
Dec 15 '06 #8
On 2006-12-15, hg <hg@nospam.orgw rote:
>Here is OpenPort

############## ############### ############### ############### #########
#### OpenPort procedure
############## ############### ############### ############### #########
def OpenPort(name):
BRate = 19200
Tout = 3
[...]
>for i in range(3):
port = OpenPort()
I don't get it: you never pass any parameter to OpenPort
The code he posted isn't the code he's actually running.
The second thing I wonder about is whether you need to reinit
serial every time.
I don't know what you mean by "reinit serial".

--
Grant Edwards grante Yow! We are now enjoying
at total mutual interaction in
visi.com an imaginary hot tub...
Dec 15 '06 #9
Rob
I have been modifying the code today, tracing through it and trying to
make it more robust and implement data hiding. That way as someone
reads through my main they will see intuitively what I am doing. For
that reason I went back and added a filename parameter to OpenPort.

The reason I am closing and opening the port is the same reason I
posted. I thought that by opening and closing the port each time (I
don't do it in the 'real' code, I could determine whether the bug lay
with hardware or software, or in the USB to Serial adapter.

Rob

On Dec 15, 3:02 pm, hg <h...@nospam.or gwrote:
Rob wrote:
Here is OpenPort
############### ############### ############### ############### ########
#### OpenPort procedure
############### ############### ############### ############### ########
def OpenPort(name):
BRate = 19200
Tout = 3
try:
# Initialize the port
p = serial.Serial(n ame)
# handle failures gracefully
except SerialException :
print "The serial port is unavailable."
print "Disconnect your USB to Serial adapter, Then"
print "reconnect it and try again."
sys.exit(1)
p.setBaudrate(1 9200)
p.setTimeout(3) #set timeout to 1.5 seconds
# finish opening the port and assign a file handle
p.open()
return p
On Dec 15, 1:07 pm, hg <h...@nospam.or gwrote:
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.serialut il 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(Lan es):
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(l ane):
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(Lan es)
#<><><><><><><> <><><><><><><>< ><>
#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@nb29:~/py$ ./Thex.py
Looking for connected units
True
Request #1
True
Request #2
Serial port failure. Power cycle units
dad@nb29:~/py$ ./Thex.py
The serial port is unavailable.
Disconnect your USB to Serial adapter, Then
reconnect it and try again.
dad@nb29:~/py$
Does anyone have any ideas?
Thanks,
rob < Amateur.N7...@g mail.com >Where is OpenPort ?
hgI don't get it: you never pass any parameter to OpenPort

The second thing I wonder about is whether you need to reinit serial every
time .

hg
Dec 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9091
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by a html document allocated in the same local machine under W98 where the classes and the serial port...
2
13187
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. However, I'm unsure of how exactly I should be designing the program, I thought I could use threading to start class: class scanner(Thread): def __init__(self,port):
13
4825
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
4
11196
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
4
17812
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available with this code sample: ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_SerialPort");
4
5067
by: H J van Rooyen | last post by:
Hi All, I am writing a polling controller for an RS-485 line that has several addressable devices connected. It is a small access control system. All is well- the code runs for anything from three hours to three days, then sometimes when I get a comms error and have to send out a nak character, it fails hard... The traceback below pops up. - the first lines are just some debug prints.
3
11611
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to be working fine when I test it with Hyperterminal . I am using the example program that comes with pyserial, as below. --------------- import serial
9
14396
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying, "That's easy, there's a lot out there, Google it,", 1 is a discussion on it without examples and the other is who knows what. I did find some info on it and have been experimenting. The one example that I liked the best in terms of...
6
6663
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send '%' to serial port and make sure it reached the serial port. 2. Once confirmed, send another character.
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8894
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.