来自串行端口的数据采用垂直格式



我有一个使用条形码扫描仪从串行端口 (TXRX( 读取的 python 脚本,一切正常,但是当来自串行端口的文本以垂直格式传入时,我的问题,如下所示:

如果我正在读取的条形码有123456,它在我的 Python 脚本中显示为:

1
2
3
4
5
6

我尝试更改 print(( 选项,但似乎没有任何运气。

import sys
import serial
ser = serial.Serial("/dev/ttyAMA0",115200,timeout=0.8)
print('serial test start ...')
if ser != None:
print('serial ready...')
else:
print('serial not ready')
sys.exit()
ser.timerout=0.8 #read time out
ser.writeTimeout = 0.8 #write time out.
try:
x = ""
while True:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
print(str(x)) #<--this one shows what it reads,line by line.
except KeyboardInterrupt:
#print(str(x)) #<--this work fine when I I terminate the loop.
if ser != None:
ser.close()

我希望我捕获的文本看起来像:

123456

更新我的代码后,如果我添加:

try:
x = ""
while True:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
print(str(x))
except KeyboardInterrupt:
if ser != None:
ser.close()
print(str(x))

我得到这个结果:(我正在阅读条形码X001PB45ZF(

X
X0
X00
X001
X001P
X001PB
X001PB4
X001PB45
X001PB45Z
X001PB45ZF

如果我将其添加到循环之外:

try:
x = ""
while True:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
except KeyboardInterrupt:
print(str(x))
if ser != None:
ser.close()

我得到这个结果,但只有当我终止程序时。

X001PB45ZF

我将其添加到循环中的代码中:

try:
while True:
t = ser.read()
if t != b'':
ser.write(t)
print(repr(t)) 

输出现在如下所示:

'X'
'0'
'0'
'1'
'P'
'B'
'4'
'5'
'Z'
'F'
'r'

现在我看到最后的r,我可以终止我的循环,对吧? 并根据需要捕获文本?我仍在尝试弄清楚当扫描仪给出r时如何终止循环......

它现在工作了!!

try:
x = ""
# look for rn. if this doesn't work
# try x00
while 'r' not in x:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
x = x.strip() # removes whitepace from beginning and end of string
# including r n
print(str(x)) 
except KeyboardInterrupt:
#print(str(x)) #<--this work fine when I I terminate the loop.
if ser != None:
ser.close()

现在我可以在一行上捕获输入,如何将其添加到无限循环中?

我的目标是读取条形码,将其存储在 txt 或数据库中。条形码处于运动模式,这意味着,一旦相机检测到运动,条形码就会激活并尝试读取。

我相信您的代码正在按预期工作。while True:是一个无限循环。通常,您将 True 替换为您测试 True 的条件。

现在的诀窍是弄清楚如何打破你的循环。我们可以测试什么条件?许多条形码扫描仪会在消息末尾发送回车符 (\r = b"\x0d"( 或换行符 ( = b"\x0a"(。或者其他角色。

您可能需要挖掘条形码扫描仪的手册,看看是否可以将其配置为发送终止字符。

假设你在传输结束时得到'\r',你可以修改 while 循环为:

import sys
import serial
ser = serial.Serial("/dev/ttyAMA0",115200,timeout=0.8)
fid = open('output.txt', 'w')
print('serial test start ...')
if ser != None:
print('serial ready...')
else:
print('serial not ready')
sys.exit()
ser.timerout=0.8 #read time out
ser.writeTimeout = 0.8 #write time out.
try:
x = b""
# look for rn. if this doesn't work
# try x00
while b"r" not in x:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
x = x.strip() # removes whitepace from beginning and end of string
# including r n
x = str(x)
fid.write(x + 'n')  # write barcode to output file, one per line
except KeyboardInterrupt:
#print(str(x)) #<--this work fine when I I terminate the loop.
fid.close()
if ser != None:
ser.close()

我已经将代码中的缩进重做为每个缩进 4 个空格。像你一样,我更喜欢 2 个空格。但是官方风格指南说4,迟早你可能不得不和别人一起工作,其他人都使用4。

上面的代码应该可以工作一次。如果要让它保持运行,直到发生键盘中断,则需要第二个 while 循环。

import sys
import serial
ser = serial.Serial("/dev/ttyAMA0",115200,timeout=0.8)
fid = open('output.txt', 'w')
print('serial test start ...')
if ser != None:
print('serial ready...')
else:
print('serial not ready')
sys.exit()
ser.timerout=0.8 #read time out
ser.writeTimeout = 0.8 #write time out.
try:
x = b""
# look for rn. if this doesn't work
# try x00
while True:  # outer while loop. keeps writing barcodes to file until
# keyboard interrupt occurs
while b"r" not in x:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
x = x.strip() # removes whitepace from beginning and end of string
# including r n
x = str(x)
fid.write(x + 'n')  # write barcode to output file, one per line
except KeyboardInterrupt:
#print(str(x)) #<--this work fine when I I terminate the loop.
fid.close()
if ser != None:
ser.close()

在 while 循环完成之前尽量不要打印。

try:
x = ""
while True:
t = ser.read()
if t != b'':
ser.write(t)
x = x + t
#print(str(t))
except KeyboardInterrupt:
print(str(x))
if ser != None:
ser.close()

只是想给你另一个解决方案。

您可以尝试在stdout上书写,而不是打印。

以下是您的一个片段:

try:
import sys
while True:
t = ser.read()
if t != b'':
ser.write(t)
sys.stdout.write(str(t))

如果这不起作用,则可能是您在输入中收到了'n'。 如果是这种情况,您可以使用str(t).replace('n', '')

最新更新