netFlash.py 1.2 KB

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