netUpload.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. # Uploads file to BDOS over network.
  3. # File is written to current path.
  4. import socket
  5. import sys
  6. from time import sleep
  7. # read file to send
  8. filename = "code.bin" # default file to send
  9. if len(sys.argv) >= 2:
  10. filename = sys.argv[1]
  11. outFilename = filename # name of the file on FPGC
  12. # TODO: check on valid 8.3 filename
  13. if len(sys.argv) >= 3:
  14. outFilename = sys.argv[2]
  15. with open(filename, "rb") as f:
  16. binfile = f.read()
  17. downloadToFile = True
  18. # init connection
  19. port = 3220
  20. for idx, attempt in enumerate(range(10)):
  21. try:
  22. s = socket.socket()
  23. s.connect(("192.168.0.213", port))
  24. bdata = ""
  25. if downloadToFile:
  26. bdata += "DOWN "
  27. else:
  28. bdata += "EXEC "
  29. bdata += str(len(binfile)) + ":"
  30. if downloadToFile:
  31. bdata += outFilename
  32. bdata += "\n"
  33. bdata = bdata.encode()
  34. bdata = bdata + binfile
  35. s.send(bdata)
  36. rcv = s.recv(1024)
  37. if rcv != b'THX!':
  38. print("Got wrong response:")
  39. print(rcv)
  40. s.close()
  41. except Exception as e:
  42. s.close()
  43. #print(e)
  44. sleep(0.5)
  45. else:
  46. break
  47. if idx == 9:
  48. print("ERROR: could not send", outFilename)
  49. if downloadToFile:
  50. print("File sent")
  51. else:
  52. print("Program sent")