Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Pyserial发送文件?_Python_Radio_Pyserial_Xmodem - Fatal编程技术网

Python 使用Pyserial发送文件?

Python 使用Pyserial发送文件?,python,radio,pyserial,xmodem,Python,Radio,Pyserial,Xmodem,我有一个树莓Pi通过两个无线电模块连接到我的MacBookPro。到目前为止,我已经成功地使用pyserial将字符串和命令从一个设备发送到另一个设备,但是,我找不到发送文本文件的方法。与超级终端类似,您可以选择通过xmodem发送文本文件。我已经下载了xmodem库并使用了它,我想我能够发送文件,但我不知道如何在另一端接收它们。 有什么帮助吗?这个问题不是很清楚。。。您只需通过串行端口发送字节。。。客户端将字节保存到文件中。下面是一个简单的实现 服务器代码 从串行导入串行 ser=串行(“co

我有一个树莓Pi通过两个无线电模块连接到我的MacBookPro。到目前为止,我已经成功地使用pyserial将字符串和命令从一个设备发送到另一个设备,但是,我找不到发送文本文件的方法。与超级终端类似,您可以选择通过xmodem发送文本文件。我已经下载了xmodem库并使用了它,我想我能够发送文件,但我不知道如何在另一端接收它们。
有什么帮助吗?

这个问题不是很清楚。。。您只需通过串行端口发送字节。。。客户端将字节保存到文件中。下面是一个简单的实现

服务器代码
从串行导入串行
ser=串行(“com4”)或其他类型
readline=lambda:iter(lambda:ser.read(1),“\n”)
而“.加入(readline())!=“”:#等待客户端请求文件
通过#什么也不做。。。只是等待。。。如果我们不想不停地循环,我们可以使用time.sleep()
ser.write(打开(“some_file.txt”,“rb”).read())#发送文件
ser.write(“\n\n”)#发送指示文件传输完成的消息
客户端代码
从串行导入串行
ser=串行(“com4”)或其他类型
ser.write(“\n”)#告诉服务器我们已准备好接收
readline=lambda:iter(lambda:ser.read(1),“\n”)
以open(“somefile.txt”、“wb”)作为输出文件:
尽管如此:
line=”“.join(readline())
如果行==“”:
中断#完成,停止累积线
打印>>输出文件,行

这是一个过度简化的例子,应该可以工作,但它假设100%正确传输,这并不总是实现。。。一个更好的方案是用校验和逐行发送,以验证正确的传输,但其基本思想是相同的。。。校验和将是OP的一个exercize

比方说,我们想发送一个图像(a.png),而不是一个.txt文件。。。假设我们发送到的设备是一台打印机。。。我该如何以打印机能够识别并打印图像的格式发送数据,而不是输出ascii giberish或unicode giberish?我使用
ser.write(open(“file.png”,“rb”).read())
发送文件。我可以为这个打开另一个线程。。。但这真的值得吗?你需要从打印机文档中获取这些信息。。。您可能需要先将file.png转换为其他文件
from serial import Serial
ser = Serial("com4") #or whatever 
readline = lambda : iter(lambda:ser.read(1),"\n")
while "".join(readline()) != "<<SENDFILE>>": #wait for client to request file
    pass #do nothing ... just waiting ... we could time.sleep() if we didnt want to constantly loop
ser.write(open("some_file.txt","rb").read()) #send file
ser.write("\n<<EOF>>\n") #send message indicating file transmission complete
from serial import Serial
ser = Serial("com4") #or whatever 
ser.write("<<SENDFILE>>\n") #tell server we are ready to recieve
readline = lambda : iter(lambda:ser.read(1),"\n")
with open("somefile.txt","wb") as outfile:
   while True:
       line = "".join(readline())
       if line == "<<EOF>>":
           break #done so stop accumulating lines
       print >> outfile,line