Send file via serial port with Python

I am trying to send a file (image .jpg in this case) via the serial port.

It currently works by calling an external script:

subprocess.Popen(['./sendFile.sh','myImage.jpg']).communicate()

where sendFile.sh:

cp /home/pi/$1 /dev/ttyAMA0

This method works, but is somehow unstable. Sometimes my Python program stops after file transfer.

I am wondering if there is a way to do this in Python instead of calling the script for reference? I searched for XMODEM, but it looks like both ends should install it. I am sending an image to a GPRS chip that does not allow me to change any code on it. Therefore, installing something on the receiving side is not possible.

+4
source share
2 answers
import serial
s = serial.Serial("/dev/ttyAMA0")
s.write(open("target.txt","rb").read())
+1
source

os.open dev:

f = os.open('/dev/ttyAMA0', os.O_RDWR)
f.write(open("myImage.jpg").read))
f.close()
0

Source: https://habr.com/ru/post/1620204/


All Articles