文件传输 PC 到 Raspberry Pi(使用 xBee) [英] File Transfer PC to Raspberry Pi (with xBee)

查看:46
本文介绍了文件传输 PC 到 Raspberry Pi(使用 xBee)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 xBee Pro S2C 模块.我正在尝试使用 xBee 模块将图像从 Windows PC 发送到 Raspberry Pi.我将 xBees 配置为 API 模式,并且可以使用 Python 代码接收/发送 AT 文本消息.

I've two xBee Pro S2C module. I'm trying to send a image from Windows PC to Raspberry Pi with xBee modules. I configured my xBees for API mode and i can receive/send AT text messages with Python codes.

我想将图像从我的 PC 发送到 Raspberry Pi Model 3 B+

I want to send a image from my PC to Raspberry Pi Model 3 B+

我也检查了这个主题:http://cms.digi.com/support/forum/70518/transfer-image-between-xbee-modules-connected-raspberry-each

我做了一些更改并尝试了下面的代码.

I made some changes and tried codes below.

PC 端(发送方)##########################

PC side(sender) ###########################

ser = serial.Serial('COM6', 9600,timeout=.5)
fc= 'xbee.jpg'
File = open(fc,'r')
#while True:
line = ser.readline()
a= File.read()
print(str(a))
ser.write(str(a))

############################

#############################

Raspberry Pi 端(接收器)############################

Raspberry Pi side (receiver) #############################

ser = serial.Serial('/dev/ttyUSB0', 9600,timeout=.5)

while True:
incoming = ser.readline().strip()


file = open ('images.png','wb')

print (incoming)
file.write(incoming)
file.close()
content= file.read()

################################

#################################

然后我得到一个错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 240: character maps to <undefined>

我也想到了一个主意.我制作了一个脚本,将图像转换为 base64 代码并保存为 .txt 文件.我改变了images.png"作为test.txt".没有发生错误,但我的树莓派上也没有创建 .txt 文件.

Also an idea came to my mind. I made a script converts an image to base64 code and saves as a .txt file. I changed "images.png" as "test.txt". There is no error occured but also there is no .txt file created on my raspberry pi.

这对我来说真的很重要.请帮忙.

Thats really important for me. Please help.

更新

我也试过这个代码:https://github.com/MortadhaDhkar/zigbee-文件传输

没有发生错误.但是文件也不会接收到 Raspbbery Pi.Sender.py 输出是:

There is no error occured. But also file don't receive to Raspbbery Pi. Sender.py out put is:

Sending Header...
Sending File...
Done

推荐答案

您不能将 readline() 用于二进制数据,例如图像,因为它是用于文本的.它基本上寻找换行符来分隔文本行,但是当您有图像时,它们是二进制的,如果像素的亮度值为 10,则可能包含换行符(过于简化的示例).

You can't use readline() with binary data such as images because it is meant for text. It basically looks for linefeed characters to delimit lines of text, but when you have images they are binary and may contain the linefeed character if a pixel happens to have the brightness value of 10 (over-simplified example).

相反,我们需要建立一种方法让接收者知道期望的数据字节数,然后它可以读取那么多字节.有很多方法可以做到这一点,但我只是在下面展示了如何以网络顺序发送一个 4 字节的整数,以便接收者可以简单地读取固定数量的 4 字节来获取图像的长度,然后读取构成图像的确切字节数.如果一台机器是大端,另一台是小端,我按网络字节顺序进行.这允许传输多达 2GB 的图像,与典型的 JPEG/PNG 图像大小相比,这巨大.

Instead, we need to establish a way for the receiver to know how many bytes of data to expect and it can then just read that many bytes. There are many ways to do that, but I just show below how to send a 4-byte integer, in network order so that the receiver can simply read a fixed number of 4 bytes to get the length of the image, and then read the exact number of bytes that make up the image. I do it in network byte order in case one machine is big-endian and the other little-endian. This allows transmission of up to 2GB images which is enormous when compared to typical JPEG/PNG image sizes.

所以,这里是发件人:

#!/usr/bin/env python3

import serial
import struct

# Open image and slurp entire contents
with open('image.jpg', 'rb') as f:
    image = f.read()
    nbytes = len(image)
    print(f'DEBUG: Image length={nbytes}')

# Open serial connection
with serial.Serial('/dev/ttyS0', 9600) as s:
    # Send 4-byte network order long with image size
    s.write(struct.pack('!L', nbytes))
    # Send image itself
    s.write(image)

这里是接收器:

#!/usr/bin/env python3

import serial
import struct

# Open serial connection
with serial.Serial('/dev/ttyS0', 9600) as s:
    # Read 4-byte network order long with image size
    print(f'DEBUG: Waiting for 4-byte header')
    nbytes = struct.unpack('!L', s.read(4))[0]
    print(f'DEBUG: Image length={nbytes}')
    # Read image itself
    image = s.read(nbytes)

# Open image and write
with open('image.jpg', 'wb') as f:
    f.write(image)

关键字:树莓派、串口、pyserial、发送图像、接收图像、帧协议.

Keywords: Raspberry Pi, serial, pyserial, send image, receive image, framing protocol.

这篇关于文件传输 PC 到 Raspberry Pi(使用 xBee)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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