(Raspberry Pi-Model 3 B +)从串行端口ttyS0或ttyAMA读取时得到错误的值

时间:2019-08-07 18:46:33

标签: python raspberry-pi3 raspbian uart

我有一个python代码,用于以1.5Mbps的波特率从串行端口“ ttyS0 ”或“ ttyAMA0 ”读取。

在我的代码中,我基本上是从 uart缓冲区中读取的,将获取的字节转换为十六进制,将读取的输出保存到列表中,并且只要列表中的元素达到10000,就打印结果。

然后我手动检查它是否正确。

import serial
import time

def getUART():
    # baudrateDaRede = 1507500
    baudrateDaRede = 1500000
    # tty = "ttyAMA0"
    tty = "ttyS0"
    ser = serial.Serial()
    ser.port='/dev/'+tty
    ser.baudrate=1234
    ser.parity=serial.PARITY_EVEN
    ser.stopbits=serial.STOPBITS_ONE
    ser.bytesize=serial.EIGHTBITS
    ser.timeout=0.1
    ser.open()
    ser.close()

    ser = serial.Serial()
    ser.port='/dev/'+tty
    ser.baudrate=baudrateDaRede
    ser.parity=serial.PARITY_EVEN
    ser.stopbits=serial.STOPBITS_ONE
    ser.bytesize=serial.EIGHTBITS
    ser.timeout=None
    ser.exclusive = True
    ser.open()
    time.sleep(2)
    index = 0
    vet = []
    while True:
        # time.sleep(0.01)
        if ser.inWaiting() == 0:
                continue
        uartRead = ser.read(ser.inWaiting())
        uartReadhex = uartRead.hex()
        if not index < 200:
            vet.append(uartReadhex)

        if len(vet) > 10000:
            uartHexa = "".join(vet)
            print(uartHexa)
            vet.clear()
            break
        index += 1

getUART()

但是,在检查结果时,有时部分结果缺少一些 0 或比其应有的 0

示例:读取输出时,其中一部分应该是:

(十六进制) 6807076860027d00000000df1668

但是,我得到了:

(十六进制) 6807076860027d0000000000df1668

它偶尔会发生,但这足以使我的软件无法正常工作,因为此十六进制表示数据包(在这种情况下,它将是损坏的数据包)。< / p>

PS:我肯定知道数据包并没有真正损坏,因为我使用了树莓以外的其他硬件进行检查,并获得了正确的值。

我尝试使用 minicom 来读取端口,但遇到了同样的问题,因此我认为我的代码不是这里的问题,可能与树莓派本身有关

我正在使用 Raspbian(2019-04-08-raspbian-stretch) Raspberry Pi-Model 3 B + 上运行代码。

我对其进行了超频,所以我在rapsbian的配置文件下发布了

# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
arm_freq=1200
enable_uart=1
force_turbo=1
core_freq=425
gpu_freq=425

#test to over clock ttyAMA0

#init_uart_clock=96000000
#init_uart_baud=1500000
# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi
#dtoverlay=pi3-disable-bt
# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

0 个答案:

没有答案