Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1294

Python • send .bin file via USB Port

$
0
0
Hi Guys,

last year I set up a demo Raspi with 3 printers.

Setup
1xRaspi 3b+ (headless)
3x thermal printer
3x Button connected via GPIO
3X bin. files

When the Raspberry Pi is powered on, it automatically loads and executes the .py file.
Now, I want to send a .bin file from the Pi to the printer, when one of the buttons is pushed via USB port instead of ethernet.

the printer can receive and use .bin files. nothing to worry about here.

If possible, I’d like to avoid using tools like CUPS or similar setups. A simple "send-and-hope-for-the-best" solution is perfectly fine!

Could someone guide me on which command to use or where to look? I’ve done some research, but I’m still not entirely sure how to proceed.

The below sample was working fine for my needs so far, but now I need to send via USB as well.
I`m sure its not the most sophisticated solution, but its made with zero knowledge from my side only a lot of research, copy and paste and trial and error.

Thanks so much for your help!


Code:

import RPi.GPIO as GPIO  from time import sleep       import subprocessimport RPi.GPIO as GPIOBASEMODELL = 21RETRACTOR = 20PRESENTER = 16             GPIO.setmode(GPIO.BCM)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           GPIO.setup(BASEMODELL,GPIO.IN,pull_up_down=GPIO.PUD_UP)                                                                 GPIO.setup(RETRACTOR,GPIO.IN,pull_up_down=GPIO.PUD_UP)                                                                  GPIO.setup(PRESENTER,GPIO.IN,pull_up_down=GPIO.PUD_UP)   # Define a threaded callback function to run in another thread when events are detected  def print_RETRACTOR(channel):      if GPIO.input(RETRACTOR):             print ("falling 20")        result = subprocess.run(["bash", "-c", "cat /home/tps/1.bin | nc -w 1 192.168.0.100 9000"],shell=False)    else:                          print ("rising 20")     GPIO.add_event_detect(RETRACTOR, GPIO.BOTH, callback=print_RETRACTOR, bouncetime=200)    def print_PRESENTER(channel):      if GPIO.input(PRESENTER):             print ("falling 16")        result = subprocess.run(["bash", "-c", "cat /home/tps/specs.bin | nc -w 1 192.168.0.100 9000"],shell=False)            else:                          print ("rising 16")     GPIO.add_event_detect(PRESENTER, GPIO.BOTH, callback=print_PRESENTER, bouncetime=200)  def print_BASEMODELL(channel):      if GPIO.input(BASEMODELL):              print ("falling 21")        result = subprocess.run(["bash", "-c", "cat /home/tps/specs.bin | nc -w 1 192.168.0.100 9000"],shell=False)            else:                           print ("rising 21")     GPIO.add_event_detect(BASEMODELL, GPIO.BOTH, callback=print_BASEMODELL, bouncetime=200)   try:    while True:        print ("When pressed, you'll see: falling Edge ")          print ("When released, you'll see: rising")          sleep(5)                 print ("Time's up. Finished!")          finally:                         GPIO.cleanup()        

Statistics: Posted by SIG-Ricker — Wed Jan 22, 2025 1:52 pm



Viewing all articles
Browse latest Browse all 1294

Trending Articles