netUpload.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. # Add three bytes of 0 at the end
  18. binfile += b'\x00\x00\x00'
  19. downloadToFile = True
  20. # init connection
  21. port = 3220
  22. for idx, attempt in enumerate(range(10)):
  23. try:
  24. s = socket.socket()
  25. s.connect(("192.168.0.213", port))
  26. bdata = ""
  27. if downloadToFile:
  28. bdata += "DOWN "
  29. else:
  30. bdata += "EXEC "
  31. bdata += str(len(binfile)) + ":"
  32. if downloadToFile:
  33. bdata += outFilename
  34. bdata += "\n"
  35. bdata = bdata.encode()
  36. bdata = bdata + binfile
  37. s.send(bdata)
  38. rcv = s.recv(1024)
  39. if rcv != b'THX!':
  40. print("Got wrong response:")
  41. print(rcv)
  42. s.close()
  43. except Exception as e:
  44. s.close()
  45. #print(e)
  46. sleep(0.5)
  47. else:
  48. break
  49. if idx == 9:
  50. print("ERROR: could not send", outFilename)
  51. if downloadToFile:
  52. print("File sent")
  53. else:
  54. print("Program sent")