串口故障 [英] Serial port failure
问题描述
大家好,
我对python很新,但不是编程和嵌入式。我有一个我认为与硬件有关的问题,由我在pySerial中执行的软件读取触发
。我正在通过串口以菊花链方式向一组嵌入式盒子发送一条简短的
消息。
当我发送全局消息时,所有连接的单元应该以这种格式回复
他们的Id和Ack''Ack''要确定我没有错过一个数据包,因此一个单位,我执行了三次程序,
发送消息并等待超时,然后我再次执行
迭代。我经常通过前两次迭代
没有问题,但是第三次挂起并崩溃,要求我将
移除Belkin USB到串口适配器,然后重新连接它。这里
是代码:
import sys,os
import serial
import sret
导入时间
from serial.serialutil import SerialException
############### ################################################## ###
#### GetAck程序
######################### ###########################################
def GetAck(p):
response =""
试试:
response = p.readline()
除了SerialException:
print">>>>>超时<<<<<<"
返回-1
res = response.split()
#look返回消息中的确认
reslen = len(响应)
如果reslen 5:
如果res [1] ==''Ack'':
返回res [0 ]
elif res [1] ==''Nak'':
返回0x7F
否则:
返回-1
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<<<<<<<
>>>>Snip <<<<<<
########################## ##########################################
#### GetNumLanes程序
#################################### ################################
def GetNumLanes(Lanes):
打印寻找连接单位
#给出转弯命令并等待回复
msg =" .gt 0 336 \ n"
for i in range(3):
port = OpenPort()
time.sleep(3)
print port.isOpen()
print" Request#%d" %(i + 1)
尝试:
port.writelines(msg)
除了OSError:
打印串口故障。电源循环单元
port.close()
sys.exit(1)
done = False
#运行第一次连接检查
#Loop获得回复,直到我们从GetAck得到-1
完成时==错误:
#lane将为-1(超时),0x7F(Nak),
#或用Ack响应的车道号
lane = GetAck(端口)
if lane> =''0'':
如果False == Lanes.has_key(lane):
Lanes [lane] = True
else:
done = True
port.close()
time.sleep(3)
#报告的车道数量
NumLanes = len(车道)
如果NumLanes == 1:
打印" \ n \ n发现1个单位已连接
否则:
打印" \ n \\ nnFound%d个已连接的单位" %NumLanes
返回NumLanes
################################################## ##################
#### 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
$ b &b;>>>><
>>>>>Snip <<<<<<
########################## ##########################################
####主程序代码部分
################################## ##################################
#打开序列号端口
#尝试打开端口捕获串口错误
port = OpenPort()
#如果我们到达这里,港口就存在了。设置波特率和超时
值
#我需要确定此链上有多少个车道
#首先发送转弯命令
#创建一个车道词典,这样我就可以检查每个车道的响应
车道= {}
#<><><><><><><><><><><><> <><><><>
#调用lane finder实用程序
NumLanes = GetNumLanes(Lanes)
#<><><><><><><><><><><><> ;<><><><>
#if无车道响应,退出实用程序
如果0 = = NumLanes:
打印我找不到任何单位连接。
打印检查你的连接并再试一次
sys.exit(1)
#列出我们在字典中的车道
for n in Lanes:
打印La ne - %s %n
现在,这是我收到的错误消息
dad @ nb29:〜/ py $ ./Thex.py
寻找连接单位
真实
请求#1
真实
请求# 2
串口故障。电源循环装置
dad @ nb29:〜/ py $。/ Thex.py
串口不可用。
断开USB连接串口适配器,然后
重新连接并重试。
爸爸@ nb29:〜/ py $
有没有人有想法?
谢谢,
rob< Am***********@gmail.com >
################################################## ##################
#### 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@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***********@gmail.com >
推荐答案
./ Thex.py
寻找连接单位
True
请求#1
正确
请求#2
串口故障。电源循环单元
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
串口不可用。 />
断开USB转串口适配器,然后
重新连接并再试一次。
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
有没有人有任何想法?
谢谢,
rob< ; Am***********@gmail.com >
Does anyone have any ideas?
Thanks,
rob < Am***********@gmail.com >
这篇关于串口故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!