树莓派串行数据传输输入输出延迟 [英] Serial data transmission input-output delay with Raspberry Pi

查看:56
本文介绍了树莓派串行数据传输输入输出延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

  • 以无线方式驱动多个伺服/遥控电机从一个 pi 到另一个 pi.
  • 本质上,我想使用 pi 构建一个 RC 遥控器,接收端使用第二个 pion.

现在我通过一个稳定且损坏条目很少的 RF 链接模块进行串行传输.由于RF链接模块,串行传输的最大波特率为4800.

Now I have a serial transmission over a RF link module that is stable and has very few corrupt entries. The serial transmission has a max baud rate of 4800 due to the RF link module.

问题:

发射器 pi 打印值和接收器 pi 打印值之间似乎有 2-4 秒的差异.我无法弄清楚这种延迟来自何处以及为什么,以及为什么如此之大.请注意,接收 pi 上的信号与发射机 pi 发送的数据完全相同,但要晚 2-4 秒.即使当我绕过发射器/接收器模块并使用跳线连接 Tx 和 Rx 引脚时,也看到了相同的延迟.

It seems there is a 2-4 seconds difference between the transmitter pi printing values and the receiver pi printing values. I cannot figure out where and why this delay comes from and why is it so large. Please note that the signal on the receiving pi is exactly the same data that is sent by the transmitter pi, but 2-4 seconds later. EVEN when I bypassed the the transmitter/receiver module and connected the Tx and Rx pins with a jumper wire the same delay was seen.

是什么导致接收 Pi 上的数据解码这么晚?我已经粘贴了下面的代码.

What is causing the data on the receiving Pi to be decoded so much later? I have pasted in the code below.

import serial
import struct

ser = serial.Serial("/dev/ttyAMA0")
ser.baudrate = 4800

iCount = 0
bProgramLoop = True
while (bProgramLoop == True):

    iOne = iCount
    if (iOne == 100):
        iOne = 0
        iTwo += 1
        iCount = 0
    if (iTwo == 100):
        iTwo = 0
        iThree += 1
    if (iThree == 100):
        iThree = 0
        iFour += 1
    if (iFour == 100):
        iFour = 0
        iFive += 1
    if (iFive == 100):
        iFive = 0

    lData = [iOne,iTwo,iThree,iFour,iFive] # i have done it like this so that I can track in real time where the transmitter is and receiver is with respect to real time changes on the transmitter side
    sData = struct.pack('5B',*lData)
    ser.write(sData)
    print sData

    iCount += 1

-------------- Rx Pi -----------------

import serial
import struct

ser = serial.Serial("/dev/ttyAMA0")
ser.baudrate = 4800

bProgramLoop = True
while (bProgramLoop == True):
    sSerialRead = ser.read(5)
    tData = struct.unpack('5B',sSerialRead)
    print tData

Tx Pi 打印字符串 sData 和 Rx Pi 打印多组 tData 之间的时间差在 2-4 秒之间.struct.unpack 函数慢吗?

The time difference between when the Tx Pi prints the string sData and the Rx Pi prints the touple tData is somewhere between 2-4 seconds. Is the struct.unpack function slow?

我需要这个时差绝对最小.有什么想法吗?

I need this time difference to be absolutely minimal. Any ideas?

推荐答案

首先,我不确定 ser.write() 是否是异步调用.如果这是使用 pySerial 库,文档会说这是一个阻塞调用.

First, I'm not sure the ser.write() is an async call. If this is using the pySerial library, the docs say that it is a blocking call.

也许试试:

...
ser.write(sData)
ser.flush()    # Force the 'send'
print sData
...

此外,您的 ldata 可能更容易填充:

Also, your ldata might be easier to populate like so:

lData = [iCount % 100, iCount % 10000, ...etc]

此外,设置写入超时可能会有所帮助(但我不这么认为).

Also, setting a write timeout might help (but I don't think so).

这篇关于树莓派串行数据传输输入输出延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆