用于 python 的 XMODEM

发布于 2024-07-09 11:24:31 字数 98 浏览 12 评论 0原文

我正在编写一个程序,需要使用 XMODEM 从传感器设备传输数据。 我想避免编写自己的 XMODEM 代码,所以我想知道是否有人知道是否有可用的 python XMODEM 模块?

I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

葬心 2024-07-16 11:24:31
def xmodem_send(serial, file):
t, anim = 0, '|/-\\'
serial.setTimeout(1)
while 1:
    if serial.read(1) != NAK:
        t = t + 1
        print anim[t%len(anim)],'\r',
        if t == 60 : return False
    else:
        break

p = 1
s = file.read(128)
while s:
    s = s + '\xFF'*(128 - len(s))
    chk = 0
    for c in s:
        chk+=ord(c)
    while 1:
        serial.write(SOH)
        serial.write(chr(p))
        serial.write(chr(255 - p))
        serial.write(s)
        serial.write(chr(chk%256))
        serial.flush()

        answer = serial.read(1)
        if  answer == NAK: continue
        if  answer == ACK: break
        return False
    s = file.read(128)
    p = (p + 1)%256
    print '.',
serial.write(EOT)
return True
def xmodem_send(serial, file):
t, anim = 0, '|/-\\'
serial.setTimeout(1)
while 1:
    if serial.read(1) != NAK:
        t = t + 1
        print anim[t%len(anim)],'\r',
        if t == 60 : return False
    else:
        break

p = 1
s = file.read(128)
while s:
    s = s + '\xFF'*(128 - len(s))
    chk = 0
    for c in s:
        chk+=ord(c)
    while 1:
        serial.write(SOH)
        serial.write(chr(p))
        serial.write(chr(255 - p))
        serial.write(s)
        serial.write(chr(chk%256))
        serial.flush()

        answer = serial.read(1)
        if  answer == NAK: continue
        if  answer == ACK: break
        return False
    s = file.read(128)
    p = (p + 1)%256
    print '.',
serial.write(EOT)
return True
青衫负雪 2024-07-16 11:24:31

PyPi 上有 XMODEM 模块。 它使用 XModem 处理数据的发送和接收。 以下是其用法示例:

import serial
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
from xmodem import XMODEM, NAK
from time import sleep

def readUntil(char = None):
    def serialPortReader():
        while True:
            tmp = port.read(1)
            if not tmp or (char and char == tmp):
                break
            yield tmp
    return ''.join(serialPortReader())

def getc(size, timeout=1):
    return port.read(size)

def putc(data, timeout=1):
    port.write(data)
    sleep(0.001) # give device time to prepare new buffer and start sending it

port = serial.Serial(port='COM5',parity=serial.PARITY_NONE,bytesize=serial.EIGHTBITS,stopbits=serial.STOPBITS_ONE,timeout=0,xonxoff=0,rtscts=0,dsrdtr=0,baudrate=115200)
port.write("command that initiates xmodem send from device\r\n")
sleep(0.02) # give device time to handle command and start sending response
readUntil(NAK)
buffer = StringIO()
XMODEM(getc, putc).recv(buffer, crc_mode = 0, quiet = 1)
contents = buffer.getvalue()
buffer.close()
readUntil()

There is XMODEM module on PyPi. It handles both sending and receiving of data with XModem. Below is sample of its usage:

import serial
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
from xmodem import XMODEM, NAK
from time import sleep

def readUntil(char = None):
    def serialPortReader():
        while True:
            tmp = port.read(1)
            if not tmp or (char and char == tmp):
                break
            yield tmp
    return ''.join(serialPortReader())

def getc(size, timeout=1):
    return port.read(size)

def putc(data, timeout=1):
    port.write(data)
    sleep(0.001) # give device time to prepare new buffer and start sending it

port = serial.Serial(port='COM5',parity=serial.PARITY_NONE,bytesize=serial.EIGHTBITS,stopbits=serial.STOPBITS_ONE,timeout=0,xonxoff=0,rtscts=0,dsrdtr=0,baudrate=115200)
port.write("command that initiates xmodem send from device\r\n")
sleep(0.02) # give device time to handle command and start sending response
readUntil(NAK)
buffer = StringIO()
XMODEM(getc, putc).recv(buffer, crc_mode = 0, quiet = 1)
contents = buffer.getvalue()
buffer.close()
readUntil()
凉月流沐 2024-07-16 11:24:31

我认为你只能自己动手。

您也许可以使用 sz,它实现了 X/Y/ZMODEM。 您可以调用二进制文件,或者将必要的代码移植到 Python。

I think you’re stuck with rolling your own.

You might be able to use sz, which implements X/Y/ZMODEM. You could call out to the binary, or port the necessary code to Python.

动听の歌 2024-07-16 11:24:31

这是 XMODEM 文档的链接,如果您必须编写该文档,该链接将很有用你自己。 对原来的XMODEM、XMODEM-CRC、XMODEM-1K有详细的说明。

您可能还会发现这个 c 代码 感兴趣。

Here is a link to XMODEM documentation that will be useful if you have to write your own. It has detailed description of the original XMODEM, XMODEM-CRC and XMODEM-1K.

You might also find this c-code of interest.

缱绻入梦 2024-07-16 11:24:31

您可以尝试使用 SWIG 为上面链接的 C 库(或任何其他 C/C++ 库)创建 Python 绑定你在网上找到的)。 这将允许您直接从 Python 使用相同的 C API。

当然,实际的实现仍将采用 C/C++ 语言,因为 SWIG 仅创建与感兴趣的函数的绑定。

You can try using SWIG to create Python bindings for the C libraries linked above (or any other C/C++ libraries you find online). That will allow you to use the same C API directly from Python.

The actual implementation will of course still be in C/C++, since SWIG merely creates bindings to the functions of interest.

寄意 2024-07-16 11:24:31

有一个 python 模块可以使用 -> https://pypi.python.org/pypi/xmodem

您可以在<中看到传输协议a href="http://pythonhosted.org//xmodem/xmodem.html" rel="nofollow">http://pythonhosted.org//xmodem/xmodem.html

There is a python module that you can use -> https://pypi.python.org/pypi/xmodem

You can see the transfer protocol in http://pythonhosted.org//xmodem/xmodem.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文